reshape operation in numpy

numpy.reshape(-1,1)

The new shape attribute of the array should match the original value. If it is equal to -1, then Numpy will calculate another shape attribute value of the array according to the remaining dimensions.
for example:

x = np.array([[2, 1], [2, 1], [2, 3]])

Specifying the new array row is 3 and the column is 2, then:

y = x.reshape(3,2)
 
y
Out[43]: 
array([[2, 1],
       [2, 1],
       [2, 3]])
       

Specify the new array column as 1, then:

y = x.reshape(-1,1)
 
y
Out[34]: 
array([[2],
       [1],
       [2],
       [1],
       [2],
       [3]])

Specifying the new array column is 2, then:

y = x.reshape(-1,2)
 
y
Out[37]: 
array([[2, 1],
       [2, 1],
       [2, 3]])

Specifying a new array row of 1, then:

y = x.reshape(1,-1)
 
y
Out[39]: array([[2, 1, 2, 1, 2, 3]])

Specifying new array behavior 2, then:

y = x.reshape(2,-1)
 
y
Out[41]: 
array([[2, 1, 2],
       [1, 2, 3]])

This is just an example of a two-dimensional array, and the operations above two-dimensional are also done in the same way. In short, in addition to specifying the dimension of the reshape, the dimension of the -1 operation is to be determined, and is derived from other given dimensions.

Guess you like

Origin blog.csdn.net/qq_52302919/article/details/123723471