feature_importances_をちゃんと理解する
feature_importances_
とは sklearn.ensemble.RandomForestClassifier
、 sklearn.ensemble.RandomForestRegressor
(など)で特徴量の重要度を出力するメソッドです。
- とても便利で、EDAやモデルの精度向上のためのアイディアを得るためによく使用しますが、「この重要度って何を表しているの?」と聞かれたときにパッと説明できなかったので調べてみました。
- ちなみにドキュメントには↓の1行だけ説明があります。
The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance.
開発者が回答しているStackOverflow
もう少し細かい説明としてはscikit-learnの開発者 Gilles Louppe がstackoverflowで↓のように質問に回答しています。
stackoverflow.com
There are indeed several ways to get feature "importances". As often, there is no strict consensus about what this word means.
In scikit-learn, we implement the importance as described in [1] (often cited, but unfortunately rarely read...). It is sometimes called "gini importance" or "mean decrease impurity" and is defined as the total decrease in node impurity (weighted by the probability of reaching that node (which is approximated by the proportion of samples reaching that node)) averaged over all trees of the ensemble.
記載されている通り、gini importance
あるいは mean decrease impurity
と呼ばれ、ノードの不純度(impurity)をアンサンブル木で平均したものになります。
これが簡潔で正しい回答なのですが、一応コードベースでも見てみたいと思います。
続きを読む