NumPy percentile()

percentile() 方法沿指定轴计算数据的第 q 个百分位数。

示例

import numpy as np

# create an array
array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7])

# calculate the 25th, 50th and 75th percentile of the array p25 = np.percentile(array1, 25) p50 = np.percentile(array1, 50) p75 = np.percentile(array1, 75)
print(p25, p50, p75) # Output: 1.75 3.5 5.25

percentile() 语法

percentile() 的语法是

numpy.percentile(array, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, interpolation=None)

percentile() 参数

percentile() 方法接受以下参数

  • array - 输入数组(可以是 array_like
  • q- 要查找的 q-th 百分位数(可以是 floatarray_like
  • axis(可选)- 计算均值的轴或轴(inttuple of int
  • out(可选)- 用于存放结果的输出数组(ndarray
  • keepdims(可选)- 指定是否保留原始数组的形状(bool
  • override_input(可选)- 确定中间计算是否可以修改数组的 bool
  • method(可选)- 要使用的插值方法
  • interpolation(可选)- method 关键字参数的已弃用名称

注意事项

的默认值是

  • axis = None,即取整个数组的百分位数。
    • 默认情况下,keepdimsoverride_input 将为 False
    • 插值方法是 'linear'
    • 如果输入包含小于 float64 的整数或浮点数,则输出数据类型为 float64。否则,输出数据类型与输入数据类型相同。

percentile() 返回值

percentile() 方法沿指定轴返回输入数组的 q-th 百分位数。


百分位数

百分位数是一种统计量,表示数据低于该值的比例。它有助于分析数据集的分布。

在 NumPy 中,percentile() 函数计算沿指定轴的 q-th 百分位数。

q-th 百分位数表示低于该值的 q% 的数据。例如,50-th 百分位数(也称为中位数)将数据分成相等的两半,其中 50% 的值低于它。


示例 1:查找 ndArray 的百分位数

import numpy as np

# create an array
array1 = np.array([[[0, 1],[2, 3]],
   [[4, 5],[6, 7]]])

# find the 50th percentile of entire array percentile1 = np.percentile(array1, q=50) # find the 50th percentile across axis 0 percentile2 = np.percentile(array1, q=50, axis=0) # find the 50th percentile across axis 0 and 1 percentile3 = np.percentile(array1, q=50, axis=(0, 1))
print('\n50th Percentile of the entire array:', percentile1) print('\n50th Percentile across axis 0:\n', percentile2) print('\n50th Percentile across axis 0 and 1:', percentile3)

输出

50th Percentile of the entire array: 3.5

50th Percentile across axis 0:
[[2. 3.]
 [4. 5.]]

50th Percentile across axis 0 and 1:[3. 4.]

示例 2:使用 out 将结果存储在所需位置

out 参数允许指定一个输出数组,结果将存储在该数组中。

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# create an output array
output = np.zeros(3)

# compute 25th percentile and store the result in the output array
np.percentile(arr, 25, out = output, axis = 0)

print('25th Percentile:', output)

输出

25th Percentile: [1.75 2.75 3.75]

示例 3:使用可选的 keepdims 参数

如果 keepdims 设置为 True,则结果均值数组与原始数组的维数相同。

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# keepdims defaults to False result1 = np.percentile(arr, 50 , axis = 0) # pass keepdims as True result2 = np.percentile(arr, 50, axis = 0, keepdims = True)
print('Dimensions in original array:', arr.ndim) print('Without keepdims:', result1, 'with dimensions', result1.ndim) print('With keepdims:', result2, 'with dimensions', result2.ndim)

输出

Dimensions in original array: 2
Without keepdims: [2.5 3.5 4.5] with dimensions 1
With keepdims: [[2.5 3.5 4.5]] with dimensions 2

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战