矩阵数组互相转换,数组按列增加维度

>>> 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.        ]])
>>> 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值