R语言使用朴素贝叶斯分类算法

说明

朴素贝叶斯分类器也是一类基于概率的分类器,它源于贝叶斯理论,假设样本属性之间相互独立。

操作

利用朴素贝叶斯分类器对churn数据集进行分类:
导入e1071库,使用naiveBayes函数构建分类器

library(e1071)
classifier = naiveBayes(trainset[,!names(trainset) %in% c("churn")],trainset$churn)
classifier
Naive Bayes Classifier for Discrete Predictors

Call:
naiveBayes.default(x = trainset[, !names(trainset) %in% c("churn")], 
    y = trainset$churn)

A-priori probabilities:
trainset$churn
      yes        no 
0.1477322 0.8522678 

Conditional probabilities:
              international_plan
trainset$churn          0          1
           yes 0.70467836 0.29532164
           no  0.93512418 0.06487582

              voice_mail_plan
trainset$churn         0         1
           yes 0.8333333 0.1666667
           no  0.7045109 0.2954891

              number_vmail_messages
trainset$churn     [,1]     [,2]
           yes 5.099415 11.80618
           no  8.674607 14.03670

              total_day_minutes
trainset$churn     [,1]     [,2]
           yes 205.8877 69.10294
           no  174.2555 50.16357

              total_day_calls
trainset$churn     [,1]     [,2]
           yes 101.0234 22.02903
           no  100.5509 19.67038

              total_day_charge
trainset$churn     [,1]      [,2]
           yes 35.00143 11.747587
           no  29.62402  8.527769

              total_eve_minutes
trainset$churn     [,1]     [,2]
           yes 213.7269 51.92206
           no  199.6197 50.53780

              total_eve_calls
trainset$churn     [,1]     [,2]
           yes 101.4123 19.48658
           no   99.9478 20.16161

              total_eve_charge
trainset$churn     [,1]     [,2]
           yes 18.16702 4.413058
           no  16.96789 4.295730

              total_night_minutes
trainset$churn     [,1]     [,2]
           yes 205.4640 47.11434
           no  201.4184 51.34049

              total_night_calls
trainset$churn     [,1]     [,2]
           yes 100.2573 20.32690
           no  100.0193 19.68094

              total_night_charge
trainset$churn     [,1]    [,2]
           yes 9.245994 2.12038
           no  9.063882 2.31040

              total_intl_minutes
trainset$churn     [,1]     [,2]
           yes 10.73684 2.752784
           no  10.15119 2.819086

              total_intl_calls
trainset$churn     [,1]     [,2]
           yes 4.134503 2.487395
           no  4.514445 2.394724

              total_intl_charge
trainset$churn     [,1]      [,2]
           yes 2.899386 0.7432760
           no  2.741343 0.7611755

              number_customer_service_calls
trainset$churn     [,1]     [,2]
           yes 2.204678 1.808803
           no  1.441460 1.150114

生成测试数据集分类表:

bayes.table = table(predict(classifier,testset[,!names(testset) %in% c("churn")]),testset$churn)
bayes.table

      yes  no
  yes  68  45
  no   73 832

利用分类表生成混淆矩阵:

 confusionMatrix(bayes.table)
Confusion Matrix and Statistics


      yes  no
  yes  68  45
  no   73 832

               Accuracy : 0.8841          
                 95% CI : (0.8628, 0.9031)
    No Information Rate : 0.8615          
    P-Value [Acc > NIR] : 0.01880         

                  Kappa : 0.4701          
 Mcnemar's Test P-Value : 0.01294         

            Sensitivity : 0.4823          
            Specificity : 0.9487          
         Pos Pred Value : 0.6018          
         Neg Pred Value : 0.9193          
             Prevalence : 0.1385          
         Detection Rate : 0.0668          
   Detection Prevalence : 0.1110          
      Balanced Accuracy : 0.7155          

       'Positive' Class : yes    

说明

朴素贝叶斯算法假设特征变量都是条件独立,即预测变量(x)对分类结果(c)的影响与其它变量对c的影响是相互独立的。
先验概率P(ωj)是由先验知识而获得的。
后验概率P(ωj|x),即假设特征值x已知的条件下类别属于ωj的概率。朴素贝叶斯算法的优势在于其简单性,应用也比较直接,适合用训练数据集规格较小,有可能存在某些缺失与噪音的情况,预测值的概率计算比较简单,算法不足之处在于它假定的所有的特征变量之间相互独立,并且同等重要,这个前提在现实世界中很难成立。
本节使用e1071包中的朴素贝叶斯分类器构成分类模型,首先,我们假定在朴素贝叶斯函数中调用的所有变量(包括churn类标号)都是输入函数的第一输入参数,churn类标号为算法的第二输入参数。接下来,将分类模型指派给不同的变量分类。再输出分类器的相关信息,包括函数调用、先验概率以及条件概率等。我们也可以使用predict函数预测结果,并使用table函数得到测试数据集的分类表,最后,生成混淆矩阵计算分类模型。

你可能感兴趣的:(R语言-分类1(树,延迟,概率))