Python数据挖掘-词频统计-实现,,词频:某个词在该文档
Python数据挖掘-词频统计-实现,,词频:某个词在该文档
词频:某个词在该文档中出现的内容
1、语料库搭建
import jiebajieba.load_userdict("D:\\Python\\Python数据挖掘\\Python数据挖掘实战课程课件\\2.2\\金庸武功招式.txt")import osimport os.pathimport codecsfilePaths=[]fileContents=[]for root,dirs,files in os.walk("D:\\Python\\Python数据挖掘\\Python数据挖掘实战课程课件\\2.2\\SogouC.mini\\Sample"): for name in files: filePath=os.path.join(root,name) filePaths.append(filePath) f=codecs.open(filePath,"r","utf-8") fileContent=f.read() f.close() fileContents.append(fileContent) import pandascorpos=pandas.DataFrame({ "filePath":filePaths, "fileContent":fileContents})#分词来源哪个文章import jiebasegments=[]filePaths=[]for index,row in corpos.iterrows(): filePath=row["filePath"] fileContent=row["fileContent"] segs=jieba.cut(fileContent) for seg in segs: segments.append(seg) filePaths.append(filePath) segmentDataFrame=pandas.DataFrame({ "segment":segments, "filepath":filePaths})
2、词频统计
import numpy#进行词频统计#by是要分组的列,[]是要统计的列segStat=segmentDataFrame.groupby( by="segment" )["segment"].agg({ "计数":numpy.size }).reset_index().sort(columns=["计数"], #重新设置索引,再根据计数进行逆序排序 ascending=False)
by=[“列名”]后面跟着的是要分组的列,根据方括号里面的列的内容来进行统计;
第二个[]是要统计的列,在分组的列的基础上进行统计的列,可以是它自己本身
3、移除停用词,由于统计的词语很多是我们不需要的,所以需要移除
stopwords=pandas.read_csv( "D:\\Python\\Python数据挖掘\\Python数据挖掘实战课程课件\\2.3\\StopwordsCN.txt", #改文件中包含停用词 encoding="utf-8", index_col=False)fSegStat=segStat[ ~segStat.segment.isin(stopwords.stopword)]
所用方法为isin(),然后在取反~
第二种分词方法:
import jiebasegments=[]filePaths=[]for index,row in corpos.iterrows(): filePath=row["filePath"] fileContent=row["fileContent"] segs=jieba.cut(fileContent) for seg in segs: if seg not in stopwords.stopword.values and len(seg.strip())>0: segments.append(seg) filePaths.append(filePath)segmentDataFrame=pandas.DataFrame({ "segment":segments, "filePath":filePaths})segStat=segmentDataFrame.groupby( by="segment" )["segment"].agg({ "计数":numpy.size }).reset_index().sort( columns=["计数"], ascending=False)
第二种分词方法,是在jieba分词后,通过if判断,筛选除了不在stopwords里面的分词,然后在再输出为数据框,再统计计数
Python数据挖掘-词频统计-实现
相关内容
- 使用python2爬取有道翻译,,爬虫的核心思想:模拟
- (Python OpenGL)【4】Uniform变量 PyOpenGL,uniformpyopengl, 1
- Python开发运维之路day8,python维之路day8,抽象类: py2:导
- 【python】入门学习(三),,for循环for i
- Python PIL创建文字图片,,PIL库中包含了很多
- python下pip的安装,,一、下载pip并安装
- 确定当前Python环境中的site-packages目录位置,,引入“搜
- Python递归与迭代,Python递归,1、递归与迭代:递归
- Python数据分析之pandas学习,pythonpandas,#python中的p
- Logistic回归 python实现,logistic回归python,Logistic回归
评论关闭