【问题标题】:Getting feature_importances_ after getting optimal TPOT pipeline?在获得最佳 TPOT 管道后获得 feature_importances_?
【发布时间】:2019-12-13 15:30:36
【问题描述】:

我已经阅读了几页,但需要有人帮助解释如何使它工作。

我正在使用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_

我在 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 回归器?

【问题讨论】:

    标签: python scikit-learn regression pipeline tpot


    【解决方案1】:

    非常好的问题。

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

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

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

    然后你可以使用:

    exctracted_best_model.fit(X_train, Y_train) 
    

    训练它。如果最好的模型具有所需的属性,那么您将能够在exctracted_best_model.fit(X_train, Y_train)之后访问它


    更多细节(在我的 cmets 中)和一个玩具示例:

    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_ 属性。

    输出:

    【讨论】:

    • 感谢您的帮助,但特征重要性的含义是确定数据集的每个特征 (X)(不是管道参数)在确定预测 (Y) 标签中的重要性使用条形图。 TPOT 不直接这样做,所以我想我会抓住它提出的管道,在训练数据上重新运行它,然后以某种方式使用 .feature_imprtances_ 来绘制结果?跨度>
    • 我已经解决了你所有的问题。请参阅我更新的答案,请投票并接受。欢呼
    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多