python计算分位数方法

本文介绍了分位数的概念,包括中位数、四分位数和百分位数,并详细阐述了如何使用Python的numpy库中的np.percentile函数来计算分位数。该函数能够方便地沿着指定轴计算数据集的指定分位数。
部署运行你感兴趣的模型镜像
  1. 分位数(Quantile),亦称分位点,是指将一个随机变量的概率分布范围分为几个等份的数值点,常用的有中位数(即二分位数)、四分位数、百分位数等。
  2. 分位数指的就是连续分布函数中的一个点,这个点对应概率p。若概率0<p<1,随机变量X或它的概率分布的分位数Za,是指满足条件p(X≤Za)=α的实数。
  3. 常见的有二分位数(中位数:观察值有偶数个,中位数不唯一,这个时候取中间的数的平均数作为中位数;其它情况下,中位数就是大小排序后,中间的那个数)、四分位数(从小到大四等份,处于三个分割点位置的数就是四分位数,分别是25%,50%,75%三个四分位数)、百分位数(从小到大排序,计算相应的累计百分位,如常见的PR值)。
  4. 有了这些基本定义之后,如何计算分位数呢,python中使用numpy中的东东可以计算,简单点:就是np.percentile函数。
  5. 函数语法:numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
  6. 功能及返回:沿着指定的轴计算q分位数;并将数组元素中的第q分位数返回。
  7. 参数解释:
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.

New in version 1.9.0.
  1. 挺好用的函数

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值