编程语言
首页 > 编程语言> > python – 对scikit学习决策树中的random_state感到困惑

python – 对scikit学习决策树中的random_state感到困惑

作者:互联网

对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 documentation中解释

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参数允许控制这些随机选择.

interface documentation特别指出:

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(或任何其他值)调用它,那么每次都会得到相同的结果结果.

标签:decision-tree,python,scikit-learn,python-2-7,machine-learning
来源: https://codeday.me/bug/20191004/1853498.html