ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-04 16:59:24  阅读:224  来源: 互联网

标签:decision-tree python scikit-learn python-2-7 machine-learning


对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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有