r - 在这种情况下如何制作混淆矩阵?

标签 r machine-learning deep-learning h2o

library(h2o)
h2o.init(nthreads=-1)
test <- h2o.importFile(path = "C:/Users/AkshayJ/Documents/newapril/data/testdata.csv")
train <- h2o.importFile(path = "C:/Users/AkshayJ/Documents/newapril/data/traindata.csv")
y <- "Label"
train[,y] <- as.factor(train[,y])
test[,y] <- as.factor(test[,y])
train[,"Allele1Top"] <- as.factor(train[,"Allele1Top"])
test[,"Allele1Top"] <- as.factor(test[,"Allele1Top"])
train[,"Allele2Top"] <- as.factor(train[,"Allele2Top"])
test[,"Allele2Top"] <- as.factor(test[,"Allele2Top"])
train[,"Allele1Forward"] <- as.factor(train[,"Allele1Forward"])
test[,"Allele1Forward"] <- as.factor(test[,"Allele1Forward"])
train[,"Allele2Forward"] <- as.factor(train[,"Allele2Forward"])
test[,"Allele2Forward"] <- as.factor(test[,"Allele2Forward"])
train[,"Allele1AB"] <- as.factor(train[,"Allele1AB"])
test[,"Allele1AB"] <- as.factor(test[,"Allele1AB"])
train[,"Allele2AB"] <- as.factor(train[,"Allele2AB"])
test[,"Allele2AB"] <- as.factor(test[,"Allele2AB"])
train[,"Chr"] <- as.factor(train[,"Chr"])
test[,"Chr"] <- as.factor(test[,"Chr"])
train[,"SNP"] <- as.factor(train[,"SNP"])
test[,"SNP"] <- as.factor(test[,"SNP"])
x <- setdiff(names(train),y)
model <- h2o.deeplearning(
x = x,
y = y,
training_frame = train,
validation_frame = test,
distribution = "multinomial",
activation = "RectifierWithDropout",
hidden = c(32,32,32),
input_dropout_ratio = 0.2,
sparse = TRUE,
l1 = 1e-5,
epochs = 10)
predic <- h2o.predict(model, newdata = test)
table(pred=predic, true = test[,21])

一切都很好,但最后一行 表(预测=预测,真=测试[,21]) 给出错误 unique.default(x, nmax = nmax) 中的错误: 向量分配中的类型/长度无效(环境/0)

最佳答案

使用函数h2o.confusionMatrix()获取混淆矩阵。最简单的方法是为其提供模型以及您想要分析的数据:

h2o.confusionMatrix(model, test)

如果您查看 ?h2o.confusionMatrix,您会发现它还可以接受 H2OModelMetrics 对象。您可以通过调用 h2o.performance() 获得其中之一:

p = h2o.performance(model, test)
h2o.confusionMatrix(p)

我推荐第二种方式,因为 p 对象包含有关您的模型有多好的其他有用信息。

注意:无论哪种方式,你都没有使用你的预测。基本上:

  • h2o.performance 如果您想分析模型的质量。
  • h2o.predict 如果您想获得实际的预测。

关于r - 在这种情况下如何制作混淆矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43692058/

相关文章:

machine-learning - Keras:多类 NLP 任务中 model.evaluate 与 model.predict 的准确性差异

python - 了解 Tensorflow LSTM 模型输入?

python - 文本分类 CNN 过度拟合训练

python - NLP 通用英语到行动

r - 为什么这个 for 循环会出现 46 个错误?

python - 在 tensorflow 中,如何在不实际训练的情况下评估神经元网络

r - 使用 R 下载 *.xls 文件会生成错误

sockets - 权重在此代码中更新的位置?

r - R 中的全局变量

r - R中两位数字后加逗号?