【发布时间】:2020-08-07 00:13:32
【问题描述】:
我想计算两个百分位数范围之间的平均值,例如在第 25 和第 50 个百分位数之间。
我通常使用 np.percentile 来计算特定的百分位数。
知道如何计算平均值(25-50)吗?可以减吗?
mean(25-50) = np.percentile(array,50) - np.percentile(array,25)
``
【问题讨论】:
标签: python numpy percentile
我想计算两个百分位数范围之间的平均值,例如在第 25 和第 50 个百分位数之间。
我通常使用 np.percentile 来计算特定的百分位数。
知道如何计算平均值(25-50)吗?可以减吗?
mean(25-50) = np.percentile(array,50) - np.percentile(array,25)
``
【问题讨论】:
标签: python numpy percentile
您不能简单地将不同百分位数的两个值相减。
为了找到第 25 和第 50 个百分位数之间元素的平均值,您需要找到所有这些元素的总和,然后将其除以大小。
要找到上述元素的总和,您可以从第 0-50 个百分位数的总和中减去第 0-25 个百分位数的总和。
一旦得到差值总和,只需将其除以这些元素的大小即可。
# find the indexes of the element below 25th and 50th percentile
idx_under_25 = np.argwhere(array <= np.percentile(array, 25)).ravel()
idx_under_50 = np.argwhere(array <= np.percentile(array, 50)).ravel()
# find the number of the elements in between 25th and 50th percentile
diff_num = len(idx_under_50) - len(idx_under_25)
# find the sum difference
diff_sum = np.sum(np.take(array, idx_50)) - np.sum(np.take(array, idx_25))
# get the mean
mean = diff_sum / diff_num
【讨论】: