scikit-learn 中决策树模型-参数说明、注解

目录

    • scikit-learn 中决策树算法类库介绍
    • 重要参数
      • `criterion` 特征选择标准
        • 对于分类决策树:
          • 关于基尼指数
        • 对于回归决策树
          • 以 squared_error 为例
      • `splitter` 特征划分点选择标准
      • `max_depth` 最大深度
      • `max_features` 划分时考虑的最大特征数
      • `min_samples_split` 叶子节点允许拆分的最小样本数
      • `min_samples_leafi` 叶子节点的最小样本点数
      • `min_weight_fraction_leaf` min_weight_fraction_leaf
      • `max_leaf_nodes` 最大叶子节点数
      • `class_weight` 类别权重 (不适用与回归树,只适用于分类树)
      • `min_impurity_decrease` 节点划分最小不纯度
      • `ccp_alpha` 用于最小成本复杂度修剪的复杂度参数

scikit-learn 中决策树算法类库介绍

scikit-learn 决策树算法类库,内部使用的是调优过的CART算法,故既可以做分类问题也可以做回归问题 ,分别对应:

  • DecisionTreeClassfier 分类决策树

  • DecisionTreeRegressor 回归决策树

两者的参数定义几乎完全相同,但是意义不全相同。

采用 CART 算法,故强制决策树为二叉树。

主要参考

  • sklearn.tree.DecisionTreeClassifier — scikit-learn 1.0.2 documentation
  • sklearn.tree.DecisionTreeRegressor — scikit-learn 1.0.2 documentation
  • scikit-learn决策树算法类库使用小结 - 刘建平Pinard - 博客园

以下注解主要参考 sklearn 官网文档,由于sklearn 更新较频繁,在参考时请注意自己的 sklearn 版本。

重要参数

criterion 特征选择标准

对于分类决策树:

The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain.
可选:

  • “gini” , 即基尼指数 (默认)
  • “entropy” 即信息增益
    可以使用"gini"或者"entropy",前者代表基尼系数,后者代表信息增益。一般说使用默认的基尼系数"gini"就可以了,即CART算法。除非你更喜欢类似ID3, C4.5的最优特征选择方法。
关于基尼指数

scikit-learn 中决策树模型-参数说明、注解_第1张图片

采用 Gini 指数, 用其代替信息增益比或者信息增益,可以避免采用信息增益在计算熵的过程中频繁的对数运算,节省计算开销。
scikit-learn 中决策树模型-参数说明、注解_第2张图片

对于回归决策树

criterion{“squared_error”, “friedman_mse”, “absolute_error”, “poisson”}, default=”squared_error”
The function to measure the quality of a split. Supported criteria are “squared_error” for the mean squared error, which is equal to variance reduction as feature selection criterion and minimizes the L2 loss using the mean of each terminal node, “friedman_mse”, which uses mean squared error with Friedman’s improvement score for potential splits, “absolute_error” for the mean absolute error, which minimizes the L1 loss using the median of each terminal node, and “poisson” which uses reduction in Poisson deviance to find splits.

New in version 0.18: Mean Absolute Error (MAE) criterion.

New in version 0.24: Poisson deviance criterion.

Deprecated since version 1.0: Criterion “mse” was deprecated in v1.0 and will be removed in version 1.2. Use criterion="squared_error" which is equivalent.

Deprecated since version 1.0: Criterion “mae” was deprecated in v1.0 and will be removed in version 1.2. Use criterion="absolute_error" which is equivalent.

支持的标准是均方误差的“squared_error”,它等于作为特征选择标准的方差减少,并使用每个终端节点的平均值来最小化 L2 损失,“friedman_mse”,它使用均方误差和弗里德曼的潜在改进分数 分割,“absolute_error”表示平均绝对误差,它使用每个终端节点的中值最小化 L1 损失,
“泊松”使用泊松偏差的减少来找到分割。

可选:

  • squared_error (默认)即v1.0 版本之前的 mse。
  • friedman_mse
  • absolute_error(默认)即v1.0 版本之前的 mae。
  • poisson
以 squared_error 为例

