前言:平均數是用來表示數據整體趨勢的一個統計指標,本文參考wiki,采用一些簡明的例子,主要是總結。
- 算術平均數(Arithmetic Mean)
- 計算公式
- 優點:相比於中位數、眾數,更少收到隨機因素的影響
- 缺點:更容易收到極端值(biased value)的影響
- 例子:
-
- data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. result = 5.5.
- 幾何平均數(Geometric Mean)
- 計算公式
- 優點:適用於對比率數據的平均,主要用於計算數據平均增長率
- 例子:
-
- data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. result = 4.5287.
- 調和平均數(Harmonic Mean)
- 計算公式
- 優點:計算平均速率,感覺很多paper都在用,用於計算平均速率
- 例子:
- data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. result = 3.414.
- 平方平均數(Quadratic Mean)
- 計算公式:
- 優點:是2次方的廣義平均數的表達式。可以定義在連續區間。常用來計算一組數據與某個數據之間的平均差。
- 例子:
-
- data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. result = 6.204.
注意到各種算法得到的平均值有所差別,比如在這個例子之中,調和平均數偏小,平方平均數偏大,當然我沒有嚴格formulate,不能說是一個通用的結論。在看一篇Sigcomm的文章時,prediction的結果一般較為樂觀(偏大)時,error比較大,作者采用了Harmonic mean。
附python實現代碼(CalculateMeans.py)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 from math import * 5 6 def ArithmeticMean(data): 7 len_d = float(len(data)) 8 result = sum(data) / len_d 9 return result 10 11 def GeometricMean(data): 12 len_d = len(data) 13 product = 1.0 14 for i in range(len_d): 15 product = product * data[i] 16 # the next line is equal to calculate the n-root 17 result = product ** (1.0/len_d) 18 return result 19 20 def HarmonicMean(data): 21 len_d = len(data) 22 x = [1.0/data[i] for i in range(len_d)] 23 result = float(len_d) / sum(x) 24 return result 25 26 def QuadraticMean(data): 27 len_d = len(data) 28 x = [data[i] * data[i] for i in range(len_d)] 29 result = sqrt(sum(x) / float(len_d)) 30 return result 31 32 33 if __name__ == "__main__": 34 data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 35 print "Scenario used in the test:", data 36 print "The arithmetic mean:", ArithmeticMean(data) 37 print "The geometric mean:", GeometricMean(data) 38 print "The Harmonic mean:", HarmonicMean(data) 39 print "The Quadratic mean:", QuadraticMean(data)
實驗結果:
參考:
https://zh.wikipedia.org/wiki/%E8%B0%83%E5%92%8C%E5%B9%B3%E5%9D%87%E6%95%B0
https://zh.wikipedia.org/wiki/%E5%87%A0%E4%BD%95%E5%B9%B3%E5%9D%87%E6%95%B0
https://zh.wikipedia.org/wiki/%E5%B9%B3%E6%96%B9%E5%B9%B3%E5%9D%87%E6%95%B0
https://zh.wikipedia.org/wiki/%E7%AE%97%E6%9C%AF%E5%B9%B3%E5%9D%87%E6%95%B0