1

這是關於該主題的錯誤消息。當我嘗試運行naive.bayes分類器時遇到此錯誤。這裏是我的訓練數據的彙總:參數的'type'(字符)無效

'data.frame': 7269 obs. of 193 variables: 
$ pid  : int 2 4 5 7 10 11 14 18 25 31 ... 
$ acquir : int 0 0 0 0 1 1 0 0 0 0 ... 
$ addit  : int 0 0 0 0 2 2 0 0 0 0 ... 
$ agre  : int 0 0 0 0 0 0 0 0 0 0 ... 
$ agreement : int 0 0 0 0 0 0 0 0 0 0 ... 
$ also  : int 1 0 0 0 2 2 0 0 0 0 ... 
$ american : int 0 0 0 0 0 0 0 0 0 0 ... 
$ announc : int 0 0 0 0 0 0 0 0 0 0 ... 
$ annual : int 0 0 0 0 0 0 0 0 2 0 ... 
$ approv : int 0 3 0 0 0 0 0 0 0 0 ... 
$ april  : int 0 0 0 0 0 0 0 0 1 0 ... 
$ bank  : int 0 7 0 0 0 0 0 0 0 0 ... 
$ base  : int 0 0 0 0 0 0 0 0 0 0 ... 
. 
. 
$... all of them are integer, except the class column 
. 
. 
$ class  : Factor w/ 10 levels "acq","corn","crude",..: 1 1 4 4 9 1 4 3 1 4 ... 

這是naive.bayes()行:

model <- naiveBayes(as.factor(class) ~ ., data = as.matrix(train), laplace = 3) 

誰能告訴我它爲什麼發生?:

Error in sum(x) : invalid 'type' (character) of argument 
+0

由於'as.matrix(train)',最終你的數據被轉換爲字符。試試'model < - naiveBayes(class〜。,data = train,laplace = 3)'或者最終'model < - naiveBayes(train $ class〜。,data = train [,-c(「class」)],laplace = 3)' – jogo

+0

@jogo它的工作!謝謝!我想接受你的答案,但是如何? – Whcrs

+0

我現在就回答了。第一個變體還是隻有第二個變體? – jogo

回答

1

最終數據由於as.matrix(train)轉換爲字符。嘗試

model <- naiveBayes(class ~ ., data=train, laplace = 3) 

或最終

model <- naiveBayes(train$class ~ ., data=train[, -c("class")], laplace = 3) 

第二個變量是作爲第一個變體或多或少相同。公式的RHS中的.擴展爲「所有其他變量」;因此不包括LHS上提到的class列。 (更多信息請參考文檔formula