NumPy 的 mean(~)
方法計算沿指定軸的平均值。
參數
1. a
| array-like
輸入數組。
2. axis
| None
或int
或tuple
或int
| optional
計算平均值所沿的軸。對於二維數組,允許的值如下:
軸 |
意義 |
---|---|
0 |
逐行計算平均值 |
1 |
逐列計算平均值 |
None |
用於計算平均值的所有值 |
默認情況下,axis=None
。
3. dtype
| string
或 type
| optional
計算平均值期間使用的數據類型。如果輸入是整數,則使用float64
。否則,將使用相同的數據類型。
注意
作為最佳實踐,您應該將 float64
指定為 dtype
,因為如果您的輸入類型為 float32
,則在計算平均值期間將使用 float32
,這會導致結果不太準確。
4. out
| NumPy array
| optional
您可以將計算的平均值放入 out
指定的數組中,而不是創建新數組。
返回值
如果未設置 axis,則返回標量。否則,返回一個 Numpy 數組。
例子
計算一維數組的平均值
np.mean([1,2,3])
2.0
計算二維數組的平均值
考慮以下數組:
a = np.array([[1,2],[3,4]])
a
array([[1, 2],
[3, 4]])
所有值的平均值
np.mean(a)
2.5
每列的平均值
np.mean(a, axis=0)
array([2., 3.])
每行的平均值
np.mean(a, axis=1)
array([1.5, 3.5])
相關用法
- Python Pandas merge_ordered方法用法及代碼示例
- Python NumPy meshgrid方法用法及代碼示例
- Python statistics median_high()用法及代碼示例
- Python statistics median()用法及代碼示例
- Python memoryview.itemsize用法及代碼示例
- Python memoryview.nbytes用法及代碼示例
- Python memoryview.cast用法及代碼示例
- Python memoryview.obj用法及代碼示例
- Python memoryview.hex用法及代碼示例
- Python memoryview()用法及代碼示例
- Python memoryview.toreadonly用法及代碼示例
- Python merge_asof方法用法及代碼示例
- Python NumPy median方法用法及代碼示例
- Python statistics median_low()用法及代碼示例
- Python memoryview用法及代碼示例
- Python memoryview.release用法及代碼示例
- Python memoryview.tolist用法及代碼示例
- Python memoryview.__eq__用法及代碼示例
- Python statistics median_grouped()用法及代碼示例
- Python memoryview.tobytes用法及代碼示例
- Python mxnet.symbol.op.broadcast_logical_xor用法及代碼示例
- Python matplotlib.patheffects.withTickedStroke用法及代碼示例
- Python mxnet.test_utils.get_zip_data用法及代碼示例
- Python mxnet.ndarray.op.uniform用法及代碼示例
- Python mxnet.symbol.op.log_softmax用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | mean method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。