Numpy 统计函数

2024 年 8 月 29 日 | 阅读 3 分钟

Numpy 提供了各种统计函数,用于执行一些统计数据分析。 在本教程的这一部分中,我们将讨论 numpy 提供的统计函数。

查找数组中的最小值和最大值元素

numpy.amin() 和 numpy.amax() 函数用于分别查找指定轴上数组元素的最小值和最大值。

请看以下示例。

示例

输出

The original array:

[[ 2 10 20]
 [80 43 31]
 [22 43 10]]

The minimum element among the array: 2
The maximum element among the array: 80

The minimum element among the rows of array [ 2 10 10]
The maximum element among the rows of array [80 43 31]

The minimum element among the columns of array [ 2 31 10]
The maximum element among the columns of array [20 80 43]

numpy.ptp() 函数

函数 numpy.ptp() 的名称来源于 peak-to-peak。 它用于返回沿轴的值的范围。 考虑以下示例。

示例

输出

Original array:
 [[ 2 10 20]
 [80 43 31]
 [22 43 10]]

ptp value along axis 1: [18 49 33]
ptp value along axis 0: [78 33 21]

numpy.percentile() 函数

使用该函数的语法如下。

它接受以下参数。

  1. input: 它是输入数组。
  2. q: 它是数组元素计算的百分位数 (1-100)。
  3. axis: 它是计算百分位数的轴。

请看以下示例。

示例

输出

Array:
 [[ 2 10 20]
 [80 43 31]
 [22 43 10]]

Percentile along axis 0 [ 6.  16.6 12. ]
Percentile along axis 1 [ 3.6 33.4 12.4]

计算数组项的中位数、平均值和平均值

numpy.median() 函数

中位数 定义为用于将较高范围的数据样本与较低范围的数据样本分开的值。 函数 numpy.median() 用于计算多维或一维数组的中位数。

numpy.mean() 函数

平均值可以通过将数组的所有项目相加并除以数组元素的数量来计算。 我们还可以提及可以计算平均值的轴。

numpy.average() 函数

numpy.average() 函数用于查找多维数组轴的加权平均值,它们的权重在另一个数组中给出。

请看以下示例。

示例


下一个主题NumPy 排序和搜索