測量資料的重心 (Measures of Central Tendency)


套路 2: 測量資料的重心 (Measures of Central Tendency)

什麼是資料的重心? 說白了就是平均值 (mean)、中數 (median) 或眾數 (mode)
算術平均數 (Arithmetic mean)公式
其中μ母體(population)的算數平均數Xii個樣本N母體的樣本數
其中X橫槓樣本(sample)的算數平均數Xii個樣本n抽樣的樣本數

如何使用Python計算算術平均數?
方法一: 將上述計算公式寫成Python 程式
第一步: 資料 numStudent = [58,57,54,59,53,56,58,59,60]  #生物統計學歷年班級學生數
第二步: 程式
l = len(numStudent)
t = 0
for i in range(l):
    t = t + numStudent[i]
arMean = t / l
arMean
# 結果: 57.111111111111114

方法二: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
import statistics
statistics.mean(numStudent)
# 結果: 57.111111111111114

方法三: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
from statistics import mean
mean(numStudent)
# 結果: 57.111111111111114

算術平均數 (Arithmetic mean)如果資料包含次數(frequency) 公式
其中X橫槓樣本(sample)的算數平均數Xii個樣本n抽樣的樣本數fi是第i個樣本重複出現的次數。
Xi (cm)
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4.0
4.1
4.2
4.3
4.4
4.5
次數
1
0
1
2
1
3
3
4
3
2
2
1
1
將上述計算公式寫成Python 程式
第一步: 資料
x = [3.3,3.4,3.5,3.6,3.7,3.8,3.9,4.0,4.1,4.2,4.3,4.4,4.5] 
f = [1,0,1,2,1,3,3,4,3,2,2,1,1] 
第二步: 程式
t = 0
c = 0
l = len(x)
for i in range(l):
    t = t + (x[i] * f[i])
    c = c + f[i]
fm = t / c
fm
# 結果: 3.9583333333333335

中位數 (median) 公式
sample median = X(n+1)/2, n (sample size) 是奇數
sample median = (Xn/2 + X(n+2)/2)/2, n (sample size) 是偶數

如何使用Python計算算術平均數?
方法一: 將上述計算公式寫成Python 程式
第一步: 資料 numStudent = [58,57,54,59,53,56,58,59,60]  #生物統計學歷年班級學生數
第二步: 程式
def medi(dat):
    n = len(dat)
    sorted_d = sorted(dat)
    midNum = n // 2
    if n % 2 == 1:
        return sorted_d[midNum]
    else:
        dow = midNum - 1
        upp = midNum
        return (sorted_d[dow] + sorted_d[upp]) / 2
medi(numStudent)
# 結果: 58

方法二: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
import statistics
statistics.median(numStudent)
# 結果: 58

方法三: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
from statistics import median
median(numStudent)
# 結果: 58

眾數 (mode) 公式
mode = 出現次數最多的數

如何使用Python計算眾數?
方法一: 將上述計算公式寫成Python 程式
第一步: 資料 numStudent = [58,57,54,59,53,56,58,59,60]  #生物統計學歷年班級學生數
第二步: 程式
from collections import Counter
def mod(x):
    dat = Counter(x)
    max_v = max(dat.values())
    dd = {}
    for keys,values in dat.items():
        if values==max_v:
            dd[keys] = values
    return dd
mod(numStudent)
# 結果: {58: 2, 59: 2}

方法二: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
import statistics
statistics.mode(numStudent)
# 結果: StatisticsError: no unique mode; found 2 equally common values
# 兩個以上眾數Python 函數顯示錯誤訊息

方法三: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
from statistics import mode
mode(numStudent)
# 結果: StatisticsError: no unique mode; found 2 equally common values
# 兩個以上眾數Python 函數顯示錯誤訊息

幾何平均數 (Geometric mean) 公式
其中XG橫槓樣本(sample)的幾何平均數Xii個樣本n抽樣的樣本數

如何使用Python計算幾何平均數?
方法一: 將上述計算公式寫成Python 程式
第一步: 資料 numStudent = [58,57,54,59,53,56,58,59,60]  #生物統計學歷年班級學生數
第二步: 程式
n = len(numStudent)
p = 0
for i in range(n):
    p = p * numStudent[i]
gMean = p**(1/n)
gMean
# 結果: 57.066684512409616

方法二: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
from scipy.stats import gmean
gmean(numStudent)
# 結果: 57.06668451240963

調和平均數 (Harmonic mean) 公式
其中XH橫槓樣本(sample)的調和平均數Xii個樣本n抽樣的樣本數

如何使用Python計算調和平均數?
方法一: 將上述計算公式寫成Python 程式
第一步: 資料 numStudent = [58,57,54,59,53,56,58,59,60]  #生物統計學歷年班級學生數
第二步: 程式
t = 0
n = len(numStudent)
for i in range(n):
    t = t + (1 / numStudent[i])
hm = n / t
hm
# 結果: 57.02150960484172

方法二: 使用Python 函數(function)
numStudent = [58,57,54,59,53,56,58,59,60]
from scipy.stats import hmean
hmean(numStudent)
# 結果: 57.02150960484173


留言

這個網誌中的熱門文章

三因子變異數分析 (Three-Way ANOVA)

兩組獨立樣本變異數相同 t 檢定 (Two-Sample t test with equal variances,parametric)

雙因子變異數分析 (Two-Way ANOVA)