sklearn.tree.ExtraTreeClassifier

class sklearn.tree.ExtraTreeClassifier(*, criterion='gini', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, ccp_alpha=0.0)

[源码]

极为随机的树分类器。

额外树在构建方式上与经典决策树不同。当寻找最佳分割以将节点的样本分为两组时,将为每个max_features随机选择的特征绘制随机分割,然后在这些特征中选择最佳分割。当 max_features设置为1时,这等于建立一个完全随机的决策树。

警告:额外树只能在集成方法中使用。

用户指南中阅读更多内容。

参数 说明
criterion {“gini”, “entropy”}, default=”gini”
该函数用来测量分割的质量。支持的标准基尼不纯度为“基尼系数”和信息增益为“熵系数”。
splitter {“random”, “best”}, default=”random”
用于在每个节点选择分割的策略。支持的策略是“最佳”选择最佳分割和“随机”选择最佳随机分割。
max_depth int, default=None
树的最大深度。 如果为None,则将节点展开,直到所有叶子都是纯净的,或者直到所有叶子都包含少于min_samples_split个样本。
min_samples_split int or float, default=2
分割一个内部节点所需的最小样本数:
- 如果是int,那么 min_samples_split直接作为最小数目.
- 如果是float,那么min_samples_split是一个分数,ceil(min_samples_split * n_samples)是每个分割的最小样本数。
从0.18版开始发生变化: 增加分数型的浮点值.
min_samples_leaf int or float, default=1
在叶节点处所需的最小样本数。任何深度的分裂点只有在左分支和右分支中的每个分支上至少留下min_samples_leaf个训练样本时才考虑分裂。这可能具有平滑模型的效果,尤其是在回归中。
- 如果为int,则认为min_samples_leaf是最小值。
- 如果为float,min_samples_leaf则为分数, ceil(min_samples_leaf * n_samples)是每个节点的最小样本数。

从0.18版开始发生变化: 增加分数型的浮点值.
min_weight_fraction_leaf float, default=0.0
一个叶节点上所需的(所有输入样本的)总权重的最小加权分数。当不提供sample_weight时,样本的权重相等。
max_features int, float, {“auto”, “sqrt”, “log2”} or None, default=”auto”
当寻找最好的分裂时考虑的特征的数量:
- 如果是整数,则每次使用max_features个特征切分
- 如果是float,则max_features是一个分数,并且每次使用int(max_features * n_features)个特征切分
- 如果是“auto”, 则 max_features=sqrt(n_features).
- 如果是 “sqrt”, 则 max_features=sqrt(n_features).
- 如果是 “log2”, 则 max_features=log2(n_features).
- 如果是 None, 则 max_features=n_features.

注意:在找到节点样本的至少一个有效分区之前,对分割的搜索不会停止,即使它需要有效地检查超过max_features的特性。
random_state int, RandomState instance, default=None
用于随机选择每个拆分中使用的max_features。 有关详细信息,请参见词汇表
max_leaf_nodes int, default=None
以最佳优先的方式生成具有max_leaf_nodes的树。最佳节点定义为杂质的相对减少。如果没有,则无限的叶节点数。
min_impurity_decrease float, default=0.0
一个节点将被分割,如果分割导致不纯度的减少大于或等于这个值.
不纯度的加权减少方程为:
N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity)
其中N为样本总数,N_t为当前节点的样本数,N_t_L为左子节点的样本数,N_t_R为右子节点的样本数。
如果sample_weight被传递,N、N_t、N_t_R和N_t_L都指向加权和。
min_impurity_split float, (default=0)
树木停止生长的阈值。如果一个节点的不纯度超过阈值,那么它就会分裂,否则它就是叶子。
警告: 从版本0.19开始被弃用:min_impurity_split在0.19中被弃用,转而支持min_impurity_decrease。min_impurity_split的默认值在0.23中从1e-7更改为0,在0.25中将被删除。使用min_impurity_decrease代替。
class_weight dict, list of dict or “balanced”, default=None
{class_label: weight}的形式表示与类别关联的权重。如果取值None,所有分类的权重为1。对于多输出问题,可以按照y的列的顺序提供一个字典列表。
注意多输出(包括多标签) ,应在其自己的字典中为每一列的每个类别定义权重。例如:对于四分类多标签问题, 权重应为[{0:1、1:1:1],{0:1、1:5},{0:1、1:1:1},{0:1、1: 1}],而不是[{1:1},{2:5},{3:1},{4:1}]
“平衡”模式使用y的值自动将权重与输入数据中的类频率成反比地调整为n_samples /(n_classes * np.bincount(y))
对于多输出,y的每一列的权重将相乘。
请注意,如果指定了sample_weight,则这些权重将与sample_weight(通过fit方法传递)相乘。
ccp_alpha non-negative float, default=0.0
用于最小化成本复杂性修剪的复杂性参数。 将选择成本复杂度最大且小于ccp_alpha的子树。 默认情况下,不执行修剪。 有关详细信息,请参见最小成本复杂性修剪。

