在 Matplotlib 中使用 “for i in range(Y.shape[0])” 時,.shape[] 有什麼作用?


shape 屬性通常用於獲取陣列的當前形狀,但也可以透過為其分配陣列維數元組來就地重塑陣列。

步驟

  • 使用 np.array 方法獲取陣列 Y.

  • Y.shape 將返回一個元組 (4, ).

  • Y.shape[0] 方法將返回 4,即元組的第一個元素。

示例

import numpy as np

Y = np.array([1, 2, 3, 4])
print("Output of .show method would be: ", Y.shape, " for ", Y)
print("Output of .show[0] method would be: ", Y.shape[0], " for ", Y)
print("Output for i in range(Y.shape[0]): ", end=" ")
for i in range(Y.shape[0]):
   print(Y[i], end=" ")

輸出

Output of .show method would be: (4,) for [1 2 3 4]
Output of .show[0] method would be: 4 for [1 2 3 4]
Output for i in range(Y.shape[0]): 1 2 3 4

更新時間:17-Mar-2021

938 檢視

啟動你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.