中文文本分类 传统机器学习+深度学习

中文文本分类

为了完成课程要求,做了一个中文文本分类的简易系统,再此纪录。

本文项目地址:

项目地址
https://github.com/WhiteGive-Boy/ChineseTextClassification
使用到的算法:
机器学习:朴素贝叶斯 逻辑斯蒂回归 lightgbm
深度学习:CNN LSTM BERT
深度学习大部分内容使用https://github.com/649453932/Chinese-Text-Classification-Pytorch
机器学习部分自造。

数据集

为了不撞数据,github上随便找的一个头条新闻数据集,地址:https://github.com/BenDerPan/toutiao-text-classfication-dataset
数据规模共382688条,分布于15个分类中。包含的类别如下:

100 民生 故事 news_story
101 文化 文化 news_culture
102 娱乐 娱乐 news_entertainment
103 体育 体育 news_sports
104 财经 财经 news_finance
106 房产 房产 news_house
107 汽车 汽车 news_car
108 教育 教育 news_edu 
109 科技 科技 news_tech
110 军事 军事 news_military
112 旅游 旅游 news_travel
113 国际 国际 news_world
114 证券 股票 stock
115 农业 三农 news_agriculture
116 电竞 游戏 news_game

特征工程

对模型而言,数据的处理是最重要的一步,由于精力有限,我这里的处理比较粗糙,对机器学习的三个算法的数据处理大致流程:
1)由原始数据集划分训练集测试集
2)读取训练集及停用词表,选取特定维度的特征词,如10000个特征词
3)对每个句子进行向量化,向量的值表示句中出现特征词的个数
之后进行训练测试即可,以朴素贝叶斯为例:
这里的数据处理很仓促很粗糙,可以再改进,如利用TFIDF值表示特征向量而不是个数,再比如利用一些降维算法再降维筛选更有用的特征等等,还有N-Gram,预训练向量等等,可以自行尝试,我由于时间仓促所以就很简陋,但运行的结果好像传统的机器学习准确率还比较高。。。

# -*- coding: UTF-8 -*-
import os
import random
import jieba
from sklearn.naive_bayes import MultinomialNB
#import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, f1_score
from sklearn.externals import joblib

# 手写拉普拉斯修正的朴素贝叶斯
import numpy as np
import pandas as pd



"""
    函数说明:中文文本处理
    Parameters:
        path - 文本存放的路径
        test_size - 测试集占比,默认占所有数据集的百分之20
    Returns:
        all_words_list - 按词频降序排序的训练集列表
        train_data_list - 训练集列表
        test_data_list - 测试集列表
        train_class_list - 训练集标签列表
        test_class_list - 测试集标签列表
"""
def TextProcessing(path, test_size=0.2):
#    folder_list = os.listdir(folder_path)  # 查看folder_path下的文件
    data_list = []  # 数据集数据
    class_list = []  # 数据集类别
    with open(path, 'r', encoding='utf-8') as f:  # 打开txt文件
        for line in f.readlines():
            line = line.strip().split("_!_")
            # print(line)
            if (len(line) >= 5):
                strr = line[3] + line[4]
            else:
                strr = line[3]
            word_cut = jieba.cut(strr, cut_all=False)  # 精简模式,返回一个可迭代的generator
            word_list = list(word_cut)  # generator转换为list
            data_list.append(word_list)
            class_list.append(line[2])

    data_class_list = list(zip(data_list, class_list))  # zip压缩合并,将数据与标签对应压缩
    random.shuffle(data_class_list)  # 将data_class_list乱序
    index = int(len(data_class_list) * test_size) + 1  # 训练集和测试集切分的索引值
    train_list = data_class_list[index:]  # 训练集
    test_list = data_class_list[:index]  # 测试集
    train_data_list, train_class_list = zip(*train_list)  # 训练集解压缩
    test_data_list, test_class_list = zip(*test_list)  # 测试集解压缩

    all_words_dict = {}  # 统计训练集词频
    for word_list in train_data_list:
        for word in word_list:
            if word in all_words_dict.keys():
                all_words_dict[word] += 1
            else:
                all_words_dict[word] = 1

    # 根据键的值倒序排序
    all_words_tuple_list = sorted(all_words_dict.items(), key=lambda f: f[1], reverse=True)
    all_words_list, all_words_nums = zip(*all_words_tuple_list)  # 解压缩
    all_words_list = list(all_words_list)  # 转换成列表
    return all_words_list, train_data_list, test_data_list, train_class_list, test_class_list


