How do I make a confusion matrix in this case?

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])

      

Everything is fine, but the last line of table (pred = predic, true = test [, 21]) gives an error Error in unique.default (x, nmax = nmax): invalid type / length (environment / 0) in vector distribution

+3


source to share


1 answer


Use a function h2o.confusionMatrix()

to get a confusion matrix. An easy way is to give it the model and data you want to parse:

h2o.confusionMatrix(model, test)

      

If you look at ?h2o.confusionMatrix

, you will see that it can also accept an object H2OModelMetrics

. You get one of these by calling h2o.performance()

:

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

      



I recommend the second way, as the object p

contains other useful information about how good your model is.

Note: You didn't use your predictions anyway. Basically:

  • h2o.performance

    if you want to analyze the quality of the model.
  • h2o.predict

    if you want actual forecasts.
+4


source







All Articles