iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python解释数学系列——分位数Qua
  • 447
分享到

Python解释数学系列——分位数Qua

位数数学系列 2023-01-30 22:01:28 447人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

跳转到我的博客 案例1 Ex1: Given a data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36],求Q1, Q2, Q3, IQR Solving: 步骤: 1. 排序,从小到大排列d

跳转到我的博客

案例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

给定数据集 data = [7, 15, 36, 39, 40, 41],求Q1,Q2,Q3

分位数计算法一

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

四分位数
概念:把给定的乱序数值由小到大排列并分成四等份,处于三个分割点位置的数值就是四分位数。
第1四分位数 (Q1),又称“较小四分位数”,等于该样本中所有数值由小到大排列后第25%的数字。
第2四分位数 (Q2),又称“中位数”,等于该样本中所有数值由小到大排列后第50%的数字。
第3四分位数 (Q3),又称“较大四分位数”,等于该样本中所有数值由小到大排列后第75%的数字。
四分位距(InterQuartile Range, IQR)= 第3四分位数与第1四分位数的差距

确定p分位数位置的两种方法
position = (n+1)*p
position = 1 + (n-1)*p

python中计算分位数位置的方案采用position=1+(n-1)*p

案例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))

程序计算结果:

序号 a b
0 1 1
1 2 10
2 3 100
3 4 100

计算p=0.1时,a列和b列的分位数
a 1.3
b 3.7
Name: 0.1, dtype: float64

手算计算结果:
计算a列
pos = 1 + (4 - 1)*0.1 = 1.3
fraction = 0.3
ret = 1 + (2 - 1) * 0.3 = 1.3
计算b列
pos = 1.3
ret = 1 + (10 - 1)* 0.3 = 3.7

案例二

利用pandas库计算data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36]的分位数。

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

自定义分位数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中的分位数程序

直接调用.quantile(p)方法,就可以计算出分位数,采用method=2方法。

1. 分位数概念
2. pandas中的quantile

--结束END--

本文标题: Python解释数学系列——分位数Qua

本文链接: https://www.lsjlt.com/news/180006.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作