"""
函数说明:读取文件里的内容,并去重
Parameters:
    words_file - 文件路径
Returns:
    words_set - 读取的内容的set集合
"""
def MakeWordsSet(words_file):
    words_set = set()  # 创建set集合
    with open(words_file, 'r', encoding='utf-8') as f:  # 打开文件
        for line in f.readlines():  # 一行一行读取
            word = line.strip()  # 去回车
            if len(word) > 0:  # 有文本,则添加到words_set中
                words_set.add(word)
    return words_set  # 返回处理结果


"""
函数说明:文本特征选取
Parameters:
    all_words_list - 训练集所有文本列表
    deleteN - 删除词频最高的deleteN个词
    stopwords_set - 指定的结束语
Returns:
    feature_words - 特征集
"""
def words_dict(all_words_list, deleteN, stopwords_set=set()):
    feature_words = []  # 特征列表
    n = 1
    for t in range(deleteN, len(all_words_list), 1):
        if n > 1000:  # feature_words的维度为1000
            break
            # 如果这个词不是数字,并且不是指定的结束语,并且单词长度大于1小于5,那么这个词就可以作为特征词
        if not all_words_list[t].isdigit() and all_words_list[t] not in stopwords_set and 1 < len(all_words_list[t]) < 5:
            feature_words.append(all_words_list[t])
        n += 1
    return feature_words


"""
函数说明:根据feature_words将文本向量化
Parameters:
    train_data_list - 训练集
    test_data_list - 测试集
    feature_words - 特征集
Returns:
    train_feature_list - 训练集向量化列表
    test_feature_list - 测试集向量化列表
"""
def TextFeatures(train_data_list, test_data_list, feature_words):
    def text_features(text, feature_words):  # 出现在特征集中,则置1
        text_words = set(text)
        features = [1 if word in text_words else 0 for word in feature_words]
        return features

    train_feature_list = [text_features(text, feature_words) for text in train_data_list]
    test_feature_list = [text_features(text, feature_words) for text in test_data_list]
    # for features in train_feature_list:
    #     for index in range(len(features)):
    #         features[index]=str(index)+"_"+str(features[index])
    # for features in test_feature_list:
    #     for index in range(len(features)):
    #         features[index]=str(index)+"_"+str(features[index])

    return train_feature_list, test_feature_list  # 返回结果


"""
函数说明:新闻分类器
Parameters:
    train_feature_list - 训练集向量化的特征文本
    test_feature_list - 测试集向量化的特征文本
    train_class_list - 训练集分类标签
    test_class_list - 测试集分类标签
Returns:
    test_accuracy - 分类器精度
"""


if __name__ == '__main__':
    # 文本预处理
    folder_path = "./toutiao.txt"  # 训练集存放地址
    all_words_list, train_data_list, test_data_list, train_class_list, test_class_list = TextProcessing(folder_path,test_size=0.2)
    # 生成stopwords_set
    stopwords_file = './stopwords_cn.txt'
    stopwords_set = MakeWordsSet(stopwords_file)

    test_accuracy_list = []

    clf=MultinomialNB()

    id2class=['news_finance', 'news_story', 'news_travel', 'news_edu', 'news_military', 'news_game', 'news_agriculture', 'news_house', 'news_sports', 'news_car', 'news_tech', 'stock', 'news_entertainment', 'news_culture', 'news_world']

    class2id = {}
    index = 0
    for i in id2class:
        class2id[i] = index
        index = index + 1
    # print(id2class)
    train_class_list=[class2id[i] for i in train_class_list]
    test_class_list = [class2id[i] for i in test_class_list]
    feature_words = words_dict(all_words_list, 450, stopwords_set)
    a = np.array(feature_words)
    np.save("./feature_words.npy", a)  # 保存为.npy格式


    #print(feature_words)
    train_feature_list, test_feature_list = TextFeatures(train_data_list, test_data_list, feature_words)
    print(train_feature_list[0])
    print(train_class_list[0])
    clf.fit(train_feature_list,train_class_list)

    joblib.dump(clf, "./bayes.m")

    predict_y=clf.predict(test_feature_list)
    print(classification_report(test_class_list, predict_y, target_names=id2class))
    # acc = TextClassifier(train_feature_list, test_feature_list, train_class_list, test_class_list,c1)
    # #print(c1.cc)
    # #print(c1.fc)
    # print("acc:",acc)
    # #print("predict lable:",lable)

深度学习部分

这部分内容大致参考上面的git进行实现,其代码很简洁,可自行学习吧。。。。

对比及结果

算法对比
结果
运行的结果好像传统的机器学习准确率比较高,分析原因可能是因为我们选取了更多的特征词,以及以个数进行向量表示更有利于传统的机器学习算法去

展示界面

为了程序展示,勉强写了个界面,凑合能看,有时间了再搞个花哨的吧
界面

总结

为了完成课程设计做的一些工作,总的来说还是比较菜,算是入门级,后期有时间了在数据处理方面会再做研究。

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值