python计算分位数方法

  1. 分位数(Quantile),亦称分位点,是指将一个随机变量的概率分布范围分为几个等份的数值点,常用的有中位数(即二分位数)、四分位数、百分位数等。
  2. 分位数指的就是连续分布函数中的一个点,这个点对应概率p。若概率0
  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. 挺好用的函数

你可能感兴趣的:(python编程记录)