《python筆記》mean 函式功能 求取均值

2021-08-30 17:53:57 字數 652 閱讀 3555

用法:mean(matrix,axis=0)  其中 matrix為乙個矩陣,axis為引數

以m * n矩陣舉例:

axis 不設定值,對 m*n 個數求均值,返回乙個實數

axis = 0:壓縮行,對各列求均值,返回 1* n 矩陣

axis =1 :壓縮列,對各行求均值,返回 m *1 矩陣

舉例:

>>>  import numpy as np

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

>>> now2 = np.mat(num1)

>>> now2

matrix([[1, 2, 3],

[2, 3, 4],

[3, 4, 5],

[4, 5, 6]])

>>> np.mean(now2) # 對所有元素求均值

3.5>>> np.mean(now2,0) # 壓縮行,對各列求均值

matrix([[ 2.5,  3.5,  4.5]])

>>> np.mean(now2,1) # 壓縮列,對各行求均值

matrix([[ 2.],

[ 3.],

[ 4.],

[ 5.]])

python中mean函式的使用方法

mean 函式功能 求取均值 經常操作的引數為axis,以m n矩陣舉例 axis 不設定值,對 m n 個數求均值,返回乙個實數 axis 0 壓縮行,對各列求均值,返回 1 n 矩陣 axis 1 壓縮列,對各行求均值,返回 m 1 矩陣 舉例 import numpy as np num1 n...

numpy的sum函式 mean函式

sum函式主要為了求矩陣的行 或者列的和。其中用axis這個引數來指定對行還是列求和,當沒有指定axis引數的時候,就會對矩陣所有元素求和。import numpy as np 生成乙個2維矩陣 a range 16 a np.array a a a.reshape 4,4 0 1 2 3 4 5 ...

numpy中的mean 函式

mean 函式定義 numpy.mean a,axis,dtype,out,keepdims mean 函式功能 求取均值 經常操作的引數為axis,以m n矩陣舉例 axis 不設定值,對 m n 個數求均值,返回乙個實數 axis 0 壓縮行,對各列求均值,返回 1 n 矩陣 axis 1 壓縮...