H2O和Scikit-Learn指标评分之间有什么区别吗?

3

我尝试使用H2O创建一些二元分类问题的机器学习模型,测试结果非常好。但是当我检查时发现了一些奇怪的事情。出于好奇,我尝试打印模型对测试集的预测结果。结果发现我的模型实际上总是预测0(负面),但AUC约为0.65,精度不为0.0。然后我尝试使用Scikit-learn来比较指标分数,结果(如预期)不同。Scikit-learn得出了0.0的精度和0.5的AUC分数,我认为这是正确的。以下是我使用的代码:

model = h2o.load_model(model_path)
predictions = model.predict(Test_data).as_data_frame()

# H2O version to print the AUC score
auc = model.model_performance(Test_data).auc()

# Python version to print the AUC score
auc_sklearn = sklearn.metrics.roc_auc_score(y_true, predictions['predict'].tolist())

有什么想法吗?提前感谢!

只是猜测而不是给出完整的答案,Scikit Learn 可能会针对有偏差的类大小进行归一化处理。 - Hans Musgrave
1个回答

5

H2O和scikit-learn的评分没有区别,你只需要了解如何理解输出结果,以便准确比较它们。

如果您查看predictions['predict']中的数据,您会发现这是一个预测类而不是原始预测值。AUC使用后者,因此您需要使用正确的列。请参见下面:

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
model = H2OGradientBoostingEstimator(distribution="bernoulli", seed=1)
model.train(x=x, y=y, training_frame=train)

# Test AUC
model.model_performance(test).auc()
# 0.7817203808052897

# Generate predictions on a test set
pred = model.predict(test)

请检查输出:
In [4]: pred.head()
Out[4]:
  predict        p0        p1
---------  --------  --------
        0  0.715077  0.284923
        0  0.778536  0.221464
        0  0.580118  0.419882
        1  0.316875  0.683125
        0  0.71118   0.28882
        1  0.342766  0.657234
        1  0.297636  0.702364
        0  0.594192  0.405808
        1  0.513834  0.486166
        0  0.70859   0.29141

[10 rows x 3 columns]

现在与sklearn进行比较:
from sklearn.metrics import roc_auc_score

pred_df = pred.as_data_frame()
y_true = test[y].as_data_frame()

roc_auc_score(y_true, pred_df['p1'].tolist())
# 0.78170751032654806

在这里,您可以看到它们大致相同。AUC是一种近似方法,因此当您比较不同的实现时,在几个小数位之后会看到差异。


嗨,Erin,你能否看一下我目前遇到的问题链接,即比较两个混淆矩阵结果时遇到的问题?先谢谢了。 - Anastasiya-Romanova 秀

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接