numpy reshape-1パラメーター



Numpy Reshape 1 Parameter



Reshapeは配列の形を変えることですが、今日、他の人のコードにはnp.reshape(z、[-1])のようなパラメーターがあります。もっとよく見なさい。

による 見積もり 例:https://blog.csdn.net/weixin_39449570/article/details/78619196



>>> z = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]]) >>> print(z) [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] >>> print(z.shape) (4, 4) >>> print(z.reshape(-1)) [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] >>> print(z.reshape(-1,1)) #We don’t know what the shape attribute of z is, #But want to make z have only one column, the number of rows is unknown, #Through `z.reshape(-1,1)`, Numpy automatically calculates that there are 16 rows, #The new array shape attribute is (16, 1), which matches the original (4, 4). [[ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16]] >>> print(z.reshape(2,-1)) [[ 1 2 3 4 5 6 7 8] [ 9 10 11 12 13 14 15 16]]

自分で書いた例:

>>> z = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]]) >>>print(z) [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] >>>z.reshape(-1) array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) >>>print(z) # Note that reshape does not change the dimension of z itself [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] >>>np.reshape(z,[-1]) array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) >>> print(z) # Note that reshape does not change the dimension of z itself [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] >>>np.reshape(z,[1,-1]) array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]) >>>z1=np.reshape(z,[-1]) #[-1] This way of passing parameters is to reshape the array into an array with the number of rows and the number of columns unknown. >>>z1 array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) >>>z1.shape (16,)
>>>z2=np.reshape(z,[-1,1]) #[-1,1] This kind of parameter is reshaped into 1 column >>>z2 array([[ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [10], [11], [12], [13], [14], [15], [16]]) >>>z2.shape (16, 1) >>>z3=np.reshape(z,[1,-1]) >>>z3 array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]) >>>z3.shape (1, 16)

一般に:



[1,2]の形状値(2、)は、配列内に2つの要素がある1次元配列を意味します。

[[1]、[2]]の形状値は(2,1)であり、これは 二次元配列 、各行には1つの要素があります。

[[1,2]]の形状値は(1、2)であり、これは 二次元配列 、各行には2つの要素があります。