之間有什么區別
np.shape(X[1])
和
X.shape[0]
其中 X 是一個陣列。
uj5u.com熱心網友回復:
array.shape并且np.shape(array)是相同的:
See on Numpy Docs
uj5u.com熱心網友回復:
np.shape是一個函式;x.shape是一個屬性,通常回傳相同的內容。
制作一個示例陣列:
In [119]: x = np.ones((1,2,3),int)
In [120]: x
Out[120]:
array([[[1, 1, 1],
[1, 1, 1]]])
In [121]: x.shape
Out[121]: (1, 2, 3) # a tuple
In [122]: x.shape[1]
Out[122]: 2 # select the 2nd element of the tuple
該函式回傳相同的元組:
In [123]: np.shape(x)
Out[123]: (1, 2, 3)
這里x[0]首先執行,回傳一個新陣列:
In [124]: np.shape(x[0])
Out[124]: (2, 3)
In [125]: x[0].shape
Out[125]: (2, 3)
np.shape 可以采用非陣列,例如串列串列
In [126]: np.shape([[1],[2]])
Out[126]: (2, 1)
但是如果你想要第二維,你可以使用size,它只回傳一個數字(取決于可選的軸引數):
In [127]: np.size(x,1)
Out[127]: 2
In [128]: np.size(x)
Out[128]: 6
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345554.html
下一篇:理解python字典排序
