首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Numpy,如何计算1号to10的25百分位数?

使用Numpy,如何计算1号to10的25百分位数?
EN

Stack Overflow用户
提问于 2019-11-28 18:47:04
回答 5查看 1.8K关注 0票数 2
代码语言:javascript
复制
from numpy import percentile
import numpy as np
data=np.array([1,2,3,4,5,6,7,8,9,10])
# calculate quartiles
quartile_1 = percentile(data, 25)
quartile_3 =percentile(data, 75)
# calculate min/max

print(quartile_1) # show 3.25
print(quartile_3) # shows 7.75

你能解释一下3.25和7.75的价值是如何计算的吗?我预计他们是3岁和8岁。

EN

回答 5

Stack Overflow用户

发布于 2020-09-04 17:41:46

手动逐步计算Numpy百分位数:

第1步:查找长度

代码语言:javascript
复制
x = [1,2,3,4,5,6,7,8,9,10]
l = len(x) 
# Output --> 10

Step-2:减去1得到x中从第一项到最后一项的距离

代码语言:javascript
复制
# n = (length - 1) 
# n = (10-1) 
# Output --> 9

Step-3:将n乘以分位数,这里是第25个百分位数或0.25个分位数或1个四分位数

代码语言:javascript
复制
n * 0.25
# Therefore, (9 * 0.25) 
# Output --> 2.25
# So, fraction is 0.25 part of 2.25
# m = 0.25

第4步:现在获取最终答案

线性

代码语言:javascript
复制
# i + (j - i) * m
# Here, think i and j as values at indices
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, for '2.25':
# value at index immediately before 2.25, is at index=2 so, i=3
# value at index immediately after 2.25, is at index=3 so, i=4
# and fractions 
3 + (4 - 3)*0.25
# Output --> 3.25

针对较低的

代码语言:javascript
复制
# Here, based on output from Step-3
# Because, it is '2.25', 
# Find a number a index lower than 2.25
# So, lower index is '2'
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, at index=2 we have '3' 
# Output --> 3

适用于更高

代码语言:javascript
复制
# Here, based on output from Step-3
# Because, it is '2.25', 
# Find a number a index higher than 2.25
# So, higher index is '3'
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, at index=3 we have '4' 
# Output --> 4

最近

代码语言:javascript
复制
# Here, based on output from Step-3
# Because, it is '2.25', 
# Find a number a index nearest to 2.25
# So, nearest index is '2'
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, at index=2 we have '3' 
# Output --> 3

适用于中点

代码语言:javascript
复制
# Here, based on output from Step-3
# (i + j)/2
# Here, think i and j as values at indices
# x = [1,2,3,4,5,6,7,8,9,10]
#idx= [0,1,2,3,.........,9]
# So, for '2.25'
# value at index immediately before 2.25, is at index=2 so, i=3
# value at index immediately after 2.25, is at index=3 so, i=4
(3+4)/2
# Output --> 3.5

Python中的代码:

代码语言:javascript
复制
x = np.array([1,2,3,4,5,6,7,8,9,10])
print("linear:", np.percentile(x, 25, interpolation='linear'))
print("lower:", np.percentile(x, 25, interpolation='lower'))
print("higher:", np.percentile(x, 25, interpolation='higher'))
print("nearest:", np.percentile(x, 25, interpolation='nearest'))
print("midpoint:", np.percentile(x, 25, interpolation='midpoint'))

输出:

代码语言:javascript
复制
linear: 3.25
lower: 3
higher: 4
nearest: 3
midpoint: 3.5
票数 2
EN

Stack Overflow用户

发布于 2019-11-28 18:59:19

1.9.0版或更高版本的Numpy有一个可选的'interpolation‘参数,默认情况下是线性的。

此可选参数指定当所需的百分位数位于两个数据点i‘线性’:i+ (j - i) *分数,其中分数是由i和j包围的索引的分数部分。

如果要更改该行为,只需手动添加参数并使用interpolation='nearest’覆盖默认值

票数 1
EN

Stack Overflow用户

发布于 2019-11-28 19:13:13

虽然这可能是一个插值问题,但通过某些方法(即方法2),答案应该是[3, 8]

根据我的答案hereherenumpy使用方法3。

不幸的是,除非统计领域对四分位数有一个统一的定义,否则混乱将会继续下去。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59087048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档