sohu_news搜狐新闻类型分类
数据获取
- 数据是从搜狐新闻开放的新闻xml数据,经过一系列的处理之后,生成的一个excel文件
- 该xml文件的处理有单独的处理过程,就是用pandas处理,该过程在此省略
import numpy as np
import pandas as pd
- 读取新闻文本文件,查看文本的长度
df=pd.read_excel('sohu_data.xlsx')
df['length']=df['content'].apply(lambda x: len(x)).values
- 去掉长度小于50的文本
df_data = df[df['length']>=50][['content','category']]
- 查看新闻类型的分布,共9类
df_data['category'].value_counts()
# 可以看到这里面存在类别不平衡,最大的差距有17倍。
health      30929
news        27613
auto        22841
stock       18152
it          13875
yule        13785
women        4667
book         4411
business     1769
Name: category, dtype: int64
- 使用sklearn中的处理模块的labelEncoder方法对类标进行处理
from sklearn.preprocessing import LabelEncoder
class_le=LabelEncoder()
class_le.fit(np.unique(df['category'].values)
print(list(class_le.classes_))
y=class_le.transform(df['category'].values)
# 查看前20个新闻样本的类别
y[:20]
['auto', 'book', 'business', 'health', 'it', 'news', 'stock', 'women', 'yule']
array([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], dtype=int64)
- 导入jieba,进行分词
import jieba
def chinese_word_cut(mytext):
    return " ".join(jieba.cut(mytext))
X=pd.DataFrame()
X['cut_content']=df["content"].apply(chinese_word_cut)
X['cut_content'].head()
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\HUANG_~1\AppData\Local\Temp\jieba.cache
Loading model cost 0.966 seconds.
Prefix dict has been built succesfully.
1    产品名称 :  规格 及 价格 : 3 0 m l / 3 0 0   元  羽西 当归...
2    常见问题  Q : 为什么 我 提交 不了 试用 申请   A : 试用 申请 必须 同时...
3    产品名称 : 肌醇 ( P u r e   S k i n ) 深层 卸妆 凝胶  规格 ...
4    欧诗漫 的 试用装 终于 延期 而 至 , 果然 不负 所望 , 包装 很 精美 。 从 快...
5    试用 申请 步骤  1 注册 并 完善 个人资料   登入 搜狐 试用 频道 , 填写 并...
Name: cut_content, dtype: object
- 使用词袋模型进行文本处理,去除停用词,去除数字特征,使用朴素贝叶斯进行分类
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(token_pattern=u'(?u)\\b[^\\d\\W]\\w+\\b',stop_words=frozenset(stopwords))
from sklearn.naive_bayes import MultinomialNB
nb=MultinomialNB()
from sklearn.pipeline import make_pipeline
pipe=make_pipeline(vect,nb)
pipe.fit(X_train.cut_content, y_train)
y_pred = pipe.predict(X_test.cut_content)
from sklearn import  metrics
print(metrics.accuracy_score(y_test,y_pred))
metrics.confusion_matrix(y_test,y_pred)
0.897216216938
array([[6266,  163,    2,  249,    5,  345,   66,   74,   53],
       [   5, 1118,    0,    0,    0,   31,    2,    5,   37],
       [   8,    4,   15,    0,    0,  104,  329,    5,    3],
       [   4,    1,    0, 8230,    0,   64,    6,    1,    0],
       [  59,   29,    0,   10, 3672,   66,   29,   26,   45],
       [  72,   71,    6,   26,    1, 5683,  756,   60,  193],
       [  28,    0,   10,    0,    0,  381, 4275,    0,    2],
       [   9,   90,    0,    5,    1,   74,    5,  890,  132],
       [   2,   38,    1,    2,    0,   44,    1,   11, 3467]], dtype=int64)
- 可以看到朴素贝叶斯对该测试集的正确率达到了接近90% 
- 对训练集进行评估,正确率91% 
y_pred = pipe.predict(X_train.cut_content)
from sklearn import  metrics
print(metrics.accuracy_score(y_train,y_pred))
0.913158362989
from sklearn.linear_model import LogisticRegression
- 随后我们使用逻辑回归模型进行拟合模型并对测试集进行预测,测试集准确率达到94%
lr=LogisticRegression()
from sklearn.pipeline import make_pipeline
pipe=make_pipeline(vect,lr)
pipe.fit(X_train.cut_content, y_train)
y_pred = pipe.predict(X_test.cut_content)
from sklearn import  metrics
print(metrics.accuracy_score(y_test,y_pred))
metrics.confusion_matrix(y_test,y_pred)
0.944644620599
array([[7079,    3,    3,    5,   19,   62,   27,   10,   15],
       [  43, 1131,    1,    0,    3,    3,    4,    6,    7],
       [  16,    0,   36,    1,    1,  106,  296,    7,    5],
       [   7,    0,    0, 8298,    0,    1,    0,    0,    0],
       [  48,    1,    0,    0, 3870,    9,    2,    1,    5],
       [  60,   12,   22,   14,    9, 6453,  218,   35,   45],
       [  36,    1,  140,    0,    7,  415, 4090,    3,    4],
       [  48,   28,    1,    1,   10,   54,    6, 1008,   50],
       [  44,   12,    0,    1,   10,   38,    4,   29, 3428]], dtype=int64)
from sklearn.tree import DecisionTreeClassifier
tree=DecisionTreeClassifier(criterion='entropy',random_state=1)
from sklearn.ensemble import BaggingClassifier
bag=BaggingClassifier(base_estimator=tree,
                     n_estimators=10,
                     max_samples=1.0,
                     max_features=1.0,
                     bootstrap=True,
                     bootstrap_features=False,
                     n_jobs=4,random_state=1)
pipe=make_pipeline(vect,bag)
pipe.fit(X_train.cut_content, y_train)
y_pred = pipe.predict(X_test.cut_content)
metrics.accuracy_score(y_test,y_pred)
- 使用bagging的方法将决策树进行ensemble,得到的准确率比逻辑回归低了1%
 0.9294045426642111
- 通过对混淆矩阵进行分析,我们发现对第三类,也就是business类的误分类较多,后续需要改进的模型
- 可以使用td-idf进行文本特征处理
- word2vector与深度学习的方式进行结合,测试文本分类效果
- LSTM
- embedding
- 其他NLP 方法
 
sohu_news搜狐新闻类型分类的更多相关文章
- 基于jieba,TfidfVectorizer,LogisticRegression进行搜狐新闻文本分类
		一.简介 此文是对利用jieba,word2vec,LR进行搜狐新闻文本分类的准确性的提升,数据集和分词过程一样,这里就不在叙述,读者可参考前面的处理过程 经过jieba分词,产生24000条分词结果 ... 
- 利用jieba,word2vec,LR进行搜狐新闻文本分类
		一.简介 1)jieba 中文叫做结巴,是一款中文分词工具,https://github.com/fxsjy/jieba 2)word2vec 单词向量化工具,https://radimrehurek ... 
