numpy.percentile()

1.函数

百分位数是统计中使用的度量,表示小于这个值的观察值的百分比。 函数numpy.percentile()接受以下参数。

np.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)

2.参数说明:

  • a: 输入数组
  • q: 要计算的百分位数,在 0 ~ 100 之间
  • axis: 沿着它计算百分位数的轴
  • keepdims :bool是否保持维度不变
  • 首先明确百分位数:第 p 个百分位数是这样一个值,它使得至少有 p% 的数据项小于或等于这个值,且至少有 (100-p)% 的数据项大于或等于这个值。

【注】举个例子:高等院校的入学考试成绩经常以百分位数的形式报告。比如,假设某个考生在入学考试中的语文部分的原始分数为 54 分。相对于参加同一考试的其他学生来说,他的成绩如何并不容易知道。但是如果原始分数54分恰好对应的是第70百分位数,我们就能知道大约70%的学生的考分比他低,而约30%的学生考分比他高。这里的 p = 70。

  • a : array_like
    Input array or object that can be converted to an array.
  • q : array_like of float
    Percentile or sequence of percentiles to compute, which must be between 0 and 100 inclusive.
  • axis : {int, tuple of int, None}, optional
    Axis or axes along which the percentiles are computed. The default is to compute the percentile(s) along a flattened version of the array.
    Changed in version 1.9.0: A tuple of axes is supported
  • out : ndarray, optional
    Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary.
  • overwrite_input : bool, optional
    If True, then allow the input array a to be modified by intermediate calculations, to save memory. In this case, the contents of the input a after this function completes is undefined.
  • interpolation : {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}
    This optional parameter specifies the interpolation method to use when the desired percentile lies between two data points i < j:
    ‘linear’: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.
    ‘lower’: i.
    ‘higher’: j.
    ‘nearest’: i or j, whichever is nearest.
    ‘midpoint’: (i + j) / 2.
    New in version 1.9.0.
  • keepdims : bool, optional
    If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array a.

3.实例分析

import numpy as np a = np.array([[10, 7, 4], [3, 2, 1]])
print ('我们的数组是:')
print (a)print ('调用 percentile() 函数:')
# 50% 的分位数,就是 a 里排序之后的中位数
print (np.percentile(a, 50)) # axis 为 0,在纵列上求
print (np.percentile(a, 50, axis=0)) # axis 为 1,在横行上求
print (np.percentile(a, 50, axis=1)) # 保持维度不变
print (np.percentile(a, 50, axis=1, keepdims=True))

输出结果

我们的数组是:
[[10  7  4][ 3  2  1]]
调用 percentile() 函数:
3.5
[6.5 4.5 2.5]
[7. 2.]
[[7.][2.]]

4.更多例子

>> a = np.array([[10, 7, 4], [3, 2, 1]])
>>> a
array([[10,  7,  4],[ 3,  2,  1]])
>>> np.percentile(a, 50)
3.5
>>> np.percentile(a, 50, axis=0)
array([6.5, 4.5, 2.5])
>>> np.percentile(a, 50, axis=1)
array([7.,  2.])
>>> np.percentile(a, 50, axis=1, keepdims=True)
array([[7.],[2.]])
>>> m = np.percentile(a, 50, axis=0)
>>> out = np.zeros_like(m)
>>> np.percentile(a, 50, axis=0, out=out)
array([6.5, 4.5, 2.5])
>>> m
array([6.5, 4.5, 2.5])
>>> b = a.copy()
>>> np.percentile(b, 50, axis=1, overwrite_input=True)
array([7.,  2.])
>>> assert not np.all(a == b)

参考:
1.https://docs.scipy.org/doc/numpy/reference/generated/numpy.percentile.html
2.https://www.runoob.com/numpy/numpy-statistical-functions.html

【python】numpy.percentile()函数相关推荐

  1. python numpy 多项式函数 求导求根

    python numpy 多项式函数 求导求根 """求出多项式的 导函数与根 """import numpy as np import m ...

  2. Python numpy.median函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  3. Python numpy.mat函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  4. Python numpy.corrcoef函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  5. Python numpy.atleast_1d函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  6. Python numpy.atleast_2d函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  7. Python numpy.atleast_3d函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  8. Python numpy.var函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  9. Python numpy.digitize函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

  10. Python numpy.vander函数方法的使用

    NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵.NumPy包含很多实用的数学函数,涵盖线性代数运算.傅里叶变换和随 ...

最新文章

  1. LTE-连接态下的DRX
  2. 知乎点赞工具使用教程
  3. 递归算法——汉诺塔问题
  4. linux一些好用的命令和快捷键
  5. nginx http server
  6. mysql 二进制日志大小_mysql二进制日志。
  7. HDU2216:Game III(BFS)
  8. iptables小案例
  9. paip.提升用户体验---c++ 右键菜单以及socket接口
  10. Camera Probe 代码分析
  11. macos系统安装homebrew包管理工具
  12. PS中如何简单、快速更换照片的背景色
  13. 求一个点到任意两个点所在直线距离 C#代码公式
  14. 苹果宣布换芯 背后究竟硬气何在?
  15. 为什么我们放弃了 Vue?Vue 和 React 深度对比
  16. 《每日论文》ImageNet Classification with Deep Convolutional Neural Networks
  17. 桌面计算机没反应是什么意思,点击显示桌面没反应? 显示桌面没反应解决方法...
  18. “双碳”背景下 数据中心气体灭火技术演进方向
  19. C# 打开指定目录并定位到文件
  20. 【pd.to_datetime】时间object转换datetime实例

热门文章

  1. win激活时错误0xc0000022
  2. 绕过安卓SSL验证证书的常见四种方式
  3. 硬件安全技术——芯片安全设计技术1
  4. 华氏度与摄氏度的转化(C语言)
  5. Macbook Tools
  6. Qt学习之解决linux下qt构建出现cannot find -lGL的问题
  7. linux man 命令 详解
  8. lombok导入报错,版本1.18.12已在maven本地仓库中
  9. 全网最新小白API查Q绑定带反查SGK+带接口
  10. 测试工作流程图,你一定要知道的