2016-05-04 25 views
2

我有一個數據幀:斯卡拉 - 第一個四分位數,第三個四分,並IQR從火花SQLContext數據幀不蜂巢

data.show() 
+--------+------+------------------+ 
| Count| mean|    stdev| 
+--------+------+------------------+ 
|  5| 6337| 1684.569470220803| 
|  3| 7224| 567.8250904401182| 
|  330| 20280|23954.260831863092| 
|  42| 26586| 32957.9072313323| 
... 
|  49| 23422|21244.094701798418| 
|  4| 36949| 8616.596311769514| 
|  35| 20915|14971.559603562522| 
|  33| 20874|16657.756963894684| 
|  14| 22698|15416.614921307082| 
|  25| 19100| 12342.11627585264| 
|  27| 21879|21363.736895687238| 
+--------+------+------------------+ 

不使用蜂巢,我想第一個四分位數,第二個四分位數和IQR(四分位範圍)列「平均」。

其他解決方案似乎使用每個人都無法訪問的Hive。

Hive Solution 1

Hive Solution 2

Solution for Python

回答

1

我想先指出,這似乎是一個非常昂貴的解決方案,但我得到正是我想wihtout使用蜂巢。如果你能夠使用Hive,肯定會這樣做,因爲它不會更容易。

我結束了使用commons-math3 jar。使用它的技巧是將數據從數據框中提取出來並放入數組中供math3庫使用。我從HERE解決了這個問題。您可能需要根據列的數據類型來玩「asInstanceOf」。

import org.apache.commons.math3.stat.descriptive._ 

// Turn dataframe column into an Array[Long] 
val mean = data.select("mean").rdd.map(row => row(0).asInstanceOf[Long]).collect() 

// Create the math3 object and add values from the 
// mean array to the descriptive statistics array 
val arrMean = new DescriptiveStatistics() 
genericArrayOps(mean).foreach(v => arrMean.addValue(v)) 

// Get first and third quartiles and then calc IQR 
val meanQ1 = arrMean.getPercentile(25) 
val meanQ3 = arrMean.getPercentile(75) 
val meanIQR = meanQ3 - meanQ1 
相關問題