- 使用百度NLP接口对搜狐新闻做分类
		一.简介 本文主要是要利用百度提供的NLP接口对搜狐的新闻做分类,百度对NLP接口有提供免费的额度可以拿来练习,主要是利用了NLP里面有个文章分类的功能,可以顺便测试看看百度NLP分类做的准不准.详细 ... 
- 【NLP】3000篇搜狐新闻语料数据预处理器的python实现
		3000篇搜狐新闻语料数据预处理器的python实现 白宁超 2017年5月5日17:20:04 摘要: 关于自然语言处理模型训练亦或是数据挖掘.文本处理等等,均离不开数据清洗,数据预处理的工作.这里 ... 
- 利用朴素贝叶斯分类算法对搜狐新闻进行分类(python)
		数据来源 https://www.sogou.com/labs/resource/cs.php介绍:来自搜狐新闻2012年6月—7月期间国内,国际,体育,社会,娱乐等18个频道的新闻数据,提供URL ... 
- 搜狗输入法弹出搜狐新闻的解决办法(sohunews.exe)
		狗输入法弹出搜狐新闻的解决办法(sohunews.exe) 1.找到搜狗输入法的安装目录(一般是C:\program files\sougou input\版本号\)2.右键点击sohunews.ex ... 
- 利用搜狐新闻语料库训练100维的word2vec——使用python中的gensim模块
		关于word2vec的原理知识参考文章https://www.cnblogs.com/Micang/p/10235783.html 语料数据来自搜狐新闻2012年6月—7月期间国内,国际,体育,社会, ... 
