网络编程
位置:首页>> 网络编程>> Python编程>> python笔记之mean()函数实现求取均值的功能代码

python笔记之mean()函数实现求取均值的功能代码

作者:perfect_young  发布时间:2022-01-12 07:06:47 

标签:python,mean(),求取均值

用法: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.]])

来源:https://blog.csdn.net/jiangsujiangjiang/article/details/83660368

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com