title: sklearn random forest - view features subset by individual decision trees in forest
body: <p>I'm using the scikit-learn RandomForestRegressor and I am setting an explicit value for max_features. When I do that, I expect that, for each tree in the forest, there is a random subset of features of length max_features that is considered when build the tree (is this correct or is does it perform the feature subsampling from the entire feature set for each node of each tree?). I'm trying to find out which subset of features was used by each tree.</p>
  
  <p>Here's an example of where I am now:</p>
  
  <pre><code># initialize random forest with 10 trees of depth 2 (max 3 features), 
  # with 10 randomly subset features selected per tree
  rf = RandomForestRegressor(n_estimators=10, max_depth=2, max_features=10) 
  forest = rf.fit(X,y) # fit the model
  
  # get a list of individual DecisionTreeRegressor objects
  trees = forest.estimators_ 
  </code></pre>
  
  <p>If I wanted to find out which features were <strong>used</strong> in the first tree, I think I can do the following:</p>
  
  <pre><code>[j for j,v in enumerate(trees[0].feature_importances_) if v &gt; 0]
  </code></pre>
  
  <p>which gives me the position of the columns containing features with a non-zero feature importance. But, that only gives me the (up to) 3 features that were chosen by the decision tree out of the 10 subsampled features. As far as I can see, the feature_importances_ attribute does not distinguish between features that were used but not chosen vs not used at all.</p>
  
  <p>I've been looking through the source code but can't seem to find where the subsampling of the full feature set occurs in the <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/tree.py" rel="nofollow noreferrer">DecisionTreeRegressor</a> definition or the <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx" rel="nofollow noreferrer">_tree</a> definition.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As I've been reading the source code, I believe that it's the splitter class that does the sampling of the features for each node split, not per tree, and stores them in the splitter.features attribute. Unfortunately, I can't find a way to access that attribute.</p>