R中的朴素贝叶斯函数

时间:2021-01-08 17:21:19

标签: r

我正在尝试将朴素贝叶斯算法应用于我的数据集,但不知何故最终的comprovation 不起作用。我错过了什么?

#Loading library
if(!require(e1071)){
    install.packages("e1071")
    library(e1071)
}

#Loading subdataset from data

d <- data[,c("Region","Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen")]

#Creating both train and test datasets
ind <- sample(2,nrow(d), replace = TRUE, prob = c(0.7,0.3) ) #70% entrenamiento y 30% test
trainData<- d[ind==1,]
testData<- d[ind==2,]

#Executing Naive Bayes
mod <- naiveBayes(Region ~ ., data = trainData)
mod

#Final comprovation
pred <- predict(mod, testData)
tab <- table(testData$Region, pred, dnn = c("Actual", "Predicha"))
confusionMatrix(tab)

数据集样本:

Channel Region Fresh  Milk Grocery Frozen Detergents_Paper Delicassen
1         2      3 12669  9656    7561    214             2674       1338
2         2      3  7057  9810    9568   1762             3293       1776
3         2      3  6353  8808    7684   2405             3516       7844
4         1      3 13265  1196    4221   6404              507       1788
5         2      3 22615  5410    7198   3915             1777       5185
6         2      3  9413  8259    5126    666             1795       1451
7         2      3 12126  3199    6975    480             3140        545
8         2      3  7579  4956    9426   1669             3321       2566
9         1      3  5963  3648    6192    425             1716        750
10        2      3  6006 11093   18881   1159             7425       2098

提前致谢

0 个答案:

没有答案