What does -1 mean in numpy reshape?

 I have a numpy array (A) of shape = (100000, 28, 28)
 I reshape it using A.reshape(-1, 28x28)

This is a very common use in machine channels. How it works? I never understood the meaning of "-1" in the change.

Exact question. But no solid explanation. Any answers pls?

+4
source share
2 answers

This means that the size of the measurement for which you have passed -1is displayed. In this way,

A.reshape(-1, 28*28)

means, "reseape Aso that its second dimension is 28 * 28 and calculates the correct size of the first dimension."

See reformatting documentation .

+10
source

numpy, 100X100 :

import numpy as np
x = np.ndarray((100, 100))
x.shape  # outputs: (100, 100)

numpy 10000 10000 , ,

, 10X1000 , 10000 :

x = x.reshape((10, 1000))

10X2000 , .

x.reshape((10, 2000))
ValueError: total size of new array must be unchanged

-1, , , : numpy , .

:

x = x.reshape((10, 1000))

:

x = x.reshape((10, -1)) 

, numpy , 10000 / 10, .

-1 .

:

x = x.reshape((-1, 1000)) 

, numpy , , , .

x = x.reshape((-1, -1))
ValueError: can only specify one unknown dimension
+8

Source: https://habr.com/ru/post/1667414/


All Articles