【发布时间】:2018-11-11 02:14:50
【问题描述】:
df 在训练和测试数据帧中被拆分。训练数据帧分为训练和测试数据帧。因变量Y 是二进制(因子),值为 0 和 1。我正在尝试使用此代码(神经网络、插入符号包)预测概率:
library(caret)
model_nn <- train(
Y ~ ., training,
method = "nnet",
metric="ROC",
trControl = trainControl(
method = "cv", number = 10,
verboseIter = TRUE,
classProbs=TRUE
)
)
model_nn_v2 <- model_nn
nnprediction <- predict(model_nn, testing, type="prob")
cmnn <-confusionMatrix(nnprediction,testing$Y)
print(cmnn) # The confusion matrix is to assess/compare the model
但是,它给了我这个错误:
Error: 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).
我不明白“使用可用作有效 R 变量名称的因子级别”是什么意思。因变量Y 已经是一个因子,但不是有效的 R 变量名称?
PS:如果您删除trainControl() 中的classProbs=TRUE 和train() 中的metric="ROC",代码将完美运行。但是,"ROC" 指标是我比较最佳模型的指标,因此我正在尝试使用“ROC”指标制作模型。
编辑:代码示例:
# You have to run all of this BEFORE running the model
classes <- c("a","b","b","c","c")
floats <- c(1.5,2.3,6.4,2.3,12)
dummy <- c(1,0,1,1,0)
chr <- c("1","2","2,","3","4")
Y <- c("1","0","1","1","0")
df <- cbind(classes, floats, dummy, chr, Y)
df <- as.data.frame(df)
df$floats <- as.numeric(df$floats)
df$dummy <- as.numeric(df$dummy)
classes <- c("a","a","a","b","c")
floats <- c(5.5,2.6,7.3,54,2.1)
dummy <- c(0,0,0,1,1)
chr <- c("3","3","3,","2","1")
Y <- c("1","1","1","0","0")
df <- cbind(classes, floats, dummy, chr, Y)
df <- as.data.frame(df)
df$floats <- as.numeric(df$floats)
df$dummy <- as.numeric(df$dummy)
【问题讨论】:
标签: r machine-learning neural-network r-caret roc