【发布时间】:2018-12-18 03:46:35
【问题描述】:
我正在使用 h2o DRF 和 GBM 构建分类模型。我想改变预测的概率,如果 p0
【问题讨论】:
标签: python-3.x random-forest h2o gbm
我正在使用 h2o DRF 和 GBM 构建分类模型。我想改变预测的概率,如果 p0
【问题讨论】:
标签: python-3.x random-forest h2o gbm
目前,您需要手动执行此操作。如果我们为predict() 方法添加一个threshold 参数会更容易,因此我创建了一个JIRA ticket 票证以使其更直接。
请参阅下面的 Python 示例,了解如何在下面手动执行此操作。
import h2o
from h2o.estimators.gbm import H2OGradientBoostingEstimator
h2o.init()
# Import a sample binary outcome train/test set into H2O
train = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_train_10k.csv")
test = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_test_5k.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()
test[y] = test[y].asfactor()
# Train and cross-validate a GBM
my_gbm = H2OGradientBoostingEstimator(distribution="bernoulli", seed=1)
my_gbm.train(x=x, y=y, training_frame=train)
# Predict on a test set using default threshold
pred = my_gbm.predict(test_data=test)
查看pred 框架:
In [16]: pred.tail()
Out[16]:
predict p0 p1
--------- -------- --------
1 0.484712 0.515288
0 0.693893 0.306107
1 0.319674 0.680326
0 0.582344 0.417656
1 0.471658 0.528342
1 0.079922 0.920078
1 0.150146 0.849854
0 0.835288 0.164712
0 0.639877 0.360123
1 0.54377 0.45623
[10 rows x 3 columns]
以下是手动创建所需预测的方法。 H2O User Guide 中提供了有关如何切片 H2OFrames 的更多信息。
# Binary column which is 1 if >=0.2 and 0 if <0.2
newpred = pred["p1"] >= 0.2
newpred.tail()
看二进制列:
In [23]: newpred.tail()
Out[23]:
p1
----
1
1
1
1
1
1
1
0
1
1
[10 rows x 1 column]
现在你有了你想要的预测。您还可以将 "predict" 列替换为新的预测标签。
pred["predict"] = newpred
现在重新检查pred 框架:
In [24]: pred.tail()
Out[24]:
predict p0 p1
--------- -------- --------
1 0.484712 0.515288
1 0.693893 0.306107
1 0.319674 0.680326
1 0.582344 0.417656
1 0.471658 0.528342
1 0.079922 0.920078
1 0.150146 0.849854
0 0.835288 0.164712
1 0.639877 0.360123
1 0.54377 0.45623
[10 rows x 3 columns]
【讨论】:
my_gbm.predict(test_data=train)重新生成它们。