np.reshape(-1)、np.reshape(-1、1)、np.reshape(1、-1)詳細な説明



Np Reshape Np Reshape 1



np.reshape() specification: the new arrangement (shape) should be compatible with the original arrangement

Numpyを使用すると、新しい配置のパラメーターを次のように設定できます。 -1 ((2、-1)、(-1、3)などですが、(-1、-1)は許可されていません)、それは 不明な寸法 、そしてNumpy自体はreshape()を使用します。このメソッドは、出力値がnp.reshape()仕様に準拠するように、未知の次元の値を取得します。

コード1:np.reshape(-1)



original = np.array([2, 4, 1, 3], [1, 2, 5, 2]) orginal.shape # (2, 4) new_1 = orginal.reshape(-1) print(new_1) # The new arrangement is (1, 8) array([2, 4, 1, 3, 1, 2, 5, 2])

コード2:np.reshape(-1、1)

# Set the number of columns in the new arrangement to 1, and the number of rows to be unknown new_2 = orginal.reshape(-1, 1) print(new_2) # The new arrangement is (8, 1) array([[ 2], [ 4], [ 1], [ 3], [ 1], [ 2], [ 5], [ 2]])

コード3:np.reshape(1、-1)



# Set the number of rows in the new arrangement to 1, and the number of columns as unknown new_3 = orginal.reshape(1, -1) print(new_3) # The new arrangement is (1, 8) array([2, 4, 1, 3, 1, 2, 5, 2])

コード4:np.reshape(-1、2)

# Set the number of columns in the new arrangement to 2, and the number of rows to be unknown new_4 = original.reshape(-1, 2) print(new_4) # The new arrangement is (4, 2) array([[2, 4], [1, 3], [1, 2], [5, 2] ])

参照URL:
https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape