numpy中的mean()函數


 

mean() 函數定義: 
numpy.mean(a, axis, dtype, out,keepdims )

mean()函數功能:求取均值 
經常操作的參數為axis,以m * n矩陣舉例:

  • axis 不設置值,對 m*n 個數求均值,返回一個實數
  • axis = 0:壓縮行,對各列求均值,返回 1* n 矩陣
  • axis =1 :壓縮列,對各行求均值,返回 m *1 矩陣

例子: 
1. 數組的操作:

>>> a = np.array([[1, 2], [3, 4]]) >>> a array([[1, 2], [3, 4]]) >>> np.mean(a) 2.5 >>> np.mean(a, axis=0) # axis=0,計算每一列的均值 array([ 2., 3.]) >>> np.mean(a, axis=1) # 計算每一行的均值 array([ 1.5, 3.5]) >>> 

 

2.矩陣的操作

>>> import numpy as np
>>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]]) >>> num1 array([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]) >>> num2 = np.mat(num1) >>> num2 matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]) >>> np.mean(num2) # 對所有元素求均值 3.5 >>> np.mean(num2,0) # 壓縮行,對各列求均值 matrix([[ 2.5, 3.5, 4.5]]) >>> np.mean(num2,1) # 壓縮列,對各行求均值 matrix([[ 2.], [ 3.], [ 4.], [ 5.]]) >>> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM