title: sklearn.tree.DecisionTreeClassifier: Get all samples that fell into leaf node
body: <p>I want to evaluate for all samples the size of the leaf node they fell into.</p>
  
  <p>Based on <a href="https://stackoverflow.com/a/39772170/1701600">this excellent answer</a>, I already figured out a way to extract the number of samples for each leaf node:</p>
  
  <pre><code>from sklearn.tree import _tree, DecisionTreeClassifier
  import numpy as np
  
  clf = DecisionTreeClassifier().fit(X_train, y_train)
  
  def tree_get_leaf_size_for_elem(tree, feature_names):
  
      tree_ = tree.tree_
  
      def recurse(node):
          if tree_.feature[node] != _tree.TREE_UNDEFINED:
              recurse(tree_.children_left[node])
          else:
              samples_in_leaf = np.sum(tree_.value[node][0])
  
      recurse(0)
  
  tree_get_leaf_size_for_elem(clf, feature_names)
  </code></pre>
  
  <p>Is there a way to get the indices of all samples (<code>X_train</code>) that ended up in a leaf node? A new column for <code>X_train</code> called "leaf_node_size" would be the desired output.</p>