Advertisement
Advertisement

## What does -1 mean in numpy reshape?

### Question

A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here.

For example:

``````a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)
``````

The result of `b` is: `matrix([[1, 2, 3, 4, 5, 6, 7, 8]])`

Does anyone know what -1 means here? And it seems python assign -1 several meanings, such as: `array[-1]` means the last element. Can you give an explanation?

2019/12/05
1
442
12/5/2019 7:19:59 AM

Used to reshape an array.

Say we have a 3 dimensional array of dimensions 2 x 10 x 10:

``````r = numpy.random.rand(2, 10, 10)
``````

Now we want to reshape to 5 X 5 x 8:

``````numpy.reshape(r, shape=(5, 5, 8))
``````

will do the job.

Note that, once you fix first dim = 5 and second dim = 5, you don't need to determine third dimension. To assist your laziness, python gives the option of -1:

``````numpy.reshape(r, shape=(5, 5, -1))
``````

will give you an array of shape = (5, 5, 8).

Likewise,

``````numpy.reshape(r, shape=(50, -1))
``````

will give you an array of shape = (50, 4)

You can read more at http://anie.me/numpy-reshape-transpose-theano-dimshuffle/

2018/04/23

According to `the documentation`:

newshape : int or tuple of ints

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

2013/09/09

numpy.reshape(a,newshape,order{}) check the below link for more info. https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

for the below example you mentioned the output explains the resultant vector to be a single row.(-1) indicates the number of rows to be 1. if the

``````a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)
``````

output:

matrix([[1, 2, 3, 4, 5, 6, 7, 8]])

this can be explained more precisely with another example:

``````b = np.arange(10).reshape((-1,1))
``````

output:(is a 1 dimensional columnar array)

array([,

``````   ,
,
,
,
,
,
,
,
])
``````

b = np.arange(10).reshape((1,-1))

output:(is a 1 dimensional row array)

array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

2017/01/02

It is fairly easy to understand. The "-1" stands for "unknown dimension" which can should be infered from another dimension. In this case, if you set your matrix like this:

``````a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
``````

Modify your matrix like this:

``````b = numpy.reshape(a, -1)
``````

It will call some deafult operations to the matrix a, which will return a 1-d numpy array/martrix.

However, I don't think it is a good idea to use code like this. Why not try:

``````b = a.reshape(1,-1)
``````

It will give you the same result and it's more clear for readers to understand: Set b as another shape of a. For a, we don't how much columns it should have(set it to -1!), but we want a 1-dimension array(set the first parameter to 1!).

2017/02/27

Long story short: you set some dimensions and let NumPy set the remaining(s).

``````(userDim1, userDim2, ..., -1) -->>

(userDim1, userDim1, ..., TOTAL_DIMENSION - (userDim1 + userDim2 + ...))
``````
2018/12/08

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]