以下代码使用随机森林模型为我提供一个显示特征重要性的图表:
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')
I get
AttributeError: 'LogisticRegression' object has no attribute 'feature_importances_'
有人可以帮助我哪里出错了吗?