0.22版中的新功能。
属性 说明
classes_ ndarray of shape (n_classes,) or list of ndarray
类标签(单一输出问题)或类标签数组列表(多输出问题)。
max_features_ int
max_features的推断值。
n_classes_ int or list of int
类的数量(用于单个输出问题),或包含每个输出的类数量的列表(用于多输出问题)。
feature_importances_ ndarray of shape (n_features,)
返回特征重要性
n_features_ int
模型训练过程特征数量
n_outputs_ int
模型训练时输出的数量。
tree_ Tree
底层树对象。请参考帮助(sklearn.tree._tree.Tree)了解树对象的属性和了解这些属性的基本用法的决策树结构。

另见

控制树大小的参数的默认值(例如max_depth, min_samples_leaf等)会导致完全生长和未修剪的树,在某些数据集上可能会非常大。为了减少内存消耗,应该通过设置这些参数值来控制树的复杂性和大小。

参考

P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.

示例

>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.ensemble import BaggingClassifier
>>> from sklearn.tree import ExtraTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
...    X, y, random_state=0)
>>> extra_tree = ExtraTreeClassifier(random_state=0)
>>> cls = BaggingClassifier(extra_tree, random_state=0).fit(
...    X_train, y_train)
>>> cls.score(X_test, y_test)
0.8947...

方法

方法 说明
apply(X[, check_input]) 返回每个样本预测为叶的叶索引。
cost_complexity_pruning_path(X, y[, …]) 在最小代价复杂度剪枝过程中计算剪枝路径。
decision_path(X[, check_input]) 返回树中的决策路径.
fit(X, y[, sample_weight, check_input, …]) 从训练集(X, y)构建决策树分类器。
get_depth() 返回决策树的深度。
get_n_leaves() 返回决策树的叶节点数。
get_params([deep]) 获取这个估计器的参数。
predict(X[, check_input]) 预测X的类或回归值。
predict_log_proba(X) 预测输入样本X的类别对数概率。
predict_proba(X[, check_input]) 预测输入样本X的类概率。
score(X, y[, sample_weight]) 返回给定测试数据和标签的平均精度。
set_params(**params) 设置这个估计器的参数。
__init__(*, criterion='gini', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, ccp_alpha=0.0)

[源码]

初始化自身实例。请参阅帮助(type(self))以获得准确的签名。

apply(X, check_input=True)

[源码]

返回每个样本预测为叶的叶索引。

在 0.17 版中新增

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
check_input bool, default=True
允许绕过多个输入检查。 除非您知道自己要做什么,否则不要使用此参数。
返回值 说明
X_leaves array-like of shape (n_samples,)
对于X中的每个数据点x,返回x结尾的叶子的索引。叶子编号在[0; self.tree_.node_count),可能在编号上有间隔。
cost_complexity_pruning_path(X, y, sample_weight=None)

[源码]

在最小代价复杂度剪枝过程中计算剪枝路径。

有关修剪过程的详细信息,请参见最小成本复杂性修剪

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
目标值(类标签)为整数或字符串。
sample_weight array-like of shape (n_samples,), default=None
样本权重。如果为None,那么样本的权重相等。当在每个节点中搜索分割时,将忽略创建具有净零权值或负权值的子节点的分割。如果分割会导致任何一个类在任一子节点中具有负权值,那么分割也将被忽略。
返回值 说明
ccp_path Bunch
类字典的对象,具有以下属性。
ccp_alphas : ndarray
修剪期间子树的有效Alpha。

impurities : ndarray
ccp_alphas中对应alpha值的子树叶子的不纯度之和。
decision_path(X, check_input=True)

