# Python Numpy.mean() - 算术平均数

`numpy.mean()` 函数计算给定数组沿指定轴的算术平均值，或者用通俗的话说–平均数。

## `numpy.mean()` 语法

``````numpy.mean(arr, axis=None, dtype=float64)
``````

### 参数

`arr` array_like

`axis` `None`, `int` 或元素为整数的元组

`axis=0` 表示沿列计算算术平均值，
`axis=1` 表示沿行计算算术平均值。

`dtype` 是指 `dtype``None`

## 示例代码：`numpy.mean()` 与一维数组

``````import numpy as np

arr = [10, 20, 30]
print("1-D array :", arr)
print("Mean of arr is ", np.mean(arr))
``````

``````1-D array : [10, 20, 30]
Mean of arr is  20.0
``````

## 示例代码：`numpy.mean()` 与二维数组

``````import numpy as np

arr = [[10, 20, 30],
[3, 50, 5],
[70, 80, 90],
[100, 110, 120]]

print("Two Dimension array :", arr)
print("Mean with no axis :", np.mean(arr))
print("Mean with axis along column :", np.mean(arr, axis=0))
print("Mean with axis aong row :", np.mean(arr, axis=1))
``````

``````Two Dimension array : [[10, 20, 30], [3, 50, 5], [70, 80, 90], [100, 110, 120]]
Mean with no axis : 57.333333333333336
Mean with axis along column : [45.75 65.   61.25]
Mean with axis aong row : [ 20.          19.33333333  80.         110.        ]
>>
``````

`np.mean(arr)` 将输入数组视为扁平化数组，并计算这个 1-D 扁平化数组的算术平均值。

`np.mean(arr, axis=0)` 计算沿列的算术平均值。

`np.std(arr, axis=1)` 计算沿行的算术平均值。

## 示例代码：`numpy.mean()` 指定了 `dtype`

``````import numpy as np

arr = [10.12, 20.3, 30.28]
print("1-D Array :", arr)
print("Mean of arr :", np.mean(arr))
print("Mean of arr with float32 data :", np.mean(arr, dtype = np.float32))
print("Mean of arr with float64 data :", np.mean(arr, dtype = np.float64))
``````

``````1-D Array : [10.12, 20.3, 30.28]
Mean of arr : 20.233333333333334
Mean of arr with float32 data : 20.233332
Mean of arr with float64 data : 20.233333333333334
``````