Python分位数的计算
案例1 Ex1: Given a data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36],求Q1(25分位), Q2(50分位), Q3(75分位), IQR Solving: 步骤: 1. 排序,从小到大排列data,data = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49] 2. 计算分位数的位置 3. 给出分位数 分位数计算法一 pos = (n+1)*p,n为数据的总个数,p为0-1之间的值 Q1的pos = (11 + 1)*0.25 = 3 (p=0.25) Q1=15 Q2的pos = (11 + 1)*0.5 = 6 (p=0.5) Q2=40 Q3的pos = (11 + 1)*0.75 = 9 (p=0.75) Q3=43 IQR = Q3 - Q1 = 28 import math def quantile_p(data, p): pos = (len(data) + 1)*p #pos = 1 + (len(data)-1)*p pos_integer = int(math.modf(pos)[1]) pos_decimal = pos - pos_integer Q = data[pos_integer - 1] + (data[pos_integer] - data