• numpy中shape的部分解释


    转载自:https://blog.csdn.net/qq_28618765/article/details/78081959和https://www.jianshu.com/p/e083512e4f4c

    shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度。
    shape的输入参数可以是一个整数(表示维度),也可以是一个矩阵。

    • 参数是一个数时小括号内没有矩阵符号中括号[],返回空:
    >>> import numpy as np
    >>> np.shape(0)
    ()
    
    • 参数是一维矩阵(一个[]):
    >>> import numpy as np
    >>> np.shape([1])
    (1,)
    >>> np.shape([1, 2])
    (2,)
    
    • 参数是二维矩阵(两个[]):
    >>> import numpy as np
    >>> np.shape([[1],[2]])
    (2, 1)
    >>> np.shape([[1, 2], [2, 3], [3, 4]])
    (3, 2)
    
    • 直接用.shape可以快速读取矩阵的形状,使用shape[0]读取矩阵第一维度的长度
    >>> import numpy as np
    >>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    >>> a
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    >>> a.shape
    (3, 3)
    >>> a.shape[0]
    3
    >>> a.shape[1]
    3
    
    • 但是当某一维度长度不一致时,读取所有维度时则不能读出长短不一致的维度
    >>> import numpy as np
    >>> a = np.array([[1, 2, 3], [4, 5], ])
    >>> a.shape
    >>> a.shape[0]
    2
    >>> a.shape[1]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    (2,)

     

     



    链接:https://www.jianshu.com/p/e083512e4f4c

  • 相关阅读:
    camp训练day2
    LCA板子题
    牛客多校第一场
    P1063 能量项链 区间DP
    64. Minimum Path Sum
    46. Permutations
    216. Combination Sum III
    62. Unique Paths
    53. Maximum Subarray
    22. Generate Parentheses
  • 原文地址:https://www.cnblogs.com/Li-JT/p/12195634.html
Copyright © 2020-2023  润新知