• 技术文章 >Python技术 >Python基础教程

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

    小P小P2021-03-25 18:26:13原创13796
    今天小编为大家分享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电脑。)

    专题推荐:numpy
    上一篇:python基础必学:break与continue语句用法 下一篇:python if 条件语句多条件判断怎么做?

    相关文章推荐

    • python中numpy数据类型转换的方法• Python基础:numpy中any()和all()的用法• 基础学习:Python中numpy如何切片• Python中numpy如何索引• python实例:Python中如何删除numpy数组的元素• 详解Python中numpy.loadtxt()读取txt文件• Python中numpy如何生成mask图像• Python中如何使用numpy.getmask()函数• Python中numpy数组如何添加和删除元素

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网