当前位置 博文首页 > 不太冷的莱昂的博客:Python中的shape和reshape()

    不太冷的莱昂的博客:Python中的shape和reshape()

    作者:[db:作者] 时间:2021-09-02 16:34

    1. shape
    import numpy as np
    a = np.array([1,2,3,4,5,6,7,8])  #一维数组
    print(a.shape[0])  #值为8,因为有8个数据
    print(a.shape[1])  #IndexError: tuple index out of range
    
    a = np.array([[1,2,3,4],[5,6,7,8]])  #二维数组
    print(a.shape[0])  #值为2,最外层矩阵有2个元素,2个元素还是矩阵。
    print(a.shape[1])  #值为4,内层矩阵有4个元素。
    print(a.shape[2])  #IndexError: tuple index out of range
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. reshape()
      在这里插入图片描述

    reshape新生成数组和原数组公用一个内存,不管改变哪个都会互相影响。
    在这里插入图片描述

    cs
    下一篇:没有了