- 搜狐新闻APP是如何使用HUAWEI DevEco IDE快速集成HUAWEI HiAI Engine
		6月12日,搜狐新闻APP最新版本在华为应用市场正式上线啦! 那么,这一版本的搜狐新闻APP有什么亮点呢? 先抛个图,来直接感受下——  模糊图片,瞬间清晰! 效果杠杠的吧. 而藏在这项神操作背后的 ... 
- 世界更清晰,搜狐新闻客户端集成HUAWEI HiAI 亮相荣耀Play发布会!
		6月6日,搭载有“很吓人”技术的荣耀Play正式发布,来自各个领域的大咖纷纷为新机搭载的惊艳技术站台打call,其中,搜狐公司董事局主席兼首席执行官张朝阳揭秘:华为和搜狐新闻客户端在硬件AI方面做 ... 
随机推荐
- bayer格式
			1 图像bayer格式介绍 bayer格式图片是伊士曼·柯达公司科学家Bryce Bayer发明的,Bryce Bayer所发明的拜耳阵列被广泛运用数字图像. 对于彩色图像,需要采集多种最基本的颜色, ... 
- 【.NET线程--进阶(一)】--线程方法详解
			上篇博客从线程的基本概况开始着重讨论了线程,进程,程序之间的区别,然后讨论了线程操作的几个类,并通过实例来说明了线程的创建方法.本篇博客将会带大家更深入的了解线程,介绍线程的基本方法,并通过一个Dem ... 
- Kafka与常见消息队列的对比
			Kafka与常见消息队列的对比 RabbitMQ Erlang编写 支持很多的协议:AMQP,XMPP, SMTP, STOMP 非常重量级,更适合于企业级的开发 发送给客户端时先在中心队列排队.对路 ... 
- js处理时间时区问题
			问题背景:服务器时间是东八区时间,页面会在全世界各地,页面 JS 功能需要对比服务器时间和用户本地时间,为兼容世界各地时间,需要将用户本地时间转换为东八区时间 一.基本概念 1.格林威治时间 格林威治 ... 
- Always run a program in administrator mode in Windows 10
			From: https://www.cnet.com/how-to/always-run-a-program-in-administrator-mode-in-windows-10/ If you'r ... 
- WPF背景图
			方法一:xaml中:<控件> <控件.Background><ImageBrush ImageSource="/程序集;component/images/xx ... 
- Java调用Elasticsearch API查询及matchPhraseQuery和matchQuery的区别
			一.引入依赖 <!--Elasticsearch client--> <!-- https://mvnrepository.com/artifact/org.elasticsearc ... 
- 适配器模式在Android中的应用
			工资翻倍篇之适配器模式 先了解一下适配器的基本概念,然后再详细分析一些适配器的样例,最后通过Android开发中经常使用的适配器模式进行分析,保证对适配器模式理解透彻. 适配器模式可分为三类:类的适配 ... 
- Java定时任务示例
			package com.my.timer; import java.util.Date; import java.util.TimerTask; public class myTask extends ... 
- mysql 编译安装 window篇
			传送门 # mysql下载地址 https://www.mysql.com/downloads/ # 找到MySQL Community Edition (GPL) https://dev.mysql ... 
