R中的朴素贝叶斯预测以阅读字符为因素和没有因素

Naive Bayes prediction in R with reading characters as factors and without factors

我正尝试在 Mushroom Data set 上使用朴素贝叶斯。数据集是 8124*23,第一列作为响应变量 {'edible','poisonous'}。我已经消除了丢失的数据。最后,数据集为5644*23。下面是我用过的代码。

mushroom.data <- read.csv("mushroom.data",header = FALSE, stringsAsFactors = FALSE)
#mushroom.data <- read.csv("mushroom.data",header = FALSE, stringsAsFactors = TRUE)

#Eliminating missing data
mushroom.data <- subset(mushroom.data,mushroom.data$V12 != '?')
# Factoring target class
mushroom.data$V1 <- as.factor(mushroom.data$V1)
# First 4000 records as Training set. 
mushroom.train.class <- mushroom.data[1:4000,1]
mushroom.train.data <- mushroom.data[1:4000,-1]
# Building naive bayes classifier
nb.model <- naiveBayes(mushroom.train.data,mushroom.train.class,laplace = 1)
# Last 1644 are Test records
mushroom.test.data <- mushroom.data[4001:5644,-1]
mushroom.test.class <- mushroom.data[4001:5644,1]
# Predicition
nb.pred <- predict(nb.model,mushroom.test.data)
# checking proportions of the predictions
prop.table(table(nb.pred))

模型将所有内容预测为 edible class,其中 stringAsFactors = FALSE 的准确率为 10-15%,而 stringAsFactors = TRUE 的准确率为 91%。保理是怎么回事?

编辑 1:更改了标题。原来的问题解决了。

您不能使用 NaiveBayes 为角色建模。检查 ?NaiveBayes 并查看参数部分。