当前位置 博文首页 > 小旺的博客:Python-NumPy模块-数组的重塑与转置

    小旺的博客:Python-NumPy模块-数组的重塑与转置

    作者:[db:作者] 时间:2021-08-08 16:17

    一维数组的重塑

    将一行或一列的数组转换为多行多列的数组

    from numpy import array
    a=array([1,2,3,4,5,6,7,8,9,10])
    b=a.reshape(5,2)
    c=a.reshape(2,5)
    print(b)
    print(c)
    

    结果:
    在这里插入图片描述

    多维数组的重塑

    from numpy import array
    a=array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
    b=a.reshape(4,3)
    c=a.reshape(2,6)
    print(b)
    print(c)
    

    结果:
    在这里插入图片描述

    将多维数组转换为一维数组

    from numpy import array
    a=array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
    print(a.flatten())
    print(a.ravel())
    

    结果:
    在这里插入图片描述

    数组的转置

    from numpy import array
    from numpy.ma import transpose
    a=array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
    print(a)
    print(a.T)
    b=transpose(a)
    print(b)
    

    结果:
    在这里插入图片描述

    cs
    下一篇:没有了