是否可以从 h2o.automl 模型中获取特征重要性图?

Is it possible to get a feature importance plot from a h2o.automl model?

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

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

指向某些 python 3 代码的指针将不胜感激。

谢谢。 查尔斯

这取决于您使用的型号。如果您使用 AutoML Leaderboard 上的顶级模型,那可能是 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

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