今天小编为大家分享Python中numpy按行或列提取矩阵的方法,有需要的小伙伴可以参考一下。代码:
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >import numpy as np
a=np.arange(9).reshape(3,3)<br></span></p>
|
1 2 3 4 5 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >a
Out[31]:
array([[0,1,2],
[3,4,5],
[6, 7, 8]])<br></span></p>
|
矩阵的某一行
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >a[1]
Out[32]:array([3, 4, 5])<br></span></p>
|
矩阵的某一列
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >a[:,1]
Out[33]:array([1, 4, 7])<br></span></p>
|
1 2 3 4 5 6 7 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >b=np.eye(3,3)
b
Out[36]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])<br></span></p>
|
把矩阵a的第2列赋值给矩阵b的第1列
1 2 3 4 5 6 7 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >b[:,0]=a[:,1]
b
Out[38]:
array([[ 1., 0., 0.],
[ 4., 1., 0.],
[ 7., 0., 1.]])<br></span></p>
|
以上就是Python中numpy按行或列提取矩阵的方法,学会的小伙伴可以自己尝试下。
Python中numpy相关知识点推荐阅读:
Python中NumPy的基本概念
Python中用numpy进行图片处理
更多Python学习推荐:Python学习网教学中心。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)