jupyter-notebook - 管道'对象没有属性'feature_importances_
问题描述
我的代码有问题,我想从 word2vec 模型中查看向量的特征重要性,但我不能,因为它是一个管道。有人可以帮我找到解决方案吗?
## Import the random forest model.
from sklearn.ensemble import RandomForestClassifier
## This line instantiates the model.
rf = Pipeline([
("word2vec vectorizer", MeanEmbeddingVectorizer(w2v)),
("Random_forest", RandomForestClassifier(n_estimators=100, max_depth=6,random_state=0))])
## Fit the model on your training data.
rf.fit(X_train, y_train)
## And score it on your testing data.
rf.score(X_test, y_test)
X = model.wv.syn0
X = X.astype(int)
def plot_feat_imp(model, X):
Feature_Imp = pd.DataFrame([X, rand_w2v_tfidf.feature_importances_]).transpose(
).sort_values(1, ascending=False)
plt.figure(figsize=(14, 7))
sns.barplot(y=Feature_Imp.loc[:, 0], x=Feature_Imp.loc[:, 1], data=Feature_Imp, orient='h')
plt.title("Importance des variables (qu'est ce qui explique le mieux la satisfaction)", fontsize=21)
plt.show()
return
MY PROBLEM IS HERE
AttributeError: 'Pipeline' object has no attribute 'feature_importances_'
plot_feat_imp(gbc_w2v, X)
解决方案
也许不是您正在寻找的答案,但如果您想要管道对象的 feature_importances_,您可能希望首先进入最佳分类器。
这可以通过:
rf_fit = rf.fit(X_train, y_train)
feature_importances = rf_fit.best_estimator_._final_estimator.feature_importances_
希望有帮助。
推荐阅读
- html - 谁能告诉我如何在 Python 上提取和显示图像中的文本
- php - 如何使用 laravel 查询生成器检索 12 个月的月度摘要?
- android - Android:在一个视图中保留多个 snakbar
- python - 如何修复 Tkinter Python 中的“TCL 错误”?在 Jupyter 笔记本上工作
- xcode - 在 Xcode 中创建新文件时出现奇怪的相对路径。它与 macOS Catalina beta 4 版本有关吗?
- javascript - JQuery Datatable 仅适用于空(无数据)表
- dialogflow-es - 帐户关联返回错误
- amazon-web-services - AWS 无服务器图像处理程序 - Lambda 错误
- java - 空闲固定线程池的缺点
- kubernetes - K8s 服务作为 DaemonSet