mysql 词频分析_09 使用python完成词频统计
1 系统、软件以及前提约束
CentOS-7 64
为减少linux权限对初学者造成影响,所有命令均在linux的root权限下进行操作。
CentOS7当中已经默认安装python3.7.3
2 操作步骤
创建mapper.py文件
#!/usr/bin/python
import sys
# input comes from STDIN (standard input)
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# increase counters
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
#
# tab-delimited; the trivial word count is 1
print ('%s\t%s' % (word, 1))
验证,执行以下语句:
echo aa bb cc dd aa cc|python mapper.py
得到以下结果:
查看统计结果
创建reducer.py文件:
#!/usr/bin/python
from operator import itemgetter
import sys
current_word = None
current_count = 0
word = None
# input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# parse the input we got from mapper.py
word, count = line.split('\t', 1)
# convert count (currently a string) to int
try:
count = int(count)
except ValueError:
# count was not a number, so silently
# ignore/discard this line
continue
# this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer
if current_word == word:
current_count += count
else:
if current_word:
# write result to STDOUT
print ('%s\t%s' % (current_word, current_count))
current_count = count
current_word = word
# do not forget to output the last word if needed!
if current_word == word:
print ('%s\t%s' % (current_word, current_count))
验证,执行以下语句:
echo aa bb cc dd aa cc|python mapper.py|sort|python reducer.py
得到以下结果:
查看统计结果
创建一个文件info.txt,内容如下:
aa bb cc dd aa cc
aa bb cc dd aa cc
aa bb cc dd aa cc
aa bb cc dd aa cc
aa bb cc dd aa cc cc dd
上传该文件到HDFS的/data的info文件中
hdfs dfs -mkdir /data
hdfs dfs -put info.txt /data/info
执行以下命令,确保hdfs下/out99不存在
$HADOOP_HOME/bin/hadoop jar
$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-2.5.2.jar
-input "/data/*"
-output "/out99"
-mapper "python mapper.py"
-reducer "python reducer.py"
-file "/root/mapper.py"
-file "/root/reducer.py"
注意:$HADOOP_HOME就是hadoop的家目录。
以上就是通过python完成词频统计的过程。
mysql 词频分析_09 使用python完成词频统计相关推荐
- python单词词频字典_用python实现词频分析+词云
2020.05.13更新:大家点个赞再收藏吧(点赞后观看,养成好习惯)TAT 如你所见.文章标题图是以 周杰伦的百度百科 词条为分析文档,以 周杰伦超话第一的那张图+PPT删除背景底色 为词频背景进行 ...
- python词频作图_基于Python的词频分析与云图生成
近期,学校对呼和浩特市第二中学学生特质进行了调查,具体题目为"用关键词描述一下二中学生的特质(尽量在20字以内)". 为了更好的展示调查结果,我们基于Python的jieba库和w ...
- 用python做频数分析_使用Python进行描述性统计
2 使用NumPy和SciPy进行数值分析 2.1 基本概念 1 from numpy import array 2 from numpy.random import normal, randint ...
- python 英语词频统计软件_Python数据挖掘——文本分析
作者 | zhouyue65 来源 | 君泉计量 文本挖掘:从大量文本数据中抽取出有价值的知识,并且利用这些知识重新组织信息的过程. 一.语料库(Corpus) 语料库是我们要分析的所有文档的集合. ...
- python 对excel文件进行分词并进行词频统计_python 词频分析
python词频分析 昨天看到几行关于用 python 进行词频分析的代码,深刻感受到了 python 的强大之处.(尤其是最近自己为了在学习 c 语言感觉被它的语法都快搞炸了,python 从来没有 ...
- 【python数据挖掘课程】十三.WordCloud词云配置过程及词频分析
这篇文章是学习了老曹的微信直播,感觉WordCloud对我的<Python数据挖掘课程>非常有帮助,作者学习后准备下次上课分享给我的学生,让他们结合词频分析来体会下词云.希望这篇基础文章对 ...
- python数据挖掘学习笔记】十三.WordCloud词云配置过程及词频分析
#2018-03-28 09:59:40 March Wednesday the 13 week, the 087 day SZ SSMR 11,12因为涉及到数据库被我暂时放弃了 python数据挖 ...
- 基于python的中文词频分析
受http://yixuan.cos.name/cn/2011/03/text-mining-of-song-poems/这篇文章的启发,觉得PYTHON来做文字处理分析应该不错,可以来做个词频分析, ...
- python文本txt词频统计_python实例:三国演义TXT文本词频分析
0x00 前言 找不到要写什么东西了!今天有个潭州大牛讲师 说了个 文本词频分析 我基本上就照抄了一遍 中间遇到一些小小的问题 自我百度 填坑补全了 如下 : 效果演示 0x01 准备环境及 ...
- python爬取微博评论并做词频分析_爬取李子柒微博评论并分析
爬取李子柒微博评论并分析 微博主要分为网页端.手机端和移动端.微博网页版反爬太厉害,因此选择爬取手机端. 1 需求 爬取李子柒微博中视频的评论信息,并做词频分析. 2 方法 2.1 运行环境 运行平台 ...
最新文章
- delphi 调用php接口_贝壳找房小程序从PHP到Golang的跃迁之路
- 我的算法学习(一)----数组的全排列
- spring cloud+dotnet core搭建微服务架构:配置中心(四)
- C++中重载、重写(覆盖)和隐藏的区别实例分析
- iphone开发中图像处理相关要点
- EasyRecovery——一款专业的数据恢复软件
- 前大疆RoboMaster技术总监:机器人工程师学习计划
- 计算机系统确认的gmp附录,马义岭--中国GMP 附录确认与验证计算机化系统.pdf
- 基于Struts2的网上书城(仿当当网)系统
- 学习笔记(13):MATLAB基础入门课程-kron函数
- c++new时赋初值_智慧树知到_C/C++程序设计案例实战_作业题库答案
- Millet谷仓:区块链重构电商
- 苹果app-H5封装源码-一键封装app搭建
- C#如何遍历文件夹下的所有文件
- IDEA Java 死锁 解决死锁状态的三种方法
- 轩小陌的Python笔记-day14 自定义模块、第三方模块、内置模块(部分)
- excel二进制移位运算_Excel揭秘13:在Excel中实现位运算
- Tesseract-OCR 控制台怎么使用
- 1.Transformer-Attention is all your need论文详读-PartⅠ(摘要、引言、背景)
- 【Lintcode】1718. Minimize Malware Spread