shape是查看數據有多少行多少列
reshape()是數組array中的方法,作用是將數據重新組織
1.shape
import numpy as np a = np.array([1,2,3,4,5,6,7,8]) #一維數組 print(a.shape[0]) #值為8,因為有8個數據 print(a.shape[1]) #IndexError: tuple index out of range a = np.array([[1,2,3,4],[5,6,7,8]]) #二維數組 print(a.shape[0]) #值為2,最外層矩陣有2個元素,2個元素還是矩陣。 print(a.shape[1]) #值為4,內層矩陣有4個元素。 print(a.shape[2]) #IndexError: tuple index out of range
2.reshape() 是數組對象中的方法,用於改變數組的形狀。
形狀變化是基於數組元素不能改變的,變成的新形狀中所包含的元素個數必須符合原來元素個數。如果數組元素發生變化的時候,就會報錯:
reshape新生成數組和原數組公用一個內存,不管改變哪個都會互相影響。