2020-11-26朴素贝叶斯分类器
2020-11-26 本文已影响0人
Silmarillion123
library(e1071)
library(ggplot2)
data('thyroid',package = 'mclust')
plot(thyroid$Diagnosis)
data<-thyroid
set.seed(2016)
N<-nrow(thyroid)
train<-sample(1:N,150,FALSE)#在1到N抽样,抽150次,采样不更换
head(train)
fit<-naiveBayes(Diagnosis ~.,data=data[train,])
attributes(fit)#查看属性
#$names
#[1] "apriori" "tables" "levels" "isnumeric"
#[5] "call"
#$class
#[1] "naiveBayes"
fit$apriori#参数aprioori包含类别分布
fit$table$RT3U
> fit$table$RT3U
RT3U
Y [,1] [,2]
Hypo 121.2632 10.943502
Normal 111.3585 7.950069
Hyper 93.5200 19.977320
#分别为均值和标准差
pred<-predict(fit,data[-train,-1],type='class')#给出分类
head(pred,4)
pred<-predict(fit,data[-train,-1],type='raw')#给出概率
table(pred,data$Diagnosis[-train])
#pred Hypo Normal Hyper
#Hypo 11 1 0
#Normal 0 43 0
#Hyper 0 0 10
混淆矩阵显示学习效果良好