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岁。
发布于 2020-09-04 17:41:46
手动逐步计算Numpy百分位数:
第1步:查找长度
x = [1,2,3,4,5,6,7,8,9,10]
l = len(x)
# Output --> 10Step-2:减去1得到x中从第一项到最后一项的距离
# n = (length - 1)
# n = (10-1)
# Output --> 9Step-3:将n乘以分位数,这里是第25个百分位数或0.25个分位数或1个四分位数
n * 0.25
# Therefore, (9 * 0.25)
# Output --> 2.25
# So, fraction is 0.25 part of 2.25
# m = 0.25第4步:现在获取最终答案
线性:
# 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针对较低的
# 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适用于更高的
# 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最近的
# 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适用于中点的
# 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.5Python中的代码:
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'))输出:
linear: 3.25
lower: 3
higher: 4
nearest: 3
midpoint: 3.5发布于 2019-11-28 18:59:19
1.9.0版或更高版本的Numpy有一个可选的'interpolation‘参数,默认情况下是线性的。
此可选参数指定当所需的百分位数位于两个数据点i‘线性’:i+ (j - i) *分数,其中分数是由i和j包围的索引的分数部分。
如果要更改该行为,只需手动添加参数并使用interpolation='nearest’覆盖默认值
https://stackoverflow.com/questions/59087048
复制相似问题