如何使用星火找到中位数和分位数我怎样才能找到RDD使用分布式方法的整数,IPython和SPark?这个RDD大约有70万个元素,因此太大,无法收集和找到中位数。这个问题和这个问题类似。但是,这个问题的答案是使用Scala,我不知道。如何计算ApacheSPark的精确中值?使用Scala答案的思路,我试图用Python编写类似的答案。我知道我首先想把RDD..我不知道怎么做。我看到sortBy(将此rdd按给定的keyfunc)和sortByKey(整理这个RDD,它假定由(键,值)对组成。)方法。我认为两者都使用键值,而我的RDD只有整数元素。首先,我在考虑myrdd.sortBy(lambda x: x)?接下来,我将找到RDD的长度(rdd.count()).最后,我希望找到RDD中心的元素或2个元素。这个方法我也需要帮助。编辑:我有个主意。也许我可以索引我的RDD然后Key=index和value=Element。然后我就可以试着按价值分类了?我不知道这是否可能,因为只有一个sortByKey方法。
3 回答
绝地无双
TA贡献1946条经验 获得超4个赞
火花2.0+:
approxQuantile
Python:
df.approxQuantile("x", [0.5], 0.25)
斯卡拉:
df.stat.approxQuantile("x", Array(0.5), 0.25)
df.approxQuantile(["x", "y", "z"], [0.5], 0.25)
df.approxQuantile(Array("x", "y", "z"), Array(0.5), 0.25)
火花<2.0
Python
import numpy as np np.random.seed(323)rdd = sc.parallelize(np.random.randint(1000000, size=700000))%time np.median(rdd.collect())np.array(rdd.collect()).nbytes
from numpy import floorimport timedef quantile(rdd, p, sample=None, seed=None): """Compute a quantile of order p ∈ [0, 1] :rdd a numeric rdd :p quantile(between 0 and 1) :sample fraction of and rdd to use. If not provided we use a whole dataset :seed random number generator seed to be used with sample """ assert 0 <= p <= 1 assert sample is None or 0 < sample <= 1 seed = seed if seed is not None else time.time() rdd = rdd if sample is None else rdd.sample(False, sample, seed) rddSortedWithIndex = (rdd. sortBy(lambda x: x). zipWithIndex(). map(lambda (x, i): (i, x)). cache()) n = rddSortedWithIndex.count() h = (n - 1) * p rddX, rddXPlusOne = ( rddSortedWithIndex.lookup(x)[0] for x in int(floor(h)) + np.array([0L, 1L])) return rddX + (h - floor(h)) * (rddXPlusOne - rddX)
np.median(rdd.collect()), quantile(rdd, 0.5)## (500184.5, 500184.5)np.percentile(rdd.collect(), 25), quantile(rdd, 0.25)## (250506.75, 250506.75)np.percentile(rdd.collect(), 75), quantile(rdd, 0.75)(750069.25, 750069.25)
from functools import partial median = partial(quantile, p=0.5)
语言独立 (蜂箱):
HiveContext
rdd.map(lambda x: (float(x), )).toDF(["x"]).registerTempTable("df")sqlContext.sql("SELECT percentile_approx(x, 0.5) FROM df")
sqlContext.sql("SELECT percentile(x, 0.5) FROM df")
percentile_approx
守候你守候我
TA贡献1802条经验 获得超10个赞
/** * Gets the nth percentile entry for an RDD of doubles * * @param inputScore : Input scores consisting of a RDD of doubles * @param percentile : The percentile cutoff required (between 0 to 100), e.g 90%ile of [1,4,5,9,19,23,44] = ~23. * It prefers the higher value when the desired quantile lies between two data points * @return : The number best representing the percentile in the Rdd of double */ def getRddPercentile(inputScore: RDD[Double], percentile: Double): Double = { val numEntries = inputScore.count().toDouble val retrievedEntry = (percentile * numEntries / 100.0 ).min(numEntries).max(0).toInt inputScore .sortBy { case (score) => score } .zipWithIndex() .filter { case (score, index) => index == retrievedEntry } .map { case (score, index) => score } .collect()(0) }
添加回答
举报
0/150
提交
取消