1 加载数据
import pandas as pd
import jieba
val_path = '/Users/haiwangluo/Downloads/人工智能全套/自然语言处理(Python版)/第六章:贝叶斯算法-新闻分类任务/贝叶斯Python文本分析/Python文本分析/data/val.txt'
df_news = pd.read_table(val_path,names=['category','theme','URL','content'],encoding='utf-8')
df_news = df_news.dropna()
df_news.head()
category theme URL content
0 汽车 新辉腾 4.2 V8 4座加长Individual版2011款 最新报价 http://auto.data.people.com.cn/model_15782/ 经销商 电话 试驾/订车U憬杭州滨江区江陵路1780号4008-112233转5864#保常...
1 汽车 918 Spyder概念车 http://auto.data.people.com.cn/prdview_165423.... 呼叫热线 4008-100-300 服务邮箱 kf@peopledaily.com.cn
2 汽车 日内瓦亮相 MINI性能版/概念车-1.6T引擎 http://auto.data.people.com.cn/news/story_5249... MINI品牌在二月曾经公布了最新的MINI新概念车Clubvan效果图,不过现在在日内瓦车展...
3 汽车 清仓大甩卖一汽夏利N5威志V2低至3.39万 http://auto.data.people.com.cn/news/story_6144... 清仓大甩卖!一汽夏利N5、威志V2低至3.39万=日,启新中国一汽强势推出一汽夏利N5、威志...
4 汽车 大众敞篷家族新成员 高尔夫敞篷版实拍 http://auto.data.people.com.cn/news/story_5686... 在今年3月的日内瓦车展上,我们见到了高尔夫家族的新成员,高尔夫敞篷版,这款全新敞篷车受到了众...
df_news.shape
(5000, 4)
2 数据预处理
2.1 使用jieba分词器
content = df_news.content.values.tolist()
print(content[1000])
阿里巴巴集团昨日宣布,将在集团管理层面设立首席数据官岗位(Chief Data Officer),阿里巴巴B2B公司CEO陆兆禧将会出任上述职务,向集团CEO马云直接汇报。>菹ぃ和6月初的首席风险官职务任命相同,首席数据官亦为阿里巴巴集团在完成与雅虎股权谈判,推进“one company”目标后,在集团决策层面新增的管理岗位。0⒗锛团昨日表示,“变成一家真正意义上的数据公司”已是战略共识。记者刘夏
content_S = []
for line in content:
current_segment = jieba.lcut(line)
if len(current_segment) > 1 and current_segment != '\r\n':
content_S.append(current_segment)
content_S[1000][:10]
['阿里巴巴', '集团', '昨日', '宣布', ',', '将', '在', '集团', '管理', '层面']
df_content=pd.DataFrame({
'content_S':content_S})
df_content.head()
content_S
0 [经销商, , 电话, , 试驾, /, 订车, U, 憬, 杭州, 滨江区, 江陵, ...
1 [呼叫, 热线, , 4, 0, 0, 8, -, 1, 0, 0, -, 3, 0, 0...
2 [M, I, N, I, 品牌, 在, 二月, 曾经, 公布, 了, 最新, 的, M, I...
3 [清仓, 大, 甩卖, !, 一汽, 夏利, N, 5, 、, 威志, V, 2, 低至, ...
4 [在, 今年, 3, 月, 的, 日内瓦, 车展, 上, ,, 我们, 见到, 了, 高尔夫...
2.2 去掉停用词
stopwords_path = '/Users/haiwangluo/Downloads/人工智能全套/自然语言处理(Python版)/第六章:贝叶斯算法-新闻分类任务/贝叶斯Python文本分析/Python文本分析/stopwords.txt'
stopwords = pd.read_csv(stopwords_path,index_col=False,sep="\t",quoting=3,names=['stopword'], encoding='utf-8')
def drop_stopwords(