如何使用 NumPy 计算 Minitab 等效四分位数
How to compute Minitab-equivalent quartiles using NumPy
我有一个家庭作业是用 Minitab 做的,目的是找出数据集的四分位数和四分位数间距。当我尝试使用 NumPy 复制结果时,结果不同。谷歌搜索后,我发现计算四分位数有许多不同的算法:as listed here。我已经尝试了 NumPy 文档中列出的百分位数函数的所有不同类型的插值,但其中 none 与 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
我想你必须自己动手。 np.percentile
提供的插值方法仅影响分位数位置周围最近数据点之间的插值方式。但似乎 minitab 实际上首先使用不同的方法来确定分位数位置。
这是实现 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
我有一个家庭作业是用 Minitab 做的,目的是找出数据集的四分位数和四分位数间距。当我尝试使用 NumPy 复制结果时,结果不同。谷歌搜索后,我发现计算四分位数有许多不同的算法:as listed here。我已经尝试了 NumPy 文档中列出的百分位数函数的所有不同类型的插值,但其中 none 与 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
我想你必须自己动手。 np.percentile
提供的插值方法仅影响分位数位置周围最近数据点之间的插值方式。但似乎 minitab 实际上首先使用不同的方法来确定分位数位置。
这是实现 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