美团店铺评价语言处理以及分类(NLP)

  • 第一篇 数据分析部分
  • 第二篇 可视化部分,
  • 本文是该系列第三篇,文本分类
  • 主要用到的包有jieba,sklearn,pandas,本篇博文主要先用的是词袋模型(bag of words),将文本以数值特征向量的形式来表示(每个文档构建一个特征向量,有很多的0,出现在特征向量中的值也叫做原始词频,tf(term frequency), 得到的矩阵为稀疏矩阵)
  • 后续的算法模型会陆续进行构建

导入数据分析常用库

import pandas as pd
import numpy as np
  • 读取文件
df=pd.read_excel("all_data_meituan.xlsx")[["comment","star"]]
df.head()

  • 查看DataFrame的大小
df.shape
(17400, 2)
df['sentiment']=df['star'].apply(lambda x:1 if x>30 else 0)
df=df.drop_duplicates() ## 去掉重复的评论,剩余的文本1406条,我们将数据复制为原有数据的三倍
df=df.dropna()
X=pd.concat([df[['comment']],df[['comment']],df[['comment']]])
y=pd.concat([df.sentiment,df.sentiment,df.sentiment])
X.columns=['comment']
X.reset_index
X.shape
(3138, 1)

import jieba # 导入分词库
def chinese_word_cut(mytext):
return " ".join(jieba.cut(mytext))
X['cut_comment']=X["comment"].apply(chinese_word_cut)
X['cut_comment'].head()
Building prefix dict from the default dictionary ...
DEBUG:jieba:Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\HUANG_~1\AppData\Local\Temp\jieba.cache
DEBUG:jieba:Loading model from cache C:\Users\HUANG_~1\AppData\Local\Temp\jieba.cache
Loading model cost 0.880 seconds.
DEBUG:jieba:Loading model cost 0.880 seconds.
Prefix dict has been built succesfully.
DEBUG:jieba:Prefix dict has been built succesfully. 0 还行 吧 , 建议 不要 排队 那个 烤鸭 和 羊肉串 , 因为 烤肉 时间 本来 就 不够...
1 去过 好 几次 了 东西 还是 老 样子 没 增添 什么 新花样 环境 倒 是 ...
2 一个 字 : 好 ! ! ! # 羊肉串 # # 五花肉 # # 牛舌 # ...
3 第一次 来 吃 , 之前 看过 好多 推荐 说 这个 好吃 , 真的 抱 了 好 大 希望 ...
4 羊肉串 真的 不太 好吃 , 那种 说 膻 不 膻 说 臭 不 臭 的 味 。 烤鸭 还 行...
Name: cut_comment, dtype: object
  • 导入sklearn中的数据分割模块,设定test数据集大小,shuffle默认Ture
from sklearn.model_selection import  train_test_split
X_train,X_test,y_train,y_test= train_test_split(X,y,random_state=42,test_size=0.25)
  • 获取停用词
def get_custom_stopwords(stop_words_file):
with open(stop_words_file,encoding="utf-8") as f:
custom_stopwords_list=[i.strip() for i in f.readlines()]
return custom_stopwords_list
stop_words_file = "stopwords.txt"
stopwords = get_custom_stopwords(stop_words_file) # 获取停用词
  • 导入词袋模型
from sklearn.feature_extraction.text import  CountVectorizer
vect=CountVectorizer() # 实例化
vect # 查看参数
CountVectorizer(analyzer='word', binary=False, decode_error='strict',
dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None, stop_words=None,
strip_accents=None, token_pattern='(?u)\\b\\w\\w+\\b',
tokenizer=None, vocabulary=None)
# dir(vect)  # 查看vect的属性
  • 将分割后的文本进行fit_transform,矩阵大小为2353*1965
vect.fit_transform(X_train["cut_comment"])
<2353x1965 sparse matrix of type '<class 'numpy.int64'>'
with 20491 stored elements in Compressed Sparse Row format>
vect.fit_transform(X_train["cut_comment"]).toarray().shape
(2353, 1965)
pd.DataFrame(vect.fit_transform(X_train["cut_comment"]).toarray(),columns=vect.get_feature_names()).iloc[:,0:25].head()
# print(vect.get_feature_names())
# 数据维数1956,不算很大(未使用停用词)
# 将其转化为DataFrame
  • 发现其中有很多的数字以及无效特征,随后传入实例化参数的同时,加入正则匹配取出这些无意义特征,同时取出停用词

vect = CountVectorizer(token_pattern=u'(?u)\\b[^\\d\\W]\\w+\\b',stop_words=frozenset(stopwords)) # 去除停用词,匹配以数字开头的非单词字符
pd.DataFrame(vect.fit_transform(X_train['cut_comment']).toarray(), columns=vect.get_feature_names()).head()
# 1691 columns,去掉以数字为特征值的列,减少了近三百列,由1965减小到1691
# max_df = 0.8 # 在超过这一比例的文档中出现的关键词(过于平凡),去除掉(可以自行设定)
# min_df = 3 # 在低于这一数量的文档中出现的关键词(过于独特),去除掉。(可以自行设定)
  • 取出数字特征之后

