Numpy中的shape和reshape()

shape是查看數據有多少行多少列
reshape()是數組array中的方法,做用是將數據從新組織數組

1.shapespa

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()  是數組對象中的方法,用於改變數組的形狀。
在這裏插入圖片描述code

形狀變化是基於數組元素不能改變的,變成的新形狀中所包含的元素個數必須符合原來元素個數。若是數組元素髮生變化的時候,就會報錯:對象

reshape新生成數組和原數組公用一個內存,無論改變哪一個都會互相影響。
在這裏插入圖片描述blog

相關文章
相關標籤/搜索