使用朴素贝叶斯进行文本分类(代码实验:超详细)

1、首先需要了解朴素贝叶斯:对于给出的待分类样本,求解在此样本出现的条件下各个类别出现的概率,哪个最大,就认为此待分类样本属于哪个类别([参考文章](https://zhuanlan.zhihu.com/p/656721603))
2、数据格式:训练数据和测试数据都需要文本及其对应类型标签
在这里插入图片描述

3、代码实验:

1、获取已分类文本数据及其类型
2、对其进行分词和去停用词操作
3、训练朴素贝叶斯分类器:
  (1)获取文本数据的特征矩阵
  (2)根据文本数据特征矩阵和对应的目标标签训练朴素贝叶斯分类器
4、测试朴素贝叶斯分类器:
  (1)使用之前训练好的文本特征提取器对测试数据进行转换,得到测试集的特征矩阵
  (2)使用之前训练好的文本分类器对测试数据进行预测,得到预测结果
5、预测数据:
  (1)对其进行分词和去停用词操作
  (2)使用训练好的文本特征提取器对预测数据进行转换,得到特征矩阵
  (3)使用训练好的文本分类器对预测数据进行分类,得到预测结果

3、实验代码

import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from tqdm import tqdm
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import numpy as np

categories = [
    'Literary', 'Knowledge', 'Society',
    'Nature','Society','Religion','Technology','Campaign','Politics',
    'Economy','Military','Law','Health','Transportation',
    'Nation','Geography'
]	#类型
stop_words = set(stopwords.words('english'))	#停用词

"""
1、语料库获取
"""
def Corpus(sourceFile):
    #读取文本数据
    all_data = open(sourceFile,'r',encoding='utf-8').read()
    #采用正则表达式提取标签内容
    datasList = re.findall('<doc.*?>(.*?)</doc>',all_data,re.DOTALL)
    return datasList

"""
2、数据预处理
"""
def Preprocess_text(text):
	#分词、去停用词
    tokens = nltk.word_tokenize(text)
    filtered_tokens = [token_ for token_ in tokens if token_.lower() not in stop_words]
    return ' '.join(filtered_tokens)

"""
3、分类
"""
def Text_classification(texts01,dir_source01):
    train_target = [target.strip() for target in open(dir_source01, 'r', encoding='utf-8').readlines()]
    train_data = [Preprocess_text(text) for text in texts01]  # 训练集
    vectorizer = TfidfVectorizer()  # 将文本数据转换为数值特征向量,这是一个类
    # fit方法对训练数据集中的文本数据进行拟合,得到特征提取器、transform方法将文本数据转为数值特征向量
    train_features = vectorizer.fit_transform(train_data)  # 训练集特征矩阵
    classifier = MultinomialNB()  # 多项式特征的朴素贝叶斯分类器
    classifier.fit(train_features, train_target)
    return vectorizer,classifier

"""
4、测试
"""
def Test_classification(Text_vectorizer,Text_classifier,texts02,dir_source02):
    test_target = [target.strip() for target in open(dir_source02, 'r', encoding='utf-8').readlines()]
    test_data = [Preprocess_text(text) for text in texts02]  # 测试集
    test_features = Text_vectorizer.transform(test_data)
    predictions = Text_classifier.predict(test_features)  # 使用训练好的分类器对测试数据进行分类并计算准确率。
    Result = list()
    for i in range(len(test_target)):
        if test_target[i] == predictions[i]:
            Result.append(True)
        else:
            Result.append((test_target[i], predictions[i]))
    print(Result)
    accuracy = accuracy_score(test_target, predictions)	
    return accuracy

"""
5、统计
"""
def statistics(Text_vectorizer,Text_classifier,Test_texts_02):
    test_data =[Preprocess_text(text) for text in Test_texts_02]
    test_features = Text_vectorizer.transform(test_data)
    predictions = Text_classifier.predict(test_features)
    ClassifyList = list()
    for i in range(len(categories)):
        C = list(predictions).count(categories[i])
        ClassifyList.append(C)
    return test_data,predictions

# 主函数
def main():
    dir_source_01 = '../../../Datas/enwiki/AA/wiki_04_10_01.txt'	#训练数据
    dir_source_03 = '../../../Datas/enwiki/AA/Classify_04_10_02.txt'	#训练数据对应类型
    test_dir_source_02 = '../../../Datas/enwiki/AA/wiki_01_10_01_test.txt'	#测试数据
    test_dir_source_04 = '../../../Datas/enwiki/AA/Classify_01_10_01.txt'	#测试数据对应类型
    test_dir_source_05 = '../../../Datas/enwiki/AA/wiki_02_10.txt'	#带预测数据
    texts_01 = Corpus(dir_source_01)
    texts_02 = Corpus(test_dir_source_02)

    Text_vectorizer, Text_classifier = Text_classification(texts_01, dir_source_03)

    accuracy = Test_classification(Text_vectorizer, Text_classifier, texts_02, test_dir_source_04)
    print(f"Accuracy: {accuracy:.2f}")

    Test_texts_02 = Corpus(test_dir_source_05)
    test_data, predictions = statistics(Text_vectorizer, Text_classifier, Test_texts_02)
    return test_data, predictions

if __name__ =='__main__':
    test_data, predictions = main()
    print(test_data, predictions)

【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip 人工智能大作业基于TF-IDF+手写朴素贝叶斯实现文本分类python源码+实验报告.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值