【发布时间】:2021-06-19 08:56:48
【问题描述】:
以下代码使用随机森林模型为我提供了一个显示特征重要性的图表:
from sklearn.feature_selection import SelectFromModel
import matplotlib
clf = RandomForestClassifier()
clf = clf.fit(X_train,y_train)
clf.feature_importances_
model = SelectFromModel(clf, prefit=True)
test_X_new = model.transform(X_test)
matplotlib.rc('figure', figsize=[5,5])
plt.style.use('ggplot')
feat_importances = pd.Series(clf.feature_importances_, index=X_test.columns)
feat_importances.nlargest(20).plot(kind='barh',title = 'Feature Importance')
但是,我需要对逻辑回归模型执行相同的操作。以下代码产生错误:
from sklearn.feature_selection import SelectFromModel
import matplotlib
clf = LogisticRegression()
clf = clf.fit(X_train,y_train)
clf.feature_importances_
model = SelectFromModel(clf, prefit=True)
test_X_new = model.transform(X_test)
matplotlib.rc('figure', figsize=[5,5])
plt.style.use('ggplot')
feat_importances = pd.Series(clf.feature_importances_, index=X_test.columns)
feat_importances.nlargest(20).plot(kind='barh',title = 'Feature Importance')
我明白了
AttributeError: 'LogisticRegression' object has no attribute 'feature_importances_'
有人可以帮忙解决我哪里出错了吗?
【问题讨论】:
-
正如错误所说:
'LogisticRegression' object has no attribute 'feature_importances_'我建议你看看shap library,这可能是你在这种情况下要找的:)
标签: python scikit-learn classification