zoukankan      html  css  js  c++  java
  • 13-垃圾邮件分类2

    1.读取

    2.数据预处理

    3.数据划分—训练集和测试集数据划分

    from sklearn.model_selection import train_test_split

    x_train,x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, random_state=0, stratify=y_train)

    def split_dataset(data, label):
         x_train, x_test, y_train, y_test = train_test_split(data, label, test_size=0.2, random_state=0, stratify=label)
         return x_train, x_test, y_train, y_test

    4.文本特征提取

    sklearn.feature_extraction.text.CountVectorizer

    https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html?highlight=sklearn%20feature_extraction%20text%20tfidfvectorizer

    sklearn.feature_extraction.text.TfidfVectorizer

    https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html?highlight=sklearn%20feature_extraction%20text%20tfidfvectorizer#sklearn.feature_extraction.text.TfidfVectorizer

    from sklearn.feature_extraction.text import TfidfVectorizer

    tfidf2 = TfidfVectorizer()

    观察邮件与向量的关系

    向量还原为邮件

    # 把文本转化为tf-idf的特征矩阵
    def tfidf_dataset(x_train,x_test):
         tfidf2 = TfidfVectorizer()
         X_train = tfidf2.fit_transform(x_train)
         X_test = tfidf2.transform(x_test)
         return X_train, X_test, tfidf2
    
    # 向量还原成邮件
    def revert_mail(x_train, X_train, model):
        s = X_train.toarray()[0]
        print("第一封邮件向量表示为:", s)
        a = np.flatnonzero(X_train.toarray()[0])
        print("非零元素的位置:", a)
        print("向量的非零元素的值:", s[a])
        b = model.vocabulary_  # 词汇表
        key_list = []
        for key, value in b.items():
            if value in a:
                key_list.append(key)  # key非0元素对应的单词
        print("向量非零元素对应的单词:", key_list)
        print("向量化之前的邮件:", x_train[0])

    5.模型选择

    from sklearn.naive_bayes import GaussianNB

    from sklearn.naive_bayes import MultinomialNB

    说明为什么选择这个模型?

    邮件数据是概率性的,不符合正态分布,应该选择多项式分布模型

    def mnb_model(x_train, x_test, y_train, y_test):
        mnb = MultinomialNB()
        mnb.fit(x_train, y_train)
        predict = mnb.predict(x_test)
        print("总数:", len(y_test))
        print("预测正确数:", (predict == y_test).sum())
        print("预测准确率:",sum(predict == y_test) / len(y_test))
        return predict

    6.模型评价:混淆矩阵,分类报告

    from sklearn.metrics import confusion_matrix

    confusion_matrix = confusion_matrix(y_test, y_predict)

    说明混淆矩阵的含义

    混淆矩阵是机器学习中总结分类模型预测结果的情形分析表,以矩阵形式将数据集中的记录按照真实的类别与分类模型预测的类别判断两个标准进行汇总。

    混淆矩阵 confusion-matrix:

    •   TP(True Positive):真实为0,预测为0
    •   TN(True Negative):真实为1,预测为1
    •   FN(False Negative):真实为0,预测为1 
    •   FP(False Positive):真实为1,预测为0

    from sklearn.metrics import classification_report

    说明准确率、精确率、召回率、F值分别代表的意义 

    准确率 accuracy

    • 所有样本中被预测正确的样本的比率
    • 分类模型总体判断的准确率(包括了所有class的总体准确率)


    精确率Precision

    • 预测为正类0的准确率
    • TP / ( TP + FP )

    召回率 recall

    • 真实为0的准确率

    • 真实为1的准确率
    • Recall = TN/(TN+FP)

    F1值

    • 用来衡量二分类模型精确度的一种指标。它同时兼顾了分类模型的准确率和召回率。F1分数可以看作是模型准确率和召回率的一种加权平均,它的最大值是1,最小值是0。
     
    def class_report(ypre_mnb, y_test):
        conf_matrix = confusion_matrix(y_test, ypre_mnb)
        print("=====================================================")
        print("混淆矩阵:
    ", conf_matrix)
        c = classification_report(y_test, ypre_mnb)
        print("=====================================================")
        print("分类报告:
    ", c)
        print("模型准确率:", (conf_matrix[0][0] + conf_matrix[1][1]) / np.sum(conf_matrix))

    7.比较与总结

    如果用CountVectorizer进行文本特征生成,与TfidfVectorizer相比,效果如何?

    CountVectorizer只考虑单个词在训练文本中出现的频率,而TfidfVectorizer除了考量某个词在当前训练文本中出现的频率之外,同时包含这个词的其它训练文本数量。

    所以训练文本的数量越多,TfidfVectorizer更有优势,而且TfidfVectorizer可以削减高频率没有意义的词。

  • 相关阅读:
    4月19日 疯狂猜成语-----第五次站立会议 参会人员:杨霏,袁雪,胡潇丹,郭林林,尹亚男,赵静娜
    prototype
    angularJs scope的简单模拟
    angularjs DI简单模拟
    洗牌算法
    深入探索 TCP TIME-WAIT
    Logitech k480 蓝牙键盘连接 ubuntu 系统
    在 centos6 上安装 LAMP
    vlc 播放器的点播和广播服务
    Linux 文件系统及 ext2 文件系统
  • 原文地址:https://www.cnblogs.com/ccw1124486193/p/12970082.html
Copyright © 2011-2022 走看看