>>> import numpy as np
>>> a=np.random.rand(3,3)
>>> a
array([[0.34466754, 0.81649866, 0.49670435],
[0.48517591, 0.29970508, 0.82610943],
[0.09956289, 0.0032179 , 0.40870012]])
>>> b=np.mat(a)#数组转换成矩阵
>>> b
matrix([[0.34466754, 0.81649866, 0.49670435],
[0.48517591, 0.29970508, 0.82610943],
[0.09956289, 0.0032179 , 0.40870012]])
>>> c=b.tolist()#矩阵转换成列表
>>> c
[[0.3446675379532145, 0.816498659005646, 0.49670434900037175], [0.48517591424371664, 0.29970507744939645, 0.826109430164631], [0.09956289145143449, 0.0032179037025916735, 0.4087001225345782]]
>>> c=np.array(c)
>>> c
array([[0.34466754, 0.81649866, 0.49670435],
[0.48517591, 0.29970508, 0.82610943],
[0.09956289, 0.0032179 , 0.40870012]])
>>> #即实现了从数组到矩阵的转换
>>>
>>> #计算数组矩阵的所有元素的均值
>>> a
array([[0.34466754, 0.81649866, 0.49670435],
[0.48517591, 0.29970508, 0.82610943],
[0.09956289, 0.0032179 , 0.40870012]])
>>> np.mean(a)
0.4200379872783978
>>> #计算每列元素的均值
>>> np.mean(a,axis=0)
array([0.30980211, 0.37314055, 0.5771713 ])
>>> #计算每行元素的均值
>>> np.mean(a,axis=1)
array([0.55262352, 0.53699681, 0.17049364])
>>> #对每一列求标准差
>>> np.std(a, axis=0)
array([0.15934459, 0.33605652, 0.17965491])
>>> #对每一行求标准差
>>> np.std(a, axis=1)
array([0.19664073, 0.21800526, 0.17296885])
>>> a[:,0].min()#取第0列的最小值
0.09956289145143449
>>> a[0,:].max()#取第0行的最大值
0.816498659005646
>>> #
KeyboardInterrupt
>>> #向数组中添加列:np.hstack()
>>> a
array([[0.34466754, 0.81649866, 0.49670435],
[0.48517591, 0.29970508, 0.82610943],
[0.09956289, 0.0032179 , 0.40870012]])
>>> b=np.array([1,2,3]).reshape(3,1)
>>> b
array([[1],
[2],
[3]])
>>> c=np.hstack((a,b))
>>> c
array([[0.34466754, 0.81649866, 0.49670435, 1. ],
[0.48517591, 0.29970508, 0.82610943, 2. ],
[0.09956289, 0.0032179 , 0.40870012, 3. ]])
>>>
矩阵数组互相转换,数组按列增加维度
最新推荐文章于 2023-08-27 22:19:27 发布