H2O的混淆矩阵

时间:2018-08-05 18:50:57

标签: python random-forest h2o confusion-matrix

最终编辑:该问题最终发生,因为目标数组是应该表示类别的整数,因此正在进行回归。一旦我使用.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)

给出与上面相同的错误。

不确定在这里做什么,如何查看模型混淆矩阵的结果?

编辑:在上下文问题的开头添加了更多代码

1 个答案:

答案 0 :(得分:3)

请参阅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)