案例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[pos_integer - 1])*pos_decimal
return Q
data = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49]
Q1 = quantile_p(data, 0.25)
print("Q1:", Q1)
Q2 = quantile_p(data, 0.5)
print("Q2:", Q2)
Q3 = quantile_p(data, 0.75)
print("Q3:", Q3)
分位数计算法二
pos = 1+ (n-1)*p,n为数据的总个数,p为0-1之间的值
Q1的pos = 1 + (11 - 1)*0.25 = 3.5 (p=0.25) Q1=25.5
Q2的pos = 1 + (11 - 1)*0.5 = 6 (p=0.5) Q2=40
Q3的pos = 1 + (11 - 1)*0.75 = 8.5 (p=0.75) Q3=42.5
import math
def quantile_p(data, 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[pos_integer - 1])*pos_decimal
return Q
data = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49]
Q1 = quantile_p(data, 0.25)
print("Q1:", Q1)
Q2 = quantile_p(data, 0.5)
print("Q2:", Q2)
Q3 = quantile_p(data, 0.75)
print("Q3:", Q3)
概括总结
自定义分位数python代码程序
import math
def quantile_p(data, p, method=1):
data.sort()
if method == 2:
pos = 1 + (len(data)-1)*p
else:
pos = (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[pos_integer - 1])*pos_decimal
Q1 = quantile_p(data, 0.25)
Q2 = quantile_p(data, 0.5)
Q3 = quantile_p(data, 0.75)
IQR = Q3 - Q1
return Q1, Q2, Q3, IQR
pandas中的分位数程序
Python中可以利用Numpy库来计算分位数,示例如下:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# 中位数
print(np.median(a))
# 25%分位数
print(np.percentile(a, 25))
# 75%分位数
print(np.percentile(a, 75))
来源:CSDN
作者:qq_36117775
链接:https://blog.csdn.net/qq_36117775/article/details/104226983