python mean()


對於矩陣:

from numpy import  *
a=mat([[2,2,2],[4,4,4]])
print(a.mean())      #所有元素的平均值
print(mean(a))       #所有元素的平均值
print(mean(a,0))     #壓縮行,對各列求平均值
print(mean(a,1))     #壓縮列,對各行求平均值

輸出:<class 'numpy.matrixlib.defmatrix.matrix'>

3.0
3.0
[[3. 3. 3.]]
[[2.]
 [4.]]

 

 對於數組:

from numpy import  *
a=array([[2,2,2],[4,4,4]])
print(a.mean())      #所有元素的平均值
print(mean(a))       #所有元素的平均值
print(mean(a,0))     #壓縮行,對各列求平均值
print(mean(a,1))     #壓縮列,對各行求平均值

 輸出:<class 'numpy.ndarray'>

3.0
3.0
[3. 3. 3.]
[2. 4.]

 


免責聲明!

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



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