模型构建

  • 从sklearn 朴素贝叶斯中导入多维贝叶斯
  • 朴素贝叶斯通常用来处理文本分类垃圾短信,速度飞快,效果一般都不会差很多
  • MultinomialNB类可以选择默认参数,如果模型预测能力不符合要求,可以适当调整
from sklearn.naive_bayes import MultinomialNB
nb=MultinomialNB()
from sklearn.pipeline import make_pipeline # 导入make_pipeline方法
pipe=make_pipeline(vect,nb)
pipe.steps # 查看pipeline的步骤(与pipeline相似)
[('countvectorizer',
CountVectorizer(analyzer='word', binary=False, decode_error='strict',
dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None,
stop_words=frozenset({'', '范围', '但愿', 'vs', '为', '过去', '集中', '这般', '孰知', '认为', '论', '36', '前后', '每年', '长期以来', 'our', '要不', '使用', '好象', 'such', '不但', '一下', 'how', '召开', '6', '全体', '严格', '除开', 'get', '可好', '毕竟', 'but', '如前所述', '满足', 'your', 'keeps', '只', '大抵', '己', 'concerning', "they're", '再则', '有意的'...'reasonably', '绝对', '咧', '除此以外', '50', '得了', 'seeming', '只是', '背靠背', '弗', 'need', '其', '第二', '再者说'}),
strip_accents=None, token_pattern='(?u)\\b[^\\d\\W]\\w+\\b',
tokenizer=None, vocabulary=None)),
('multinomialnb', MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True))]
pipe.fit(X_train.cut_comment, y_train)
Pipeline(memory=None,
steps=[('countvectorizer', CountVectorizer(analyzer='word', binary=False, decode_error='strict',
dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None,
stop_words=...e, vocabulary=None)), ('multinomialnb', MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True))])

测试集预测结果

y_pred = pipe.predict(X_test.cut_comment)
# 对测试集进行预测(其中包括了转化以及预测)
# 模型对于测试集的准确率
from sklearn import metrics
metrics.accuracy_score(y_test,y_pred)
0.82929936305732488
# 模型对于测试集的混淆矩阵
metrics.confusion_matrix(y_test,y_pred)
# 测试集中的预测结果:真阳性474个,假阳性112个,假阴性22个,真阴性为177个
array([[177, 112],
[ 22, 474]], dtype=int64)
def get_confusion_matrix(conf,clas):
import matplotlib.pyplot as plt
fig,ax=plt.subplots(figsize=(2.5,2.5))
ax.matshow(conf,cmap=plt.cm.Blues,alpha=0.3)
tick_marks = np.arange(len(clas))
plt.xticks(tick_marks,clas, rotation=45)
plt.yticks(tick_marks, clas)
for i in range(conf.shape[0]):
for j in range(conf.shape[1]):
ax.text(x=i,y=j,s=conf[i,j],
va='center',
ha='center')
plt.xlabel("predict_label")
plt.ylabel("true label")
conf=metrics.confusion_matrix(y_test,y_pred)
class_names=np.array(['0','1'])
get_confusion_matrix(np.array(conf),clas=class_names)
plt.show()

对整个数据集进行预测分类

y_pred_all = pipe.predict(X['cut_comment'])
metrics.accuracy_score(y,y_pred_all)
# 对于整个样本集的预测正确率,整个数据集的准确率高于测试集,说明有些过拟合
0.85659655831739967
metrics.confusion_matrix(y,y_pred_all)
# 真个数据集的混淆矩阵
array([[ 801,  369],
[ 81, 1887]], dtype=int64)
y.value_counts()
# 初始样本中 正类与负类的数量
1    1968
0 1170
Name: sentiment, dtype: int64
metrics.f1_score(y_true=y,y_pred=y_pred_all)
# f1_score 评价模型对于真个数据集
0.89346590909090906
metrics.recall_score(y, y_pred_all)
# 检出率,也就是正类总样本检出的比例 真正/假阴+真正
0.95884146341463417
metrics.precision_score(y, y_pred_all)
# 准确率, 检测出的来正类中真正类的比例 真正/假阳+真正
0.83643617021276595
print(metrics.classification_report(y, y_pred_all))
# 分类报告
             precision    recall  f1-score   support

      0       0.91      0.68      0.78      1170
1 0.84 0.96 0.89 1968 avg / total 0.86 0.86 0.85 3138

基于pandas python sklearn 的美团某商家的评论分类(文本分类)的更多相关文章

  1. 基于pandas python的美团某商家的评论销售数据分析(可视化)

    基于pandas python的美团某商家的评论销售数据分析 第一篇 数据初步的统计 本文是该可视化系列的第二篇 第三篇 数据中的评论数据用于自然语言处理 导入相关库 from pyecharts i ...

  2. 美团店铺评价语言处理以及文本分类(logistic regression)

    美团店铺评价语言处理以及分类(LogisticRegression) 第一篇 数据清洗与分析部分 第二篇 可视化部分, 第三篇 朴素贝叶斯文本分类 本文是该系列的第四篇 主要讨论逻辑回归分类算法的参数 ...

  3. 基于pandas python的美团某商家的评论销售(数据分析)

    数据初步的分析 本文是该系列的第一篇 数据清洗 数据初步的统计 第二篇 数据可视化 第三篇 数据中的评论数据用于自然语言处理 from pyecharts import Bar,Pie import ...

  4. 基于Naive Bayes算法的文本分类

    理论 什么是朴素贝叶斯算法? 朴素贝叶斯分类器是一种基于贝叶斯定理的弱分类器,所有朴素贝叶斯分类器都假定样本每个特征与其他特征都不相关.举个例子,如果一种水果其具有红,圆,直径大概3英寸等特征,该水果 ...

  5. python数据挖掘第三篇-垃圾短信文本分类

    数据挖掘第三篇-文本分类 文本分类总体上包括8个步骤.数据探索分析->数据抽取->文本预处理->分词->去除停用词->文本向量化表示->分类器->模型评估.重 ...

  6. 基于Spark Mllib的文本分类

    基于Spark Mllib的文本分类 文本分类是一个典型的机器学习问题,其主要目标是通过对已有语料库文本数据训练得到分类模型,进而对新文本进行类别标签的预测.这在很多领域都有现实的应用场景,如新闻网站 ...

  7. ubantu下安装pip,python,pycharm,numpy,scipy,matplotlibm,pandas 以及sklearn

    ubuntu 安装 pip 及 pip 常用命令: https://blog.csdn.net/danielpei1222/article/details/62969815 ubuntu下不同版本py ...

  8. python – 基于pandas中的列中的值从DataFrame中选择行

    如何从基于pandas中某些列的值的DataFrame中选择行?在SQL中我将使用: select * from table where colume_name = some_value. 我试图看看 ...

  9. 用python+sklearn(机器学习)实现天气预报数据 数据

    用python+sklearn机器学习实现天气预报 数据 项目地址 系列教程 勘误表 0.前言 1.爬虫 a.确认要被爬取的网页网址 b.爬虫部分 c.网页内容匹配取出部分 d.写入csv文件格式化 ...

随机推荐

  1. Ubuntu下使用dialog制作菜单执行简单脚本

    新建test5 #!/bin/bash #using select in the menu temp=$(mktemp -t test.XXXXXX) temp2=$(mktemp -t test2. ...

  2. Python游戏《外星人入侵》来了~

    在游戏<外星人入侵>中,玩家控制着一艘最初出现在屏幕底部中央的飞船.玩家可以使用箭头键左右移动飞船,还可使用空格键进行射击.游戏开始时,一群外星人出现在天空中,他们在屏幕中向下移动.玩家的 ...

  3. java字符集

    在utf-8编码中,unicode(编码字符集)是utf-8(字符编码)的表现形式 http://www.cnblogs.com/hanruyue/p/5859107.html

  4. ActiveMq持久化数据

    A:持久化为文件 这个你装ActiveMQ时默认就是这种,只要你设置消息为持久化就可以了.涉及到的配置和代码有 <persistenceAdapter> <kahaDB direct ...

  5. go搭建web服务

    https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/preface.md

  6. flume 增量上传日志文件到HDFS中

    1.采集日志文件时一个很常见的现象 采集需求:比如业务系统使用log4j生成日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs中. 1.1.根据需求,首先定义一下3大要素: 采集源 ...

  7. 双调旅行商问题 (Bitonic TSP)

    问题描写叙述: 上述问题能够使用动态规划的方法来解决. 以下是解决思路的详细介绍: 1. 最优子结构: 如果d[i][j]表示从起点1出发到达i及j两个顶点的最短路程之和. 为此能够如果K为此段路程上 ...

  8. scala spray 概念性内容的总结

    spray 是基于 akka 的轻量级 scala 库,可用于编写 REST API 服务.了解 spray 的 DSL 后可以在很短的时间内写出一个 REST API 服务,它的部署并不需要 tom ...

  9. Web实时通信之Socket.IO

    前面两篇文章使用了Ajax long polling和WebSocket两种常用的Web实时通信方式构建了简单的聊天程序. 但是,由于浏览器的兼容问题,不是所有的环境都可以使用WebSocket这种比 ...

  10. ios开发之--所有设备的屏幕尺寸

    所有设备型号官网地址:https://www.theiphonewiki.com/wiki/Models iPhone: 机型 像素 比例 像素密度 屏幕尺寸 机型代码 发布日期 iPhone 2g ...