confused about random_state in decision tree of scikit learn
<p>Confused about <code>random_state</code> parameter, not sure why decision tree training needs some randomness. My thoughts, (1) is it related to random forest? (2) is it related to split training testing data set? If so, why not use training testing split method directly (<a href="http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html" rel="nofollow">http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html</a>)?</p>

<p><a href="http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html" rel="nofollow">http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html</a></p>

<pre><code>&gt;&gt;&gt; from sklearn.datasets import load_iris
&gt;&gt;&gt; from sklearn.cross_validation import cross_val_score
&gt;&gt;&gt; from sklearn.tree import DecisionTreeClassifier
&gt;&gt;&gt; clf = DecisionTreeClassifier(random_state=0)
&gt;&gt;&gt; iris = load_iris()
&gt;&gt;&gt; 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.      ])
</code></pre>

<p>regards,
Lin</p>

<p>This is explained in <a href="http://scikit-learn.org/stable/modules/tree.html#tree" rel="nofollow">the documentation</a></p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>So, basically, a sub-optimal greedy algorithm is repeated a number of times using random selections of features and samples (a similar technique used in random forests). The <code>random_state</code> parameter allows controlling these random choices.</p>

<p>The <a href="http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html" rel="nofollow">interface documentation</a> specifically states:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>So, the random algorithm will be used in any case. Passing any value (whether a specific int, e.g., 0, or a <code>RandomState</code> instance), will not change that. The only rationale for passing in an int value (0 or otherwise) is to make the outcome consistent across calls: if you call this with <code>random_state=0</code> (or any other value), then each and every time, you'll get the same result.</p>

python python-2.7 machine-learning scikit-learn decision-tree