title: How Feature Importance is calculated in sklearn's RandomForest?
body: <p>From this <a href="https://towardsdatascience.com/visualizing-decision-trees-with-python-scikit-learn-graphviz-matplotlib-1c50b4aa68dc" rel="nofollow noreferrer">Tutorial</a> and <a href="https://datascience.stackexchange.com/questions/66280/how-is-the-feature-importance-value-calculated-in-sklearn-random-forest-regre">Feature Importance</a><br>
  I try to make my own random forest tree</p>
  <pre><code>data = load_breast_cancer()
  df = pd.DataFrame(data.data, columns=data.feature_names)
  df['target'] = data.target
  
  
  X = df.loc[:, df.columns != 'target']
  y = df.loc[:, 'target'].values
  
  
  X_train, X_test, Y_train, Y_test = train_test_split(X, y, random_state=0)
  
  
  rf = RandomForestClassifier(n_estimators=1,
                              max_depth=2,
                              max_features=2,
                              random_state=0)
  rf.fit(X_train, Y_train)
  </code></pre>
  <pre><code>rf.feature_importances_
  array([0.        , 0.11197953, 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.88802047, 0.        , 0.        , 0.        ])
  </code></pre>
  <pre><code>fn=data.feature_names
  cn=data.target_names
  fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=800)
  tree.plot_tree(rf.estimators_[0],
                 feature_names = fn, 
                 class_names=cn,
                 filled = True);
  fig.savefig('rf_individualtree.png')
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Lmzf0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lmzf0.png" alt="A single random forest tree" /></a>
  calculate the <a href="https://datascience.stackexchange.com/questions/66280/how-is-the-feature-importance-value-calculated-in-sklearn-random-forest-regre">Feature Importance</a> by hand from above <a href="https://datascience.stackexchange.com/questions/66280/how-is-the-feature-importance-value-calculated-in-sklearn-random-forest-regre">Feature Importance</a> (result from sklearn 0.11197953, 0.88802047)</p>
  <pre><code>a = (192/265)*(0.262-(68/192)*0.452-(124/192)*0.103) 
  b = (265/265)*(0.459-(192/265)*0.262-(73/265)*0.185)+(73/265)*(0.185-(72/73)*0.173)
  
  print(b/(a+b))
  print(a/(a+b))
  </code></pre>
  <pre><code>0.8625754868011606
  0.13742451319883947
  </code></pre>
  <p>Which part I did wrong my result is different from sklearn answer or sklearn just don't follow the formula?</p>