[源码]

返回树中的决策路径。

0.18.版中新增功能

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
check_input bool, default=True
允许绕过几个输入检查。除非您知道自己在做什么,否则不要使用此参数。
返回值 说明
indicator sparse matrix of shape (n_samples, n_nodes)
返回一个节点指示器CSR矩阵,其中非零元素表示样本经过节点。
property feature_importances_

返回特性的重要性。

一个特征的重要性被计算为该特征所带来的标准的(归一化)总约简。它也被称为基尼重要性。

警告:针对高基数特性(许多唯一值),基于杂质的特性重要性可能会引起误解。 另请参见sklearn.inspection.permutation_importance

返回值 说明
feature_importances_ ndarray of shape (n_features,)
按特征(基尼重要性)标准的标准化总约简。
fit(X, y, sample_weight=None, check_input=True, X_idx_sorted=None)

[源码]

从训练集(X, y)构建决策树分类器。

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
目标值(类标签)为整数或字符串。
sample_weight array-like of shape (n_samples,), default=None
样本权重。如果为None,则对样本进行平均加权。 在每个节点中搜索拆分时,将忽略创建净净值为零或负权重的子节点的拆分。 如果拆分会导致任何单个类在任一子节点中都负负重,则也将忽略拆分。
check_input bool, default=True
允许绕过多个输入检查。 除非您知道自己要做什么,否则不要使用此参数。
X_idx_sorted array-like of shape (n_samples, n_features), default=None
排序后的训练输入样本的索引。 如果在同一数据集上生长了许多树,则可以在树之间缓存顺序。 如果为None,则将在此处对数据进行排序。 除非您知道要做什么,否则不要使用此参数。
返回值 说明
self DecisionTreeClassifier
训练出来的模型
get_depth()

[源码]

返回决策树的深度。

一棵树的深度是根与任何叶子之间的最大距离。

参数 说明
self.tree_.max_depth int
树的最大深度。
get_n_leaves()

[源码]

返回决策树的叶子数目。

返回值 说明
self.tree_.n_leaves int
叶子数量
get_params(deep=True)

[源码]

​ 获取此模型的参数

参数 说明
deep bool, default=True
如果为True,则将返回此估算器以及估算器所包含子对象的参数。
返回值 说明
params mapping of string to any
参数名与值的映射
predict(X, check_input=True)

[源码]

预测X的类或回归值。

对于分类模型,返回X中每个样本的预测类。 对于回归模型,将返回基于X的预测值。

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
check_input bool, default=True
允许绕过多个输入检查。 除非知道自己要做什么,否则不要使用此参数。
返回值 说明
y array-like of shape (n_samples,) or (n_samples, n_outputs)
预测的类或预测值。
predict_log_proba(X)

[源码]

预测输入样本X的类别对数概率。

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
返回值 说明
proba ndarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1
输入样本的类别对数概率。 类的顺序与属性classes_中的顺序相对应。
predict_proba(X, check_input=True)

[源码]

预测输入样本X的类别概率。

预测的类别概率是叶子中相同类别的样本的分数。

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练输入样本。在内部,它将被转换为' dtype=np 并且如果传入的是稀疏矩阵则转换为csr_matrix矩阵。
check_input bool, default=True
允许绕过多个输入检查。 除非知道自己要做什么,否则不要使用此参数。
返回值 说明
proba ndarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1
输入样本的分类概率。 类的顺序与属性classes_中的顺序相对应。
score(X, y, sample_weight=None)

[源码]

返回给定测试数据与标签的平均准确度。

在多标签分类中,这是子集准确度,这是一个苛刻的指标,因为您需要为每个样本正确预测每个标签集。

参数 说明
X array-like of shape (n_samples, n_features)
测试样本
y array-like of shape (n_samples,) or (n_samples, n_outputs)
X的真实标签。
sample_weight array-like of shape (n_samples,), default=None
样本权重。
返回值 说明
score float
真实值与与测试值的平均准确度。
set_params(**params)

[源码]

设置此估算器的参数。

该方法适用于简单的估计器以及嵌套对象(例如管道)。 后者的参数格式为<component> __ <parameter>,以便可以更新嵌套对象的每个组件。

参数 说明
**params dict
估计器参数。
返回值 说明
self object
估计器实例。