管道"对象没有属性"feature_importances_



我的代码有问题,我想从Word2Vec模型中看到功能的重要性,但是我不能beakause这是一个管道。有人可以帮助我找到解决方案吗?

## 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_

希望会有所帮助。

最新更新