【问题标题】:Is it possible to get a feature importance plot from a h2o.automl model?是否可以从 h2o.automl 模型中获得特征重要性图?
【发布时间】:2019-01-09 10:14:06
【问题描述】:

我有一个二元分类问题,我正在使用“h2o.automl”来获取模型。

是否可以从“h2o.automl”模型中获取我的数据集特征的重要性图?

非常感谢您提供指向某些 python 3 代码的指针。

谢谢。 查尔斯

【问题讨论】:

    标签: python-3.x h2o automl


    【解决方案1】:

    这取决于您使用的型号。如果您使用 AutoML 排行榜上的顶级模型,那可能是 Stacked Ensemble,我们还没有为该类型模型提取特征重要性的功能(尽管有一个 ticket open 来添加它)。

    如果您想使用任何其他类型的模型(例如 GBM),则可以使用常规方法从 H2O 模型中获取变量重要性。这是一个使用来自H2O AutoML User Guide 的示例代码的演示。

    import h2o
    from h2o.automl import H2OAutoML
    
    h2o.init()
    
    # Import a sample binary outcome training set into H2O
    train = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_train_10k.csv")
    
    # Identify predictors and response
    x = train.columns
    y = "response"
    x.remove(y)
    
    # For binary classification, response should be a factor
    train[y] = train[y].asfactor()
    
    # Run AutoML for 10 models
    aml = H2OAutoML(max_models=10, seed=1)
    aml.train(x=x, y=y, training_frame=train)
    
    # View the AutoML Leaderboard
    lb = aml.leaderboard
    lb
    

    前两个模型是 Stacked Ensembles,但第三个是 GBM,因此我们可以从该模型中提取变量重要性。

    In [6]: lb[:5,"model_id"]
    
    Out[6]:
    model_id
    -----------------------------------------------------
    StackedEnsemble_AllModels_0_AutoML_20180801_120024
    StackedEnsemble_BestOfFamily_0_AutoML_20180801_120024
    GBM_grid_0_AutoML_20180801_120024_model_4
    GBM_grid_0_AutoML_20180801_120024_model_0
    GBM_grid_0_AutoML_20180801_120024_model_1
    
    [5 rows x 1 column]
    

    这是获取变量重要性的方法。先抓取GBM模型对象:

    # Get third model
    m = h2o.get_model(lb[2,"model_id"])
    

    然后您可以在 Pandas DataFrame 中取回数据(如果您安装了 pandas),如下所示:

    In [13]: m.varimp(use_pandas=True)
    Out[13]:
       variable  relative_importance  scaled_importance  percentage
    0       x26           997.396362           1.000000    0.224285
    1       x28           437.546936           0.438689    0.098391
    2       x27           338.475555           0.339359    0.076113
    3        x6           306.173553           0.306973    0.068849
    4       x25           295.848785           0.296621    0.066528
    5       x23           284.468292           0.285211    0.063968
    6        x1           191.988358           0.192490    0.043172
    7        x4           184.072052           0.184553    0.041392
    8       x10           137.810501           0.138170    0.030989
    9       x14           100.928482           0.101192    0.022696
    10      x12            90.265976           0.090502    0.020298
    11      x22            89.900856           0.090136    0.020216
    12      x20            87.367523           0.087596    0.019646
    13      x19            83.130775           0.083348    0.018694
    14       x5            82.661133           0.082877    0.018588
    15      x16            81.957863           0.082172    0.018430
    16      x18            80.794426           0.081005    0.018168
    17       x7            80.664566           0.080875    0.018139
    18      x11            75.841171           0.076039    0.017054
    19       x2            75.037476           0.075233    0.016874
    20       x8            72.234459           0.072423    0.016243
    21      x15            70.233994           0.070417    0.015794
    22       x3            60.015785           0.060172    0.013496
    23       x9            40.281757           0.040387    0.009058
    24      x13            35.475540           0.035568    0.007977
    25      x17            25.367661           0.025434    0.005704
    26      x24            22.506416           0.022565    0.005061
    27      x21            18.564632           0.018613    0.004175
    

    如果您安装了 ma​​tplotlib,您还可以使用 m.varimp_plot() 绘制变量重要性。

    【讨论】:

    • 谢谢艾琳。很不错。我期待从 Stacked Ensemble 模型中获得特征重要性
    • 我需要更新我上面的答案,但请注意,您现在可以使用置换变量重要性方法(自我的原始答案以来的新方法)获得 Stacked Ensemble 模型的变量重要性:docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/…
    猜你喜欢
    • 2022-08-05
    • 2021-04-15
    • 2019-07-22
    • 2018-06-30
    • 2020-03-17
    • 2021-04-08
    • 2021-05-22
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多