1. 朴素贝叶斯——文档分类 - 图1为给定文档的特征值(频数统计,预测文档提供),朴素贝叶斯——文档分类 - 图2为文档 ,则

    朴素贝叶斯——文档分类 - 图3

    1. 朴素贝叶斯——文档分类 - 图4每个文档类别的概率(某文档类别词数/总文档词数)
    2. 朴素贝叶斯——文档分类 - 图5给定类别下特征(被预测文档中出现的词)的概率
      1. 特征朴素贝叶斯——文档分类 - 图6有特征词朴素贝叶斯——文档分类 - 图7

    朴素贝叶斯——文档分类 - 图8

    1. 计算方法 朴素贝叶斯——文档分类 - 图9(训练文档中去计算)
    2. 朴素贝叶斯——文档分类 - 图10朴素贝叶斯——文档分类 - 图11词在 朴素贝叶斯——文档分类 - 图12类别所有文档中出现的次数
    3. 朴素贝叶斯——文档分类 - 图13 为所属类别 朴素贝叶斯——文档分类 - 图14下的文档所有词出现的次数和

      1. 拉普拉斯平滑系数

      如果词频列表里面 有很多出现次数都为 0,很可能计算结果都为零
      朴素贝叶斯——文档分类 - 图15
      拉普拉斯平滑系数朴素贝叶斯——文档分类 - 图16 一般为1, m 为训练文档中统计出的特征词个数

    1. 文本分类实例
      1. 加载数据集 ```python news = fetch20newsgroups(subset=’all’, data_home=’data’) #subset: ‘train’或者’test’,’all’,可选,选择要加载的数据集,fetch*的文件较大,所以需要下载,data_home是下载路径 print(news.target) print(news.target_names)

    结果: [10 3 17 … 3 1 7] [‘alt.atheism’, ‘comp.graphics’, ‘comp.os.ms-windows.misc’, ‘comp.sys.ibm.pc.hardware’, ‘comp.sys.mac.hardware’, ‘comp.windows.x’, ‘misc.forsale’, ‘rec.autos’, ‘rec.motorcycles’, ‘rec.sport.baseball’, ‘rec.sport.hockey’, ‘sci.crypt’, ‘sci.electronics’, ‘sci.med’, ‘sci.space’, ‘soc.religion.christian’, ‘talk.politics.guns’, ‘talk.politics.mideast’, ‘talk.politics.misc’, ‘talk.religion.misc’]

    1. b. 划分训练集和测试集,特征提取
    2. ```python
    3. # 进行数据分割
    4. x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=1)
    5. # 对数据集进行特征抽取
    6. tf = TfidfVectorizer()
    7. x_train = tf.fit_transform(x_train)
    8. x_test = tf.transform(x_test)

    c. 朴素贝叶斯预测

    1. # 进行朴素贝叶斯算法的预测,alpha是拉普拉斯平滑系数,分子和分母加上一个系数,分母加alpha*特征词数目
    2. mlt = MultinomialNB(alpha=1.0)
    3. mlt.fit(x_train, y_train)
    4. y_predict = mlt.predict(x_test)
    5. print("预测的文章类别为:", y_predict)
    6. print("准确率为:", mlt.score(x_test, y_test))
    7. print("每个类别的精确率和召回率:", classification_report(y_test, y_predict, target_names=news.target_names))
    8. 结果:
    9. 预测的文章类别为: [16 19 18 ... 13 7 14]
    10. 准确率为: 0.8518675721561969
    11. 每个类别的精确率和召回率:
    12. precision recall f1-score support
    13. alt.atheism 0.91 0.77 0.83 199
    14. comp.graphics 0.83 0.79 0.81 242
    15. comp.os.ms-windows.misc 0.89 0.83 0.86 263
    16. comp.sys.ibm.pc.hardware 0.80 0.83 0.81 262
    17. comp.sys.mac.hardware 0.90 0.88 0.89 234
    18. comp.windows.x 0.92 0.85 0.88 230
    19. misc.forsale 0.96 0.67 0.79 257
    20. rec.autos 0.90 0.87 0.88 265
    21. rec.motorcycles 0.90 0.95 0.92 251
    22. rec.sport.baseball 0.89 0.96 0.93 226
    23. rec.sport.hockey 0.95 0.98 0.96 262
    24. sci.crypt 0.76 0.97 0.85 257
    25. sci.electronics 0.84 0.80 0.82 229
    26. sci.med 0.97 0.86 0.91 249
    27. sci.space 0.92 0.96 0.94 256
    28. soc.religion.christian 0.55 0.98 0.70 243
    29. talk.politics.guns 0.76 0.96 0.85 234
    30. talk.politics.mideast 0.93 0.99 0.96 224
    31. talk.politics.misc 0.98 0.56 0.72 197
    32. talk.religion.misc 0.97 0.26 0.41 132
    33. accuracy 0.85 4712
    34. macro avg 0.88 0.84 0.84 4712
    35. weighted avg 0.87 0.85 0.85 4712

    d. 计算AUC

    1. # 把0-19总计20个分类,变为0和1
    2. y_test1 = np.where(y_test == 5, 1, 0)
    3. y_predict1 = np.where(y_predict == 5, 1, 0)
    4. # roc_auc_score的y_test只能是二分类,针对多分类如何计算AUC
    5. print("AUC指标:", roc_auc_score(y_test1, y_predict1))