运行神经网络时出错

时间:2015-11-01 14:53:01

标签: r neural-network

library(nnet)
set.seed(9850)
train1<- sample(1:155,110)
test1 <- setdiff(1:110,train1)
ideal <- class.ind(hepatitis$class)
hepatitisANN = nnet(hepatitis[train1,-20], ideal[train1,], size=10, softmax=TRUE)
j <- predict(hepatitisANN, hepatitis[test1,-20], type="class")
hepatitis[test1,]$class
table(predict(hepatitisANN, hepatitis[test1,-20], type="class"),hepatitis[test1,]$class)
confusionMatrix(hepatitis[test1,]$class, j)

错误:

Error in nnet.default(hepatitis[train1, -20], ideal[train1, ], size = 10,  : 
  NA/NaN/Inf in foreign function call (arg 2)
In addition: Warning message:
In nnet.default(hepatitis[train1, -20], ideal[train1, ], size = 10,  :
  NAs introduced by coercion

hepatitis variable consists of the hepatitis dataset available on UCI.

1 个答案:

答案 0 :(得分:0)

此错误消息是因为您的数据中包含字符值。

尝试用na.strings =&#34;?&#34;阅读肝炎数据集。这在uci页面上的数据集描述中定义。

headers <- c("Class","AGE","SEX","STEROID","ANTIVIRALS","FATIGUE","MALAISE","ANOREXIA","LIVER BIG","LIVER FIRM","SPLEEN PALPABLE","SPIDERS","ASCITES","VARICES","BILIRUBIN","ALK PHOSPHATE","SGOT","ALBUMIN","PROTIME","HISTOLOGY")
hepatitis <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/hepatitis/hepatitis.data", header = FALSE, na.strings = "?")
names(hepatitis) <- headers

library(nnet)
set.seed(9850)
train1<- sample(1:155,110)
test1 <- setdiff(1:110,train1)

ideal <- class.ind(hepatitis$Class)

# will give error due to missing values
# 1st column of hepatitis dataset is the class variable
hepatitisANN <- nnet(hepatitis[train1,-1], ideal[train1,], size=10, softmax=TRUE)

此代码不会提供您的错误,但会在缺失值时出错。在继续之前,您需要先解决这些问题。 还要注意,类变量是直接来自UCI数据存储库的数据集中的第一个变量

根据评论进行修改:

只有在使用nnet的公式表示法时,na.action才有效。 所以在你的情况下:

hepatitisANN <- nnet(class.ind(Class)~., hepatitis[train1,], size=10,  softmax=TRUE, na.action = na.omit)