【发布时间】:2021-02-02 18:57:36
【问题描述】:
我有一个使用 sklearn.ensemble.RandomForestClassifier 的拟合模型 (clf)。我已经知道我可以通过clf.feature_importances_ 获得特征重要性。无论如何,如果可能的话,我想知道如何通过每个样本获取特征重要性。
例子:
from sklearn.ensemble import RandomForestClassifier
X = {"f1":[0,1,1,0,1], "f2":[1,1,1,0,1]}
y = [0,1,0,1,0]
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)
y_pred = clf.predict(X)
那么,我该如何得到这样的东西:
y_pred f1_importance f2_importance
1 0.57 0.43
1 0.26 0.74
1 0.31 0.69
0 0.62 0.38
1 0.16 0.84
* y_pred 值不是真实的。我实际上在 Python 3.8 中将pandas 用于实际项目。
【问题讨论】:
标签: python python-3.x scikit-learn random-forest