【问题标题】:How to compute Minitab-equivalent quartiles using NumPy如何使用 NumPy 计算 Minitab 等效四分位数
【发布时间】:2015-06-02 03:25:51
【问题描述】:

我正在使用 Minitab 完成一项家庭作业,以查找数据集的四分位数和四分位数范围。当我尝试使用 NumPy 复制结果时,结果有所不同。做了一些谷歌搜索后,我发现计算四分位数有很多不同的算法:as listed here。我已经尝试了 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

【问题讨论】:

  • 你能提供示例输入/输出吗?
  • 是否有任何文档解释 minitab 使用什么算法?特别是它如何处理NaN?你的两列有不同的长度,所以 aquatic 在末尾用 NaN 填充。
  • 问题中包含的链接有一个条目。另一篇文章证实了这一点:here

标签: python numpy statistics minitab


【解决方案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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2020-12-15
      • 2015-04-12
      • 2021-08-16
      • 2021-12-26
      • 2019-04-19
      相关资源
      最近更新 更多