Neural net confunsionmatrix

Hi, is possible write a confusionmatrix in neural?

This is my code:

iris[1,]
unique(iris$Species)

table(iris$Species) #record per specie

db_class<-iris

plot(db_class, col=as.factor(db_class$Species))

plot(db_class[, -ncol(db_class)], col=as.factor(db_class$Species))

library(class)


#--- neural net
library(neuralnet)
library(fastDummies)
db_class[1,]

db_class_nn<-db_class
unique(db_class_nn$Species)
db_class_nn$SETOSA<-ifelse(db_class_nn$Species=="setosa",1,0)
db_class_nn$VERSICOLOR<-ifelse(db_class_nn$Species=="versicolor",1,0)
db_class_nn$VIRGINICA<-ifelse(db_class_nn$Species=="virginica",1,0)

db_class_nn_norm<-db_class_nn
class(db_class_nn_norm)
normalizzo<-function(x){
  return((x-min(x))/(max(x)-min(x)))
}
db_class_nn_norm<-db_class_nn_norm[,-5]
db_class_nn_norm<-apply(db_class_nn_norm,2,normalizzo)
db_class_nn_norm[1,]

righe_train<-sample(nrow(db_class_nn_norm),nrow(db_class_nn_norm)*0.8)
db_class_nn_norm_train<-db_class_nn_norm[righe_train,]
db_class_nn_norm_test<-db_class_nn_norm[-righe_train,]

model_nn<-neuralnet(SETOSA+VERSICOLOR+VIRGINICA~.,
                    db_class_nn_norm_train,
                    act.fct="tanh",
                    linear.output = FALSE,
                    rep=2,
                    hidden=c(5,2),
                    threshold=0.05)
plot(model_nn,"best")

prev_nn_train<-compute(model_nn,db_class_nn_norm_train[,c(1:4)])
prev_nn_train$net.result

risultati_nn<-apply(prev_nn_train$net.result,1,which.max)

risultati_nn<-as.data.frame(risultati_nn)
colnames(risultati_nn)<-"RISULTATO"
risultati_nn$CLASSE<-ifelse(risultati_nn$RISULTATO == 1, "setosa",
                            ifelse(risultati_nn$RISULTATO == 2, "versicolor","virginica")
)

CrossTable(x=db_class_nn$Species[righe_train],y=risultati_nn$CLASSE)

confusionMatrix(db_class_nn$Species,risultati_nn)

I have tried with this but it doesn't work. I don't understand how to write the confusion matrix. Someone can help me? Thanks!

There's no error in you post so it's hard to tell what is going wrong.

When I run your code, it cannot fine the CrossTable or confusionMatrix functions. Try loading the library first. I know that caret has the confusionMatrix function.

Is this related to this similar post?

So we need to make risultati_nn$CLASSE into a factor with the same levels:

risultati_nn$CLASSE <- factor(risultati_nn$CLASSE, levels = levels(db_class_nn$Species))

It doesn't work

This is the correct code.
confusionMatrix(factor(db_class_nn$Species),factor(risultati_nn))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

The confusion matrix doesn't work, the error is:
Errore: data and reference should be factors with the same levels.