繁体   English   中英

如何使用python在mapreduce中的直方图(图形)中获得结果?

[英]How can i get my result in histogram(Graph) in mapreduce using python?

当我运行此代码时,我在集群上的 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 任务都尝试生成绘图

或者您可以将结果导出到一些 GCP 工具,例如 BigQuery 或 Datastore,您可以在其中插入适当的 BI 工具进行可视化分析

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM