这段代码:
print len(my_series)
print np.percentile(my_series, 98)
print np.percentile(my_series, 99)
给出:
14221 # This is the series length
1644.2 # 98th percentile
nan # 99th percentile?
为什么 98 工作正常但 99 给出 nan
?
最佳答案
np.percentile 将 nan 视为非常高/无限的数字。因此,高百分位数将在您最终得到 nan 的范围内。在您的情况下,1% 到 2% 的数据将是 nan(第 98 个百分位数将返回一个数字(实际上不是所有有效值的第 98 个百分位数),第 99 个将返回一个 nan)。
要计算没有 nan 的百分位数,您可以使用 np.nanpercentile()
所以:
print np.nanpercentile(my_series, 98)
print np.nanpercentile(my_series, 99)
关于python - 为什么 np.percentile 对高百分位数返回 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836032/