1

我有一个家庭作业,我正在使用 Minitab 来查找数据集的四分位数和四分位数范围。当我尝试使用 NumPy 复制结果时,结果有所不同。在进行了一些谷歌搜索之后,我发现计算四分位数有许多不同的算法:如列表所示。我已经尝试了 NumPy 文档中列出的所有不同类型的插值用于百分位函数,但它们都不匹配 minitab 的算法。是否有任何惰性解决方案可以使用 NumPy 实现 minitab 算法,或者我只需要推出自己的代码并实现算法?

示例代码:

import pandas as pd
import numpy as np

terrestrial = Series([76.5,6.03,3.51,9.96,4.24,7.74,9.54,41.7,1.84,2.5,1.64])
aquatic = Series([.27,.61,.54,.14,.63,.23,.56,.48,.16,.18])

df = DataFrame({'terrestrial' : terrestrial, 'aquatic' : aquatic})

这是我与 NumPy 一起使用的方法

q75,q25 = np.percentile(df.aquatic, [75,25], interpolation='linear')
iqr = q75 - q25

Minitab 的结果不同:

Descriptive Statistics: aquatic, terrestrial 

Variable         Q1      Q3     IQR
aquatic      0.1750  0.5725  0.3975
terrestrial    2.50    9.96    7.46
4

2 回答 2

1

这是实现 Minitab 算法的尝试。假设您已经从系列中删除了缺失的观察结果,我编写了这些函数a

# Drop missing obs
x = df.aquatic[~ pd.isnull(df.aquatic)]

def get_quartile1(a):
    a = a.sort(inplace=False)
    pos1 = (len(a) + 1) / 4.0
    round_pos1 = int(np.floor((len(a) + 1) / 4.0))
    first_part = a.iloc[round_pos1 - 1]
    extra_prop = pos1 - round_pos1
    interp_part = extra_prop * (a.iloc[round_pos1] - first_part)
    return first_part + interp_part

get_quartile1(x)
Out[84]: 0.17499999999999999

def get_quartile3(a):
    a = a.sort(inplace=False)
    pos3 = (3 * len(a) + 3) / 4.0
    round_pos3 = round((3 * len(a) + 3) / 4) 
    first_part = a.iloc[round_pos3 - 1]
    extra_prop = pos3 - round_pos3
    interp_part = extra_prop * (a.iloc[round_pos3] - first_part)
    return first_part + interp_part

get_quartile3(x)
Out[86]: 0.57250000000000001
于 2015-06-02T04:21:51.113 回答
0

我想你将不得不自己动手。提供的插值方法np.percentile仅影响分位数位置周围最近数据点之间的插值方式。但似乎 minitab 实际上首先使用了不同的方法来确定分位数位置。

于 2015-06-02T04:09:01.290 回答