【发布时间】:2021-03-28 21:04:12
【问题描述】:
我正在尝试在数据集上训练神经网络。一切正常。如果我将 70% 或 50% 的数据指定为训练,将其余数据指定为测试,则代码没有问题。但是当我指定 55% 和 45% 用于训练和测试时,内核卡住并给出以下错误:
Error in plot.nn(nnet, rep = "best"): weights were not calculated
Traceback:
1. plot(nnet, rep = "best")
2. plot(nnet, rep = "best")
3. plot.nn(nnet, rep = "best")
4. stop("weights were not calculated")
这是我目前写的代码:
library(neuralnet)
Main <- read.table("miRNAs200TypesofCancerData.txt", header = TRUE,stringsAsFactors = T ) # reading the dataset
for(i in 1:ncol(Main)){
Main[is.na(Main[,i]), i] <- mean(Main[,i], na.rm = TRUE)
}
set.seed(123)
# in the following line, if you replace p=0.55 by p=0.5, no problem is reported and everything works smoothly
indexes = createDataPartition(Main$Type, p=0.55, list = F)
# Creating test and train sets.
train = Main[indexes, ]
test = Main[-indexes, ]
xtest = test[, -1]
ytest = test[, 1]
nnet = neuralnet(Type~., train, hidden = 5, linear.output = FALSE)
# Plotting
plot(nnet, rep = "best")
# Predictions
ypred = neuralnet::compute(nnet, xtest)
yhat = ypred$net.result
yhat=data.frame("yhat"=ifelse(max.col(yhat[ ,1:4])==1, "Mesenchymal",
ifelse(max.col(yhat[ ,1:4])==2, "Proneural",
ifelse(max.col(yhat[ ,1:4])==3, "Classical","Neural"))))
# Confusion matrix
cm = confusionMatrix(as.factor(yhat$yhat),as.factor(ytest))
print(cm)
这是一个链接:Dataset
【问题讨论】:
标签: r neural-network train-test-split