feature_importances_ - 從決策樹到gbdt

在用sklearn的時候經常用到feature_importances_ 來做特徵篩選,那這個屬性到底是啥呢。
在這裏插入圖片描述
分析gbdt的源碼發現來源於每個base_estimator的決策樹的
feature_importances_
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
由此發現計算邏輯來源於cython文件,這個文件可以在其github上查看源代碼
在這裏插入圖片描述
而在DecisionTreeRegressor和DecisionTreeClassifier的對feature_importances_定義中
在這裏插入圖片描述
到此決策樹的feature_importances_就很清楚了:impurity就是gini值,weighted_n_node_samples 就是各個節點的加權樣本數,最後除以根節點nodes[0].weighted_n_node_samples的總樣本數
下面以一個簡單的例子來驗證下:
在這裏插入圖片描述
在這裏插入圖片描述
上面是決策樹跑出來的結果,來看petal width (cm)就是根節點,
featureimportance=(1120.6647750.4956370)/112=0.5564007189feature_importance=(112*0.6647-75*0.4956-37*0)/112=0.5564007189,
petal length (cm)的
featureimportance=(750.4956390.05360.1528)/112=0.4435992811feature_importance=(75*0.4956-39*0.05-36*0.1528)/112=0.4435992811
忽略圖上gini計算的小數位數,計算結果相同。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章