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

Mat*_*son 3 python pipeline regression scikit-learn tpot

我已经通读了几页,但需要有人帮助解释如何进行这项工作。

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

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_
Run Code Online (Sandbox Code Playgroud)

我在 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_'
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

更新

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

mak*_*kis 6

非常好的问题。

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

best_model.fit(X_train, Y_train)
exctracted_best_model = best_model.fitted_pipeline_.steps[-1][1]
Run Code Online (Sandbox Code Playgroud)

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

然后您可以使用:

exctracted_best_model.fit(X_train, Y_train) 
Run Code Online (Sandbox Code Playgroud)

训练它。如果最佳模型具有所需的属性,那么您将能够在之后访问它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()
Run Code Online (Sandbox Code Playgroud)

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

输出:

在此处输入图片说明