跳转到我的博客

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
给定数据集 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

2. 分位数解释

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

确定p分位数位置的两种方法

position = (n+1)*p

position = 1 + (n-1)*p

3. 分位数在pandas中的解释

在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

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

pandas中的分位数程序

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

参考文献:

1. 分位数概念

2. pandas中的quantile

Python解释数学系列——分位数Quantile的更多相关文章

  1. Python操作redis系列之 列表(list) (四)

    # -*- coding: utf- -*- import redis r =redis.Redis(host=,password="ZBHRwlb1608") 1. Lpush ...

  2. Python股票分析系列——系列介绍和获取股票数据.p1

    本系列转载自youtuber sentdex博主的教程视频内容 https://www.youtube.com/watch?v=19yyasfGLhk&index=4&list=PLQ ...

  3. Python操作redis系列之 列表(list) (五)(转)

    # -*- coding: utf-8 -*- import redis r =redis.Redis(host=") 1. Lpush 命令将一个或多个值插入到列表头部. 如果 key 不 ...

  4. Python操作redis系列之 列表(list) (五)

    # -*- coding: utf- -*- import redis r =redis.Redis(host=,password=") 1. Lpush 命令将一个或多个值插入到列表头部. ...

  5. 【跟我一起学Python吧】Python解释执行原理

    这里的解释执行是相对于编译执行而言的.我们都知道,使用C/C++之类的编译性语言编写的程序,是需要从源文件转换成计算机使用的机器语言,经过链接器链接之后形成了二进制的可执行文件.运行该程序的时候,就可 ...

  6. 【和我一起学python吧】Python解释执行原理

    这里的解释执行是相对于编译执行而言的.我们都知道,使用C/C++之类的编译性语言编写的程序,是需要从源文件转换成计算机使用的机器语言,经过链接器链接之后形成了二进制的可执行文件.运行该程序的时候,就可 ...

  7. Python+Django+SAE系列教程17-----authauth (认证与授权)系统1

    通过session,我们能够在多次浏览器请求中保持数据,接下来的部分就是用session来处理用户登录了. 当然,不能仅凭用户的一面之词,我们就相信,所以我们须要认证. 当然了,Django 也提供了 ...

  8. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  9. python解释执行原理(转载)

    Python解释执行原理 转自:http://l62s.iteye.com/blog/1481421 这里的解释执行是相对于编译执行而言的.我们都知道,使用C/C++之类的编译性语言编写的程序,是需要 ...

随机推荐

  1. 【转】Python介绍

    [转]Python介绍 本节内容 Python简史 Python是一门什么样的语言? Python的优点与缺点 Python解释器 一.Python简史 历史背景 在20世纪80年代,IBM和苹果已经 ...

  2. David McCullough, Jr.为韦斯利高中毕业生演讲〈你并不特别〉

    Dr. Wong, Dr. Keough, Mrs.Novogroski, Ms. Curran, members of the board of education, familyand frien ...

  3. Shell 中test 单中括号[] 双中括号[[]] 的区别

    Shell test 单中括号[] 双中括号[[]] 的区别 在写Shell脚本的时候,经常在写条件判断语句时不知道该用[] 还是 [[]],首先我们来看他们的类别: $type [ [[ test ...

  4. javascript中的return、return true、return false、continue区别

    1.语法为:return 表达式; 2.w3c中的解释: 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果  也就是:当代码执行到return语句时,函数返回一个结果就结束运行了,ret ...

  5. MariaDB基于GTID主从复制及多主复制

    一.简单主从模式配置步骤(必须要mysql5.6,此处以maridb10.0.10为例) 1.配置主从节点的服务配置文件 # vim /etc/my.cnf 1.1.配置master节点: [mysq ...

  6. tomcat8配置SSL

    参考网址:http://www.micmiu.com/enterprise-app/sso/sso-cas-sample/#viewSource 1.生成证书 keytool -genkey -ali ...

  7. adb devices检测不到夜神模拟器

    1.dos下,cd进入到夜神模拟器的bin目录 代码: nox_adb connect 127.0.0.1:62001 2.dos下,进入进Android SDK下的platform-tools目录 ...

  8. Go语言规格说明书 之 Go语句(Go statements)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  9. js检测当前设备是移动端还是PC端

    加上下面js即可 硬核判断: <script type="text/javascript"> //平台.设备和操作系统 var system ={ win : fals ...

  10. java中文GBK和UTF-8编码转换乱码的分析

    原文:http://blog.csdn.net/54powerman/article/details/77575656 作者:54powerman 一直以为,java中任意unicode字符串,可以使 ...