R. python新手日记机器学习特征变量选择

R语言 朴素贝叶斯分类器①

2018-01-16  本文已影响27人  柳叶刀与小鼠标

这种学习方法基于条件概率,也就是通过已经给定的东西来推断一件事情的发生可能性。朴素贝叶斯应用了贝叶斯定理和朴素独立性假设。

  • 优势
    • 不相关特征不敏感
    • 一次扫描就能快速训练
    • 快速分类
    • 能够处理任意数量的预测因子,不论他们是连续的还是分类的
    • 尤其适合高维数据
  • 劣势
    • 假定了特征之间相互独立

R中的e1071包中的naiveBayes函数可以实现朴素贝叶斯算法,具体的函数格式如下:naiveBayes(formula,data,laplace=0,subset)

image
setwd("E:\\Rwork")
install.packages("e1071")
library(e1071)
index <- sample(nrow(iris),0.75*nrow(iris))
train <- iris[index,]
test <- iris[index,]
nb1 <- naiveBayes(Species ~., data =train )


prediction <- predict(nb1, test[,-5])
xlab <- table(prediction , test$Species)
xlab

prediction   setosa versicolor virginica
  setosa         40          0         0
  versicolor      0         38         2
  virginica       0          2        30





pre1 <- predict(nb1,test)
a <- table(test$Species,pre1)
(sum(a)-sum(diag(a)))/sum(a)
b <- paste0(round((sum(a)-sum(diag(a)))*100/sum(a),2),"%")
library(gmodels)
CrossTable(test$Species,pre1,prop.r = FALSE,
           prop.c = FALSE,prop.t = TRUE,prop.chisq = FALSE)
   Cell Contents
|-------------------------|
|                       N |
|         N / Table Total |
|-------------------------|

 
Total Observations in Table:  112 

 
             | pre1 
test$Species |     setosa | versicolor |  virginica |  Row Total | 
-------------|------------|------------|------------|------------|
      setosa |         40 |          0 |          0 |         40 | 
             |      0.357 |      0.000 |      0.000 |            | 
-------------|------------|------------|------------|------------|
  versicolor |          0 |         38 |          2 |         40 | 
             |      0.000 |      0.339 |      0.018 |            | 
-------------|------------|------------|------------|------------|
   virginica |          0 |          2 |         30 |         32 | 
             |      0.000 |      0.018 |      0.268 |            | 
-------------|------------|------------|------------|------------|
Column Total |         40 |         40 |         32 |        112 | 
-------------|------------|------------|------------|------------|

上一篇下一篇

猜你喜欢

热点阅读