使用机器学习完成中文文本分类

文章参考:https://blog.csdn.net/weixin_40924580/article/details/83049592
https://blog.csdn.net/ywangjiyl/article/details/107546396
机器学习致力于研究如何通过计算的手段,利用经验来改善系统自身的性能。机器学习通过历史数据训练出模型,然后利用学习获得的模型对新数据进行预测

机器学习模型

机器学习是对能通过经验自动改进的计算机算法的研究。机器学习通过历史数据训练出模型对应于人类对经验进行归纳的过程,机器学习利用模型对新数据进行预测对应于人类利用总结的规律对新问题进行预测的过程。

机器学习有很多种分支,对于学习者来说应该优先掌握机器学习算法的分类,然后再其中一种机器学习算法进行学习。由于机器学习算法的分支和细节实在是太多,所以如果你一开始就被细节迷住了眼,你就很难知道全局是什么情况的。
学习目标
1、学会TF-IDF的原理和使用
2、使用sklearn的机器学习模型完成文本分类
数据集来自七月在线练习

import jieba
import pandas as pd
import random
from sklearn.model_selection import train_test_split                #划分训练/测试集
from sklearn.feature_extraction.text import CountVectorizer         #抽取特征
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfVectorizer
'''
读入数据
'''
df_technology = pd.read_csv("H:/NLP_project/NLP_project/data/technology_news.csv")
df_technology = df_technology.dropna()

df_car = pd.read_csv("H:/NLP_project/NLP_project/data/car_news.csv")
df_car = df_car.dropna()

df_entertainment = pd.read_csv("H:/NLP_project/NLP_project/data/entertainment_news.csv")
df_entertainment = df_entertainment.dropna()

df_military = pd.read_csv("H:/NLP_project/NLP_project/data/military_news.csv")
df_military = df_military.dropna()

df_sports = pd.read_csv("H:/NLP_project/NLP_project/data/sports_news.csv")
df_sports = df_sports.dropna()

'''
数据预处理
'''
technology =df_technology.content.values.tolist()[1000:21000]
car =df_car.content.values.tolist()[1000:21000]
entertainment=df_entertainment.content.values.tolist()[:20000]
military = df_military.content.values.tolist()[:20000]
sports = df_sports.content.values.tolist()[:20000]              #个类别随机抽取20000个数

#停用词
stopwords = pd.read_csv('H:/NLP_project/NLP_project/data/stopwords.txt',index_col=False,quoting=3,sep="\t",names=['stopword'])
stopwords = stopwords['stopword'].values


def preprocess_text(content_lines,sentences,category):
    for line in content_lines:
            segs = jieba.lcut(line)
            segs = filter(lambda x:len(x)>1,segs)               #过滤长度小于1的字符
            segs = filter(lambda x:x not in stopwords,segs)    #去停用词
            sentences.append((" ".join(segs),category))
    return sentences

sentences = []
preprocess_text(technology, sentences, 'technology')
preprocess_text(car, sentences, 'car')
preprocess_text(entertainment, sentences, 'entertainment')
preprocess_text(military, sentences, 'military')
preprocess_text(sports, sentences, 'sports')

random.shuffle(sentences)

x,y = zip(*sentences)           #将sentence中的内容和标签分别赋值给x,y
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=1234)         #利用模型划分测试集和验证集

使用CountVectorizer进行特征提取,使用MultinomialNB分类训练

vec = CountVectorizer(                 #特征提取
    analyzer='word',
    ngram_range=(1,4),                 #词特征变为*,**,***
    max_features=20000)                #对文本抽取词袋模型特征
vec.fit(x_train)                       #从训练集fit特征

'''
使用贝叶斯分类器训练,结果为0.87424
'''
classfier = MultinomialNB()
classfier.fit(vec.transform(x_train),y_train)
print(classfier.score(vec.transform(x_test),y_test))


使用TF-IDF进行特征提取:

vec=TfidfVectorizer(analyzer='word', ngram_range=(1,4), max_features=20000)
vec.fit(x_train)                       #从训练集fit特征

'''
使用TF-IDF提取特征,结果为0.8755
'''
classfier = MultinomialNB()
classfier.fit(vec.transform(x_train),y_train)
print(classfier.score(vec.transform(x_test),y_test))

更换训练模型,使用SVM训练,得到结果为:0.8851,测试时间相对较长

vec=TfidfVectorizer(analyzer='word', ngram_range=(1,4), max_features=20000)
vec.fit(x_train)                       #从训练集fit特征

'''
使用TF-IDF提取特征,使用SVM训练,结果为0.8851
'''
classfier = SVC(kernel='linear')
classfier.fit(vec.transform(x_train),y_train)
print(classfier.score(vec.transform(x_test),y_test))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值