在numpy中,shape函式讀取矩陣或陣列的長度
使用shape函式可以得出陣列或矩陣的形狀,得出結果是一個tuple格式的資料,之后可以使用shape[]獲取其中的資料 (具體見下面的示例 )
當陣列或矩陣是一維時
import numpy as np
a = np.array([1, 2, 3, 4])
a.shape
a.shape[0]
輸出結果
(4,)
4
shape回傳的是陣列的形狀,是一個tuple格式的資料,
shape[0]回傳的是陣列或矩陣中元素的個數,
此處使用shape[1]則會報錯
當陣列或矩陣是二維時
c1 = np.array([[1, 2],[3,4], [5,6]])
c1.shape #行列形成元組直接輸出
c1.shape[0] #讀取行數
c1.shape[1] #讀取列數
輸出結果
(3, 2)
3
2
當陣列或矩陣是三維時
a = np.array(range(24)).reshape(2,3,4) #構建一個2×3×4的三維陣列
a
a.shape
a.shape[0]
a.shape[1]
a.shape[2]
輸出結果
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
(2, 3, 4)
2
3
4
關于reshape()的用法
https://blog.csdn.net/weixin_46506757/article/details/109545799
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/206461.html
標籤:其他
下一篇:維圖PDMS切圖軟體
