python - 如何使用 python 在 mapreduce 中获得直方图(Graph)中的结果?
问题描述
当我运行此代码时,我在集群上的 reduce 部分出现错误。我正在考虑概率并使用 Matplotlib 来获取我的输出图形,但它会失败。我在 Google Cloud 集群上运行此代码。我在 excel csv 文件中的数据。
#!/usr/bin/env python3
"""mapper.py"""
import sys
# Get input lines from stdin
for line in sys.stdin:
# Remove spaces from beginning and end of the line
#line = line.strip()
# Split it into tokens
#tokens = line.split()
#Get probability_mass values
for prob in line:
print("None\t{}".format(prob))
#print(str(probability_mass)+ '\t1')
#print('%s\t%s' % (probability_mass, None))
#!/usr/bin/env python3
"""reducer.py"""
import sys
import matplotlib.pyplot as plt
from collections import defaultdict
counts = defaultdict(float)
# Get input from stdin
for line in sys.stdin:
#Remove spaces from beginning and end of the line
#line = line.strip()
# skip empty lines
if not line:
continue
# parse the input from mapper.py
k,v = line.split('\t', 1)
counts[v] += 1
total = (float(sum(counts.values())))
#total = sum(counts.values())
probability_mass = {k:v/total for k,v in counts.items()}
#print(probability_mass)
grad = probability_mass.keys()
prob = probability_mass.values()
print(str(grad))
print(str(prob))
#bins = 20
plt.hist(prob,bins=20, normed=1, facecolor='blue', alpha=0.5)
#plt.plot(bins, hist, 'r--')
plt.xlabel('Probability')
plt.ylabel('Number Of Students')
plt.title('Histogram of Students Grade')
plt.subplots_adjust(left=0.15)
plt.show()
解决方案
您需要将结果导出到文件中,然后下载它并将其绘制为两个单独的步骤。
MapReduce 没有 GUI,您不应该让每个 reducer 任务都尝试生成绘图
或者,您可以将结果导出到 BigQuery 或 Datastore 等 GCP 工具,您可以在其中插入适当的 BI 工具进行可视化分析
推荐阅读
- javascript - 本地机器上的 Nodejs webhook 服务器
- mockito - Mockito doNothing with Mockito.mockStatic
- microsoft-graph-api - 使用 Microsoftgraph 的威胁评估请求的 RestAPI 问题
- mysql - 从重复值中选择行,其中字段从未更改
- assembly - EMU8086 将 32 位数除以 16 位数给出意外的 0 余数
- html - 如何在 Power BI 中显示 HTML 页面
- javascript - 在 highcharts 上绘制数据和时区反应
- awk - 当条件为真时从另一个文件添加行
- c# - 如何将 url 重定向到特定端点
- c++ - 如何获取比例因子以获得真实的屏幕分辨率?