R - 为 multinomial_naive_bayes() 函数生成的模型生成混淆矩阵和 ROC [英] R - Generate confusion matrix and ROC for model generated by multinomial_naive_bayes() function

查看:122
本文介绍了R - 为 multinomial_naive_bayes() 函数生成的模型生成混淆矩阵和 ROC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多因子/分类/名义列/变量/特征的数据集.我需要为这些数据创建一个多项式朴素贝叶斯分类器.我尝试使用 caret 库,但我认为这不是在做多项式朴素贝叶斯,我认为它是在做高斯朴素贝叶斯,详情

I have a data set with many factor/categorical/nominal columns/variables/features. I need to create a multinomial naive bayes classifier for this data. I tried using the caret library but I don't think that was doing a multinomial naive bayes, I think it was doing gaussian naive bayes, details here. I have now discovered multinomial_naive_bayes() which seems to be perfect. It seems to handle nulls in the predictor variables and a variable with only 1 value without complaining.

The issue is, I can't figure out how to do my "post processing/analysis" of the model generated by the multinomial_naive_bayes() function. I want to get a caret style confusionMatrix on the model and also on the prediction output vs the test data to assess the classifier. I would also like to generate a ROC curve. How can I do this?

I have included the sample/reference/example from the documentation of multinomial_naive_bayes() below, how would I update this code to get my confusionMatricies and ROC curve.

From: R Package 'naivebayes', section: multinomial_naive_bayes pg 10

library(naivebayes)

### Simulate the data:
cols <- 10 ; rows <- 100
M <- matrix(sample(0:5, rows * cols, TRUE, prob = c(0.95, rep(0.01, 5))), nrow = rows, ncol = cols)
y <- factor(sample(paste0("class", LETTERS[1:2]), rows, TRUE, prob = c(0.3,0.7)))
colnames(M) <- paste0("V", seq_len(ncol(M)))
laplace <- 1

### Train the Multinomial Naive Bayes
mnb <- multinomial_naive_bayes(x = M, y = y, laplace = laplace)
summary(mnb)
    
# Classification
head(predict(mnb, newdata = M, type = "class")) # head(mnb %class% M)

# Posterior probabilities
head(predict(mnb, newdata = M, type = "prob")) # head(mnb %prob% M)

# Parameter estimates
coef(mnb)

解决方案

You can use the caret function confusionMatrix:

library(caret)
pred = predict(mnb, newdata = M, type = "class")
confusionMatrix(table(pred,y))

Confusion Matrix and Statistics

        y
pred     classA classB
  classA     10      3
  classB     20     67

Or if the factor levels are the same:

confusionMatrix(pred,y)

For ROC curve, you need to provide the probability of the prediction:

library(pROC)
roc_ = roc(y,predict(mnb, newdata = M, type ="prob")[,2])

plot(roc_)

这篇关于R - 为 multinomial_naive_bayes() 函数生成的模型生成混淆矩阵和 ROC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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