对 scikit 学习决策树中的 random_state 感到困惑
confused about random_state in decision tree of scikit learn
对 random_state
参数感到困惑,不确定为什么决策树训练需要一些随机性。我的想法,(1)它与随机森林有关吗? (2) 是否与拆分训练测试数据集有关?如果是这样,为什么不直接使用训练测试拆分方法(http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html)?
http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html
>>> from sklearn.datasets import load_iris
>>> from sklearn.cross_validation import cross_val_score
>>> from sklearn.tree import DecisionTreeClassifier
>>> clf = DecisionTreeClassifier(random_state=0)
>>> iris = load_iris()
>>> cross_val_score(clf, iris.data, iris.target, cv=10)
...
...
array([ 1. , 0.93..., 0.86..., 0.93..., 0.93...,
0.93..., 0.93..., 1. , 0.93..., 1. ])
此致,
林
中有解释
The problem of learning an optimal decision tree is known to be NP-complete under several aspects of optimality and even for simple concepts. Consequently, practical decision-tree learning algorithms are based on heuristic algorithms such as the greedy algorithm where locally optimal decisions are made at each node. Such algorithms cannot guarantee to return the globally optimal decision tree. This can be mitigated by training multiple trees in an ensemble learner, where the features and samples are randomly sampled with replacement.
因此,基本上,使用随机选择的特征和样本(与随机森林中使用的类似技术)重复多次次优贪婪算法。 random_state
参数允许控制这些随机选择。
If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
所以,无论如何都会使用随机算法。传递任何值(无论是特定的 int,例如 0,还是 RandomState
实例),都不会改变它。传递 int 值(0 或其他)的唯一理由是使结果在调用之间保持一致:如果您使用 random_state=0
(或任何其他值)调用它,那么每次都会得到同样的结果。
许多机器学习模型在模型训练中允许一些随机性。为 random_state 指定一个数字可确保您在每个 运行 中获得相同的结果。这被认为是一种很好的做法。您使用任何数字,模型质量不会有意义地取决于您选择的值。
决策树使用启发式过程。决策树不保证全局相同的解决方案。每次构建模型时,树结构都会发生变化。将特定种子传递给 random_state 可确保每次构建模型时都生成相同的结果。
上面引用的文档部分具有误导性,根本问题不是算法的贪婪性。 CART 算法是确定性的(参见 here)并找到加权 Gini 指数的全局最小值。
决策树的重复运行可能会产生不同的结果,因为有时可以使用不同的特征拆分数据并仍然获得相同的基尼指数。这在这里描述:
https://github.com/scikit-learn/scikit-learn/issues/8443
设置随机状态只是确保 CART 实现在寻找最小值时通过相同的随机特征列表工作。
对 random_state
参数感到困惑,不确定为什么决策树训练需要一些随机性。我的想法,(1)它与随机森林有关吗? (2) 是否与拆分训练测试数据集有关?如果是这样,为什么不直接使用训练测试拆分方法(http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html)?
http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html
>>> from sklearn.datasets import load_iris
>>> from sklearn.cross_validation import cross_val_score
>>> from sklearn.tree import DecisionTreeClassifier
>>> clf = DecisionTreeClassifier(random_state=0)
>>> iris = load_iris()
>>> cross_val_score(clf, iris.data, iris.target, cv=10)
...
...
array([ 1. , 0.93..., 0.86..., 0.93..., 0.93...,
0.93..., 0.93..., 1. , 0.93..., 1. ])
此致, 林
The problem of learning an optimal decision tree is known to be NP-complete under several aspects of optimality and even for simple concepts. Consequently, practical decision-tree learning algorithms are based on heuristic algorithms such as the greedy algorithm where locally optimal decisions are made at each node. Such algorithms cannot guarantee to return the globally optimal decision tree. This can be mitigated by training multiple trees in an ensemble learner, where the features and samples are randomly sampled with replacement.
因此,基本上,使用随机选择的特征和样本(与随机森林中使用的类似技术)重复多次次优贪婪算法。 random_state
参数允许控制这些随机选择。
If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
所以,无论如何都会使用随机算法。传递任何值(无论是特定的 int,例如 0,还是 RandomState
实例),都不会改变它。传递 int 值(0 或其他)的唯一理由是使结果在调用之间保持一致:如果您使用 random_state=0
(或任何其他值)调用它,那么每次都会得到同样的结果。
许多机器学习模型在模型训练中允许一些随机性。为 random_state 指定一个数字可确保您在每个 运行 中获得相同的结果。这被认为是一种很好的做法。您使用任何数字,模型质量不会有意义地取决于您选择的值。
决策树使用启发式过程。决策树不保证全局相同的解决方案。每次构建模型时,树结构都会发生变化。将特定种子传递给 random_state 可确保每次构建模型时都生成相同的结果。
上面引用的文档部分具有误导性,根本问题不是算法的贪婪性。 CART 算法是确定性的(参见 here)并找到加权 Gini 指数的全局最小值。
决策树的重复运行可能会产生不同的结果,因为有时可以使用不同的特征拆分数据并仍然获得相同的基尼指数。这在这里描述: https://github.com/scikit-learn/scikit-learn/issues/8443
设置随机状态只是确保 CART 实现在寻找最小值时通过相同的随机特征列表工作。