# NumPy统计函数用法和示例

## 本文概述

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

## 从数组中找到最小和最大元素

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

### 例子

``````import numpy as np

a = np.array([[2, 10, 20], [80, 43, 31], [22, 43, 10]])

print("The original array:\n")
print(a)

print("\nThe minimum element among the array:", np.amin(a))
print("The maximum element among the array:", np.amax(a))

print("\nThe minimum element among the rows of array", np.amin(a, 0))
print("The maximum element among the rows of array", np.amax(a, 0))

print("\nThe minimum element among the columns of array", np.amin(a, 1))
print("The maximum element among the columns of array", np.amax(a, 1))``````

``````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()函数

### 例子

``````import numpy as np

a = np.array([[2, 10, 20], [80, 43, 31], [22, 43, 10]])

print("Original array:\n", a)

print("\nptp value along axis 1:", np.ptp(a, 1))

print("ptp value along axis 0:", np.ptp(a, 0))``````

``````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()函数

``numpy.percentile(input, q, axis)``

1. 输入：这是输入数组。
2. q：是数组元素计算得出的百分位数(1-100)。
3. axis：这是要沿其计算百分位数的轴。

### 例子

``````import numpy as np

a = np.array([[2, 10, 20], [80, 43, 31], [22, 43, 10]])

print("Array:\n", a)

print("\nPercentile along axis 0", np.percentile(a, 10, 0))

print("Percentile along axis 1", np.percentile(a, 10, 1))``````

``````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.average()函数

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

### 例子

``````import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print("Array:\n", a)

print("\nMedian of array along axis 0:", np.median(a, 0))
print("Mean of array along axis 0:", np.mean(a, 0))
print("Average of array along axis 1:", np.average(a, 1))``````

• 回顶