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

    1.读取

    # 1、读取数据集
    def read_dataset():
         file_path = r'SMSSpamCollection'
         sms = open(file_path, encoding='utf-8')
         sms_data = []
         sms_label = []
         csv_reader = csv.reader(sms, delimiter='	')
         for line in csv_reader:
            sms_label.append(line[0])  # 提取出标签
            sms_data.append(preprocessing(line[1]))  # 提取出特征
         sms.close()
         return sms_data, sms_label

    2.数据预处理

    # 2、数据预处理
    def preprocess(text):
         tokens = [word for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]  # 分词
         stops = stopwords.words('english')  # 使用英文的停用词表
         tokens = [token for token in tokens if token not in stops]  # 去除停用词
         tokens = [token.lower() for token in tokens if len(token) >= 3]  # 大小写,短词
         wnl = WordNetLemmatizer()
         tag = nltk.pos_tag(tokens)  # 词性
         tokens = [wnl.lemmatize(token, pos=get_wordnet_pos(tag[i][1])) for i, token in enumerate(tokens)]  # 词性还原
         preprocessed_text = ' '.join(tokens)
         return preprocessed_text

    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)

    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()

    观察邮件与向量的关系

    向量还原为邮件

    4.模型选择

    from sklearn.naive_bayes import GaussianNB

    from sklearn.naive_bayes import MultinomialNB

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

    答:由于本次的邮件数据并不符合正态分布的特征,而是属于概率性的数据,因此不能选择高斯型分布模型,此处选择多项式分布模型。

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

    from sklearn.metrics import confusion_matrix

    confusion_matrix = confusion_matrix(y_test, y_predict)

    说明混淆矩阵的含义

    from sklearn.metrics import classification_report

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

     答:

    (1)混淆矩阵 confusion-matrix:

      TP(True Positive):真实为0,预测为0

      TN(True Negative):真实为1,预测为1

      FN(False Negative):真实为0,预测为1 

      FP(False Positive):真实为1,预测为0

    (2)准确率 accuracy:代表分类器对整个样本判断正确的比重。

    (3)精确率 precision:指被分类器判断正例中的正样本的比重。

    (4)召回率 recall:指被预测为正例的占总的正例的比重。

    (5)F值:准确率和召回率的加权调和平均。

    6.比较与总结

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

    CountVectorizer:特征数值计算类,文本特征提取方法。
    对于每一个训练文本,CountVectorizer会将文本中的词语转换为词频矩阵,它通过fit_transform函数计算各个词语在该训练文本出现的次数。

    TfidfVectorizer:可以把原始文本转化为tf-idf的特征矩阵,从而为后续的文本相似度计算,还关注其他包含这个词的文本,挖掘更有意义的特征。

    后者比较灵活。

  • 相关阅读:
    2020CCPC秦皇岛 K 【Kindom's Power】(树上贪心dp)
    对于树上状态机dp问题的一些总结与思考
    PTA_L3题解集
    PTA_L2题解集
    树上dp_学习笔记
    2020牛客国庆集训派对day2 B【Cheap Delivers】(最短路+状压dp)
    2020牛客国庆集训派对day1
    Codeforces 1426F【Number of Subsequences】(dp)
    2019icpc陕西省赛
    CF1281E【Jeremy Bearimy】(树上点对最大值/最小值和)
  • 原文地址:https://www.cnblogs.com/sgczw/p/13060743.html
Copyright © 2011-2022 走看看