H2O 的混淆矩阵

Confusion Matrix on H2O

最终编辑:这个问题最终发生是因为目标数组是应该代表类别的整数,所以它正在进行回归。一旦我使用 .asfactor() 将它们转换为因数,下面答案中详述的混淆矩阵方法就起作用了


我正在尝试 运行 我的随机森林模型 (my_model) 上的混淆矩阵,但文档没有帮助。从 here 它说命令是 h2o.confusionMatrix(my_model) 但 3.0 中没有这样的东西。

以下是拟合模型的步骤:

from h2o.estimators.random_forest import H2ORandomForestEstimator

data_h = h2o.H2OFrame(data)
train, valid = data_h.split_frame(ratios=[.7], seed = 1234)

my_model = H2ORandomForestEstimator(model_id = "rf_h", ntrees = 400, 
max_depth = 30, nfolds = 8, seed = 25)
my_model.train(x = features, y = target, training_frame=train)
pred = rf_h.predict(valid)

我试过以下方法:

my_model.confusion_matrix()

AttributeError: type object 'H2ORandomForestEstimator' has no attribute 
'confusion_matrix'

来自 this example.

我尝试使用制表符补全来找出它可能是什么并尝试过:

h2o.model.confusion_matrix(my_model)

TypeError: 'module' object is not callable

h2o.model.ConfusionMatrix(my_model)

它只输出所有模型诊断,然后输出错误:

H2OTypeError: Argument `cm` should be a list, got H2ORandomForestEstimator 

最后,

h2o.model.ConfusionMatrix(pred)

给出与上面相同的错误。

这里不知道怎么办,如何查看模型混淆矩阵的结果?

编辑:在 Context

问题的开头添加了更多代码

请参阅 documentation 以获得完整的参数列表。为了您的方便,这里是列表 confusion_matrix(metrics=None, thresholds=None, train=False, valid=False, xval=False).

这是一个如何使用该方法的工作示例:

import h2o
from h2o.estimators.random_forest import H2ORandomForestEstimator
h2o.init()


# import the cars dataset:
# this dataset is used to classify whether or not a car is economical based on
# the car's displacement, power, weight, and acceleration, and the year it was made
cars = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/junit/cars_20mpg.csv")

# convert response column to a factor
cars["economy_20mpg"] = cars["economy_20mpg"].asfactor()

# set the predictor names and the response column name
predictors = ["displacement","power","weight","acceleration","year"]
response = "economy_20mpg"

# split into train and validation sets
train, valid = cars.split_frame(ratios = [.8], seed = 1234)

# try using the binomial_double_trees (boolean parameter):
# Initialize and train a DRF
cars_drf = H2ORandomForestEstimator(binomial_double_trees = False, seed = 1234)
cars_drf.train(x = predictors, y = response, training_frame = train, validation_frame = valid)
cars_drf.confusion_matrix()
# or specify the validation frame
cars_drf.confusion_matrix(valid=True)