python - 为什么 np.percentile 对高百分位数返回 NaN?

标签 python numpy pandas

这段代码:

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/

相关文章:

python - 计算列表中唯一单词的数量

python - 以 SQLite 和 HDF5 格式从 numpy、scipy 导出/导入

python - 如何分配属于不同人群的不确定性?

python - 在 Python 中从矩阵中选择列向量

python - 如何将数据框中的列值拆分为多列

Python Pandas : Convert timedelta Value From Subtracting Two Dates Into Integer Datatype (AttributeError)

python - 如何打开 .exe 文件?

python - 字符串比较似乎不适用于从文件中读取的行

python - 过滤字典中的项目

python - 在数据帧中查找第一个匹配对并标记两者