機器學習中有沒有可以分析特徵重要性的python工具?
現在在做一個機器學習的特徵選擇,有沒有什麼工具可以分析各項特徵的重要性以剔除分類中沒用的特徵
哈哈,請參考我前幾天剛寫的日誌:
Young:kaggle | Machine Learning for Insights Challenge?zhuanlan.zhihu.com我這個日誌的主要內容從kaggle的一個教學帖子學來的,這裡分析特徵重要性有關的三個python工具庫:eli5, pdpbox, shap
這幾個工具可以方便的表達出:Permuation Importance,Partial Dependence Plots,SHAP Values,Summary Plots
1.樹模型,建立好模型以後,看那個屬性里根節點越近則越重要。模型會自帶一些輸出重要屬性的方法。
2.線性模型,模型一般可以直接輸出參數,參數越大越重要。
3.利用互信息等類似的方法可以做,sklearn有,scipy裡面也有。
4. l1正則,可以觀察訓練完畢後參數為零的特徵,刪除掉就可以。模型自帶。
5. sklearn有一個feature_select模塊可以做特徵選擇。sklearn庫裡面其實基本的都可以搞定。
6.xgb,lightgbm 這些開源庫也可以做。
- 線性回歸模型中的ANOVA方法。
- 來自sklearn里的 RandomForestClassifier,自帶 feature_importances_ 功能。返回特徵向量的重要性。
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_blobsfrom sklearn.ensemble import RandomForestClassifierclf = RandomForestClassifier(n_estimators=10, max_depth=None,min_samples_split=2, random_state=0)
clf.fit(x_train, Y_train)
importances = clf.feature_importances_
std = np.std([tree.feature_importances_ for tree in clf.estimators_], axis=0)indices = np.argsort(importances)[::-1]# Print the feature ranking
print("Feature ranking:")for f in range(min(20,x_train.shape[1])):
print("%d. feature %d %s (%f)" % (f + 1, indices[f], importances[indices[f]]))# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(x_train.shape[1]), importances[indices],
color="r", yerr=std[indices], align="center")
plt.xticks(range(x_train.shape[1]), indices)
plt.xlim([-1, x_train.shape[1]])
plt.show()
#%% =====
Out:=