获得最佳 TPOT 管道后获得 feature_importances_?

Getting feature_importances_ after getting optimal TPOT pipeline?

我已经通读了几页,但需要有人帮助解释如何让它发挥作用。

我正在使用 TPOTRegressor() 来获得最佳管道,但从那里我希望能够绘制管道的 .feature_importances_ returns:

best_model = TPOTRegressor(cv=folds, generations=2, population_size=10, verbosity=2, random_state=seed) #memory='./PipelineCache',       memory='auto',
best_model.fit(X_train, Y_train)
feature_importance = best_model.fitted_pipeline_.steps[-1][1].feature_importances_

我在 Github 上的一个现已关闭的问题中看到了这种设置,但目前我收到错误:

Best pipeline: LassoLarsCV(input_matrix, normalize=True)

Traceback (most recent call last):
  File "main2.py", line 313, in <module>
    feature_importance = best_model.fitted_pipeline_.steps[-1][1].feature_importances_
AttributeError: 'LassoLarsCV' object has no attribute 'feature_importances_'

那么,无论它落在哪个管道上,我如何从最佳管道中获得这些特征重要性?或者这甚至可能吗?或者有人有更好的方法来尝试从 TPOT 运行 中绘制特征重要性吗?

谢谢!

更新

澄清一下,特征重要性的含义是确定数据集的每个特征 (X) 在确定预测 (Y) 标签时的重要性,使用条形图绘制每个特征在未来的重要性级别符合其预测。 TPOT 不会直接这样做(我不认为),所以我想我会抓住它提出的管道,在训练数据上重新 运行 它,然后以某种方式使用 .feature_imprtances_ 然后能够绘制特征重要性图,因为这些都是我正在使用的 sklearn 回归器?

非常好的问题。

您只需再次拟合最佳模型即可获得特征重要性。

best_model.fit(X_train, Y_train)
exctracted_best_model = best_model.fitted_pipeline_.steps[-1][1]

最后一行 returns 基于 CV 的最佳模型。

然后您可以使用:

exctracted_best_model.fit(X_train, Y_train) 

训练它。如果最佳模型具有所需的属性,那么您将可以在 exctracted_best_model.fit(X_train, Y_train)

后访问它

更多详细信息(在我的评论中)和玩具示例:

from tpot import TPOTRegressor
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

digits = load_digits()
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target,
                                                    train_size=0.75, test_size=0.25)
# reduce training features for time sake
X_train = X_train[:100,:] 
y_train = y_train[:100]

# Fit the TPOT pipeline
tpot = TPOTRegressor(cv=2, generations=5, population_size=50, verbosity=2)

# Fit the pipeline
tpot.fit(X_train, y_train)

# Get the best model
exctracted_best_model = tpot.fitted_pipeline_.steps[-1][1]

print(exctracted_best_model)
AdaBoostRegressor(base_estimator=None, learning_rate=0.5, loss='square',
         n_estimators=100, random_state=None)

# Train the `exctracted_best_model` using THE WHOLE DATASET.
# You need to use the whole dataset in order to get feature importance for all the
# features in your dataset.
exctracted_best_model.fit(X, y) # X,y IMPORTNANT

# Access it's features
exctracted_best_model.feature_importances_

# Plot them using barplot
# Here I fitted the model on X_train, y_train and not on the whole dataset for TIME SAKE
# So I got importances only for the features in `X_train`
# If you use `exctracted_best_model.fit(X, y)` we will have importances for all the features !!!
positions= range(exctracted_best_model.feature_importances_.shape[0])
plt.bar(positions, exctracted_best_model.feature_importances_)
plt.show()

重要提示: *在上面的示例中,基于管道的最佳模型是 AdaBoostRegressor(base_estimator=None, learning_rate=0.5, loss='square')。这个模型确实有属性feature_importances_。 在最佳模型没有属性 feature_importances_ 的情况下,完全相同的代码将不起作用。您将需要阅读文档并查看每个返回的最佳模型的属性。 例如。如果最佳模型是 LassoCV,那么您将使用 coef_ 属性。

输出: