zoukankan      html  css  js  c++  java
  • Python数据挖掘-词云

    词云绘制

    1、语料库的搭建、分词来源、移除停用词、词频统计

    使用方法:os.path.join(path,name)   #连接目录与文件名或目录 结果为path/name

    import os
    import os.path
    import codecs
    
    filePaths=[]
    fileContents=[]
    for root,dirs,files in os.walk("D:\Python\Python数据挖掘\Python数据挖掘实战课程课件\2.4\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 pandas
    corpos=pandas.DataFrame({
                             "filePath":filePaths,
                             "fileContent":fileContents})
    
    #分词来源哪个文章
    import jieba
    
    segments=[]
    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})
    
    
    import numpy
    #进行词频统计
    #by是要分组的列,[]是要统计的列
    segStat=segmentDataFrame.groupby(
                by="segment"
                )["segment"].agg({
                "计数":numpy.size
                }).reset_index().sort(columns=["计数"],
                ascending=False)
    
    #移除停用词
    stopwords=pandas.read_csv(
        "D:\Python\Python数据挖掘\Python数据挖掘实战课程课件\2.4\StopwordsCN.txt",
        encoding="utf-8",
        index_col=False)
    fSegStat=segStat[
            ~segStat.segment.isin(stopwords.stopword)]
    
    
    #第二种去除分词的方法
    import jieba
    segments=[]
    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)
    View Code

    2、词云绘制

    首先要引入WordCloud,然后在引入画图模块matplotlib中pyplot函数

    一般先设定词云的背景和字体,用到background和font_path

    词云统计的话,一般是字典形式,这时候分词就需要作为序列,然后统计的词频数作为列,然后再作为参数传入fit_words

    图形的展示通过plt函数的方法imshow()来展示

    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    
    wordcloud =WordCloud(
        font_path="D:\Python\Python数据挖掘\Python数据挖掘实战课程课件\2.4\simhei.ttf",
        background_color="black"
        )
    
    words=fSegStat.set_index("segment").to_dict()
    
    wordcloud.fit_words(words["计数"])
    plt.imshow(wordcloud)
    plt.close()
    View Code
  • 相关阅读:
    2.变量
    1.注释
    MyEclipse使用教程:使用DevStyle增强型启动
    DevExpress WPF v19.1新版亮点:Data Editors等控件新功能
    测试工具Telerik Test Studio发布R2 2019|支持VS 2019
    MyEclipse使用教程:使用主题自定义工作台外观
    DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强
    知名界面类控件Kendo UI for jQuery R2 2019 SP1发布|附下载
    MyEclipse使用教程:unattended安装
    跨平台开发框架DevExtreme v19.1.4正式发布|附下载
  • 原文地址:https://www.cnblogs.com/U940634/p/9736009.html
Copyright © 2011-2022 走看看