一下图算法为例,(摘自《统计学习方法》第二版)。下图采用的即 squared_error。
scikit-learn 中决策树模型-参数说明、注解_第3张图片
scikit-learn 中决策树模型-参数说明、注解_第4张图片
scikit-learn 中决策树模型-参数说明、注解_第5张图片

splitter 特征划分点选择标准

可选:

  • “best” 最优 — 在特征的所有划分点中找出最优的划分点
  • “random” 随机 — 随机的在部分划分点中找局部最优的划分点。

默认的"best"适合样本量不大的时候,而如果样本数据量非常大,此时决策树构建推荐"random"

数据量非常大时,若选择best 可能导致数的构建时间极长。

max_depth 最大深度

max_depthint, default=None

  • 取值为 int ,默认为 None

The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.

即默认下,max_depth 取值为 None,决策树在建立子树的时候不会限制子树的深度,决策树的生成会一直递归到所分子节点只有一个输出,或者叶子的样本数少于 min_samples_split

一般来说,数据少或者特征少的时候可以不管这个值。如果模型样本量多,特征也多的情况下,推荐限制这个最大深度,具体的取值取决于数据的分布。常用的可以取值10-100之间。

max_features 划分时考虑的最大特征数

max_featuresint, float or {“auto”, “sqrt”, “log2”}, default=None

  • If int, then consider max_features features at each split.

  • If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split.

  • If “auto”, then max_features=sqrt(n_features).

  • If “sqrt”, then max_features=sqrt(n_features).

  • If “log2”, then max_features=log2(n_features).

  • If None, then max_features=n_features.

Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than max_features features.

可以使用很多种类型的值,默认是"None",意味着划分时考虑所有的特征数;如果是"log2"意味着划分时最多考虑 l o g 2 N log_2N log2N 个特征;如果是"sqrt"或者"auto"意味着划分时最多考虑√N个特征。如果是整数,代表考虑的特征绝对数。如果是浮点数,代表考虑特征百分比,即考虑(百分比xN)取整后的特征数。其中N为样本总特征数。

一般来说,如果样本特征数不多,比如小于50,我们用默认的"None"就可以了,如果特征数非常多,我们可以灵活使用刚才描述的其他取值来控制划分时考虑的最大特征数,以控制决策树的生成时间

该参数的主要目的:控制决策树的生成时间。若特征数极大,模型在选择最优划分特征时,需要对于每一个特征进行判断 G i n i ( D , A ) Gini(D,A) Gini(D,A) 或者信息增益,计算量较大,决策树的生成时间可能非常大。

min_samples_split 叶子节点允许拆分的最小样本数

  • min_samples_splitint or float, default=2

这个值限制了子树继续划分的条件。

即若对于某内部节点按照选择的最优特征和划分值划分后得到的两个部分 R 1 , R 2 R_1,R_2 R1,R2,若其中某一部分的样本点数量低于 min_samples_split 则不再进行划分,将该部分作为叶子节点。

如果样本量不大,不需要管这个值。如果样本量数量级非常大,则推荐增大这个值。我之前的一个项目例子,有大概10万样本,建立决策树时,我选择了min_samples_split=10。可以作为参考。

min_samples_leafi 叶子节点的最小样本点数

  • min_samples_leafint or float, default=1 (默认取值为1)
  • If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are the minimum number of samples for each node.
  • 若为浮点数,则将其看做总样本数的比例,将其转化为 min_samples_leaf * n_samples 。

若通过内部节点划分的叶子节点,其样本数目小于该值 min_samples_leaf, 则会兄弟节点一起被剪枝。

默认是1,可以输入最少的样本数的整数,或者最少样本数占样本总数的百分比。

如果样本量不大,不需要管这个值。如果样本量数量级非常大,则推荐增大这个值。之前的10万样本项目使用min_samples_leaf的值为5,仅供参考。

该参数和上一个 min_samples_split 的区别在于:

  • min_samples_split 目的在于控制过度拆分叶子节点,若低于设定值则保留该节点作为叶子节点输出,不再选择特征进行进一步的拆分。
  • min_samples_leafi 目的在于剪枝,若经过父节点拆分后得到的新的叶子节点的样本数低于设定值,则将新得到的两个叶子节点剪枝掉,保留其父节点作为叶子节点输出。

