如何生成混淆矩阵并找到朴素贝叶斯分类器的误分类率? [英] How to produce a confusion matrix and find the misclassification rate of the Naïve Bayes Classifier?

查看:99
本文介绍了如何生成混淆矩阵并找到朴素贝叶斯分类器的误分类率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 R 中的 iris 数据集,我尝试将朴素贝叶斯分类器拟合到 iris 训练数据,以便为朴素贝叶斯分类器生成训练数据集(预测与实际)的混淆矩阵,什么是朴素贝叶斯分类器的误分类率?

Using the iris dataset in R, I'm trying to fit a a Naïve Bayes classifier to the iris training data so I could Produce a confusion matrix of the training data set (predicted vs actual) for the naïve bayes classifier, what is the misclassification rate of the Naïve Bayes Classifier?

这是我目前的代码:

 iris$spl=sample.split(iris,SplitRatio=0.8)
 train=subset(iris, iris$spl==TRUE)
 test=subset(iris, iris$spl==FALSE)

 iris.nb <- naiveBayes(Species~.,data = train)
 iris.nb

 nb_test_predict <- predict(iris.nb, train)

关于如何解决这个问题有什么建议吗?

Any suggestions on how to approach this problem?

推荐答案

Package caret 包括返回非常完整输出的 confusionMatrix 函数.

Package caret includes confusionMatrix function that returns a very complete output.

library(e1071)
library(caTools)
library(caret)

iris$spl = sample.split(iris, SplitRatio = 0.8)
train <- subset(iris, iris$spl == TRUE)
test <- subset(iris, iris$spl == FALSE)

iris.nb <- naiveBayes(Species ~ ., data = train)

nb_train_predict <- predict(iris.nb, test[ , names(test) != "Species"])

cfm <- confusionMatrix(nb_train_predict, test$Species)
cfm

# Confusion Matrix and Statistics
# 
# Reference
# Prediction   setosa versicolor virginica
# setosa         17          0         0
# versicolor      0         14         1
# virginica       0          2        16
# 
# Overall Statistics
# 
# Accuracy : 0.94            
# 95% CI : (0.8345, 0.9875)
# No Information Rate : 0.34            
# P-Value [Acc > NIR] : < 2.2e-16       
# 
# Kappa : 0.9099          
# Mcnemar's Test P-Value : NA              
# 
# Statistics by Class:
# 
#                      Class: setosa Class: versicolor Class: virginica
# Sensitivity                   1.00            0.8750           0.9412
# Specificity                   1.00            0.9706           0.9394
# Pos Pred Value                1.00            0.9333           0.8889
# Neg Pred Value                1.00            0.9429           0.9688
# Prevalence                    0.34            0.3200           0.3400
# Detection Rate                0.34            0.2800           0.3200
# Detection Prevalence          0.34            0.3000           0.3600
# Balanced Accuracy             1.00            0.9228           0.9403

将混淆矩阵显示为 ggplot 图形:

To display confusion matrix as ggplot graphic:

library(ggplot2)
library(scales)

ggplotConfusionMatrix <- function(m){
  mytitle <- paste("Accuracy", percent_format()(m$overall[1]),
                   "Kappa", percent_format()(m$overall[2]))
  p <-
    ggplot(data = as.data.frame(m$table) ,
           aes(x = Reference, y = Prediction)) +
    geom_tile(aes(fill = log(Freq)), colour = "white") +
    scale_fill_gradient(low = "white", high = "steelblue") +
    geom_text(aes(x = Reference, y = Prediction, label = Freq)) +
    theme(legend.position = "none") +
    ggtitle(mytitle)
  return(p)
}

ggplotConfusionMatrix(cfm)

这篇关于如何生成混淆矩阵并找到朴素贝叶斯分类器的误分类率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