【问题标题】:Using Numpy, how 25 percentile is calculate for number 1 to10?使用 Numpy,如何计算数字 1 到 10 的 25 个百分位数?
【发布时间】:2020-09-04 17:52:47
【问题描述】:
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 岁。

【问题讨论】:

标签: python numpy percentile quartile iqr


【解决方案1】:

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

第一步:求长度

x = [1,2,3,4,5,6,7,8,9,10]
l = len(x) 
# Output --> 10

第二步:减去1,得到x中第一项到最后一项的距离

# n = (length - 1) 
# n = (10-1) 
# Output --> 9

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

n * 0.25
# Therefore, (9 * 0.25) 
# Output --> 2.25
# So, fraction is 0.25 part of 2.25
# m = 0.25

第四步:现在得到最终答案

对于线性:

# 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.5

Python 代码:

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

【讨论】:

    【解决方案2】:

    Numpy 1.9.0 或更高版本有一个可选的“插值”参数,默认情况下是线性的。

    此可选参数指定当所需百分位数位于两个数据点 i 之间时使用的插值方法

    ‘linear’:i + (j - i) * fraction,其中 fraction 是 i 和 j 包围的索引的小数部分。

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

    【讨论】:

      【解决方案3】:

      虽然这可能是一个插值问题,但某些quartile methods(即方法2)的答案应该是完全正确[3, 8]

      根据我的回答here 和here,numpy 改用方法3。

      不幸的是,在统计领域对什么是四分位数提出统一定义之前,混乱将继续存在。

      【讨论】:

        【解决方案4】:

        来自numpydocumentation:

        给定一个长度为 N 的向量 V,V 的第 q 个百分位数是值 q/100 的排序副本中从最小值到最大值的方式 五、两个最近邻的值和距离以及 插值参数将确定百分位数,如果 归一化排名与 q 的位置不完全匹配。这个 如果q=50,函数与中位数相同,如果q=50,则与最小值相同 q=0,如果 q=100,则与最大值相同。

        所以问题在于当找不到与您的分位数完全匹配时 numpy 的反应。如果你使用interpolation="nearest",你会得到你期望得到的结果:

        >>> 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, interpolation="nearest")
        >>> quartile_3 = percentile(data, 75, interpolation="nearest")
        >>> print(quartile_1) 
        3
        >>> print(quartile_3) 
        8
        

        【讨论】:

          【解决方案5】:

          根据您希望计算百分位数的插值方法的类型,可以使用多种选项。

          a = np.arange(1, 11)
          a  # array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
          
          np.percentile(a, (25, 75), interpolation='midpoint') # array([3.5, 7.5])
          np.percentile(a, (25, 75), interpolation='nearest')  # array([3, 8])
          np.percentile(a, (25, 75), interpolation='nearest')  # array([3, 8])
          np.percentile(a, (25, 75), interpolation='linear')   # array([3.25, 7.75])
          np.percentile(a, (25, 75), interpolation='lower')    # array([3, 7])
          np.percentile(a, (25, 75), interpolation='higher')   # array([4, 8])
          

          您会注意到累积相对频率是百分位数需要从中得出的

          c = np.cumsum(a)
          c  # ---- array([ 1,  3,  6, 10, 15, 21, 28, 36, 45, 55], dtype=int32)
          c/c[-1] * 100
          array([  1.81818182,   5.45454545,  10.90909091,  18.18181818,
                  27.27272727,  38.18181818,  50.90909091,  65.45454545,
                  81.81818182, 100.        ])
          

          25 和 75 的百分位数需要某种形式的插值。

          【讨论】:

            猜你喜欢
            • 2011-01-23
            • 2013-08-10
            • 2017-01-26
            • 2019-06-08
            • 2020-02-14
            • 2020-08-07
            • 2017-01-11
            • 2013-08-31
            • 1970-01-01
            相关资源
            最近更新 更多