当前位置: 首页 > 图灵资讯 > 行业资讯> Python中numpy怎样按行或列提取矩阵

Python中numpy怎样按行或列提取矩阵

来源:图灵python
时间: 2024-12-25 17:57:28

今天小编为大家分享Python中numpy按行或列提取矩阵的方法,有需要的小伙伴可以参考一下。

代码:

import numpy as np a=np.arange(9).reshape(3,3)

a Out[31]: array([[0,1,2], [3,4,5], [6, 7, 8]])

矩阵的某一行

a[1] Out[32]:array([3, 4, 5])

矩阵的某一列

a[:,1] Out[33]:array([1, 4, 7])

b=np.eye(3,3) b Out[36]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])

把矩阵a的第2列赋值给矩阵b的第1列

b[:,0]=a[:,1] b Out[38]: array([[ 1., 0., 0.], [ 4., 1., 0.], [ 7., 0., 1.]])

以上就是Python中numpy按行或列提取矩阵的方法,学会的小伙伴可以自己尝试下。

Python中numpy相关知识点推荐阅读:

Python中NumPy的基本概念

Python中用numpy进行图片处理

更多Python学习推荐:Python学习网教学中心

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)