Caret nnet: logloss not working for twoClassSummary

时光怂恿深爱的人放手 提交于 2019-12-13 13:40:45

问题


I have a training dataset

    Out  Revolver   Ratio     Num ...
0   1    0.766127   0.802982  0   ...
1   0    0.957151   0.121876  1 
2   0    0.658180   0.085113  0 
3   0    0.233810   0.036050  3 
4   1    0.907239   0.024926  5 

The outcome variable Out is binary and only takes on the values 0 or 1. Num is not a factor

I then attempted to run nnet using caret. I want to eventually try nnGrid but I just want to make sure this works first:

nnTrControl=trainControl(method = "cv", classProbs = TRUE, summaryFunction = twoClassSummary, 
    number = 2,verboseIter = TRUE, returnData = FALSE, returnResamp = "all") 
#nnGrid = expand.grid(.size=c(1,4,7),.decay=c(0,0.001,0.1))

Outf<-factor(training$Out)
model <- train(Outf~ Revolver+Ratio+Num, data=training, method='nnet',
           trControl = nnTrControl, metric="logLoss")#, tuneGrid=nnGrid)

I get the error

Error in train.default(x, y, weights = w, ...) : 
At least one of the class levels is not a valid R variable name; This will cause errors when class probabilities are generated because the variables names will be converted to  X0, X1 . Please use factor levels that can be used as valid R variable names  (see ?make.names for help).

However, I've used caret and gotten this error before, which I resolved by using make.names. So when I try the below instead:

yCat<-make.names(training$Out, unique=FALSE, allow_=TRUE)
mnn <- model.matrix( ~Revolver + Ratio + Num, data = training)
model <- train(y=yCat, x=mnn, method='nnet',
           trControl = nnTrControl, metric="logLoss")#, tuneGrid=nnGrid)

I then get the message

The metric "logLoss" was not in the result set. ROC will be used instead.

But I don't understand why its not evaluating according to logLoss?

If I then use this to predict on a test set

probs<-predict(model, newdata=testSet, type="prob")

I get

 Error in eval(expr, envir, enclos) : object '(Intercept)' not found

How do I fix this?

来源:https://stackoverflow.com/questions/35569705/caret-nnet-logloss-not-working-for-twoclasssummary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!