min_weight_fraction_leaf min_weight_fraction_leaf

  • min_weight_fraction_leaffloat, default=0.0

The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided.
需要在叶节点处的权重总和(所有输入样本的)的最小加权分数。 当未提供 sample_weight 时,样本具有相同的权重。

默认为 0.0 , 即不考虑权重问题。

这个值限制了叶子节点所有样本权重和的最小值,如果小于这个值,则会和兄弟节点一起被剪枝。 默认是0,就是不考虑权重问题。一般来说,如果我们有较多样本有缺失值,或者分类树样本的分布类别偏差很大,就会引入样本权重,这时我们就要注意这个值了。

max_leaf_nodes 最大叶子节点数

  • max_leaf_nodesint, default=None (默认是"None”,即不限制最大的叶子节点数。)

通过限制最大的叶子节点数,来防止过拟合。(不加约束和剪枝的决策树有严重的过拟合问题)。

如果加了限制,算法会建立在最大叶子节点数内最优的决策树。如果特征不多,可以不考虑这个值,但是如果特征分成多的话,可以加以限制,具体的值可以通过交叉验证得到。

class_weight 类别权重 (不适用与回归树,只适用于分类树)

  • class_weightdict, list of dict or “balanced”, default=None

接收一个 dict 、list of dict , 或者直接采用自带的 “balanced”, 默认为 None;

指定样本各类别的的权重,主要是为了防止训练集某些类别的样本过多,导致训练的决策树过于偏向这些类别。这里可以自己指定各个样本的权重,或者用“balanced”,如果使用“balanced”,则算法会自己计算权重,样本量少的类别所对应的样本权重会高。当然,如果你的样本类别分布没有明显的偏倚,则可以不管这个参数,选择默认的"None"

Weights associated with classes in the form {class_label: weight}. If None, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y.

Note that for multioutput (including multilabel) weights should be defined for each class of every column in its own dict. For example, for four-class multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}].

The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n_samples / (n_classes * np.bincount(y))
For multi-output, the weights of each column of y will be multiplied.

Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified.

该参数还涉及到多标签输出的设定,这部分我还没有明白,先mark住。

min_impurity_decrease 节点划分最小不纯度

  • min_impurity_decreasefloat, default=0.0

A node will be split if this split induces a decrease of the impurity greater than or equal to this value.
如果该分裂导致杂质减少大于或等于该值,则该节点将被分裂。

The weighted impurity decrease equation is the following:

N_t / N × \times × (impurity - N_t_R / N_t × \times × right_impurity - N_t_L / N_t × \times × left_impurity)
N t N × ( i m p u r i t y − N t R N t × r i g h t i m p u r i t y − N t L N t × l e f t i m p u r i t y ) \frac{N_t}{N}\times(impurity - \frac{N_tR}{N_t}\times right_impurity -\frac{N_tL}{N_t}\times left_impurity) NNt×(impurityNtNtR×rightimpurityNtNtL×leftimpurity)

where N is the total number of samples, N_t is the number of samples at the current node, N_t_L is the number of samples in the left child, and N_t_R is the number of samples in the right child.
其中N是样本总数,N_t是当前节点的样本数,N_t_L是左孩子的样本数,N_t_R是右孩子的样本数。

N, N_t, N_t_R and N_t_L all refer to the weighted sum, if sample_weight is passed.
N, N_t, N_t_R and N_t_L 指加权和。
New in version 0.19.

只有划分后,导致 impurity 的减少,超过 min_impurity_decrease 的时候,该节点才会作为父节点生长新的叶子结点。

这个值限制了决策树的增长,如果某节点的不纯度(基尼系数,信息增益,均方差,绝对差)小于这个阈值,则该节点不再生成子节点。即为叶子节点 。

ccp_alpha 用于最小成本复杂度修剪的复杂度参数

  • ccp_alphanon-negative float, default=0.0

Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen. By default, no pruning is performed. See Minimal Cost-Complexity Pruning for details.
New in version 0.22.

用于最小成本复杂度修剪的复杂度参数。 将选择具有最大成本复杂度且小于 ccp_alpha 的子树。 默认情况下,不进行剪枝。

你可能感兴趣的:(机器学习,sklearn,决策树,scikit-learn,sklearn)