shape函數是numpy.core.fromnumeric中的函數,它的功能是讀取矩陣的長度,比如shape[0]就是讀取矩陣第一維度的長度。
shape的輸入參數可以是一個整數(表示維度),也可以是一個矩陣。以下例子可能會好理解一些:
參數是一個數時,返回空:
直接用.shape可以快速讀取矩陣的形狀,使用shape[0]讀取矩陣第一維度的長度
但是當某一維度長度不一致時,讀取所有維度時則不能讀出長短不一致的維度
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print (a.shape)
print(a.shape[0])
print(a.shape[1])
#output
[[1 2 3]
[4 5 6]
[7 8 9]]
(3, 3)
3
3