我是apachebeam的新手,我想计算大型数据集的平均值和标准偏差。在
给定一个“a,B”格式的.csv文件,其中a,B是int,这基本上就是我所拥有的。在
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.io.textio import ReadFromText
class Split(beam.DoFn):
def process(self, element):
A, B = element.split(',')
return [('A', A), ('B', B)]
with beam.Pipeline(options=PipelineOptions()) as p:
# parse the rows
rows = (p
| ReadFromText('data.csv')
| beam.ParDo(Split()))
# calculate the mean
avgs = (rows
| beam.CombinePerKey(
beam.combiners.MeanCombineFn()))
# calculate the stdv per key
# ???
std >> beam.io.WriteToText('std.out')
我想做些类似的事情:
^{pr2}$或者别的什么,但我不知道怎么做。在
写你自己的组合器。这将起作用:
这会将方差计算为E(x^2)-E(x)*E(x),这样您只需传递一次数据。以下是使用上述合并器的方式:
^{pr2}$相关问题 更多 >
编程相关推荐