# 1. 分位数计算案例与Python代码

## 案例1

Ex1： Given a data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36]，求Q1, Q2, Q3, 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)

## 案例2

import math
def quantile_p(data, p):
data.sort()
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
return Q

data = [7, 15, 36, 39, 40, 41]
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)

Q1 = 7 +（15-7）×（1.75 - 1）= 13
Q2 = 36 +（39-36）×（3.5 - 3）= 37.5
Q3 = 40 +（41-40）×（5.25 - 5）= 40.25

Q1: 20.25
Q2: 37.5
Q3: 39.75

# 2. 分位数解释

position = (n+1)*p
position = 1 + (n-1)*p

# 3. 分位数在pandas中的解释

## 案例1

import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 100]]), columns=['a', 'b'])
print("数据原始格式：")
print(df)
print("计算p=0.1时，a列和b列的分位数")
print(df.quantile(.1))

0 1 1
1 2 10
2 3 100
3 4 100

a 1.3
b 3.7
Name: 0.1, dtype: float64

pos = 1 + (4 - 1)*0.1 = 1.3
fraction = 0.3
ret = 1 + (2 - 1) * 0.3 = 1.3

pos = 1.3
ret = 1 + (10 - 1)* 0.3 = 3.7

## 案例二

import pandas as pd
import numpy as np
dt = pd.Series(np.array([6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36])
print("数据格式：")
print(dt)
print('Q1:', df.quantile(.25))
print('Q2:', df.quantile(.5))
print('Q3:', df.quantile(.75))

Q1: 25.5
Q2: 40.0
Q3: 42.5

# 4. 概括总结

## 自定义分位数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

# 参考文献：

posted @ 2018-10-19 12:41  既生喻何生亮  阅读(...)  评论(... 编辑 收藏