【问题标题】:Naive Bayes prediction in R with reading characters as factors and without factorsR中的朴素贝叶斯预测,以阅读字符为因素,无因素
【发布时间】:2016-11-24 13:18:13
【问题描述】:

我正在尝试在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:更改了标题。原来的问题解决了。

【问题讨论】:

标签: r naivebayes


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 2016-07-29
    • 2012-02-21
    • 2011-12-28
    • 2019-05-27
    • 2013-11-24
    • 2011-10-07
    • 2019-03-16
    相关资源
    最近更新 更多