2015-11-01 101 views
0
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. 

回答

0

此錯誤消息是因爲你在你的數據有字符值。

試着用na.strings =「?」讀取肝炎數據集。這是在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數據倉庫基礎上的意見

編輯數據集中的第一個變量:

,如果你使用的公式符號的na.action只能NNET 。 所以你的情況:

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

我稍微修改了數據集和類是我20屬性,也包括在NNET功能na.action =「na.omit」,但仍是同樣的錯誤出現。 – amankedia

+0

檢查數據集的結構。但在原始數據集中,缺失值是一個問號。如果你不考慮這一點,你的價值將是一個人物。檢查str(肝炎),看看你是否有某個角色專欄。因爲這就是這個錯誤所指向的。 – phiver

+0

沒有字符列在那裏,但你能幫我關於如何照顧數據集中的這些問號? – amankedia