簡體   English   中英

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 並注意參數部分。

"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM