Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -1 mean in numpy reshape?

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?

like image 353
user2262504 Avatar asked Sep 09 '13 03:09

user2262504


People also ask

What is the meaning of reshape (- 1 1?

Artturi Jalli. In NumPy, -1 in reshape(-1) refers to an unknown dimension that the reshape() function calculates for you. It is like saying: “I will leave this dimension for the reshape() function to determine”. A common use case is to flatten a nested array of an unknown number of elements to a 1D array.

What does NumPy reshape mean?

The numpy. reshape() function allows us to reshape an array in Python. Reshaping basically means, changing the shape of an array. And the shape of an array is determined by the number of elements in each dimension. Reshaping allows us to add or remove dimensions in an array.

What is use of reshape () method in the NumPy?

Gives a new shape to an array without changing its data. Array to be reshaped. The new shape should be compatible with the original shape.

What is the meaning of reshape in Python?

Reshaping means changing the shape of an array. The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension.


2 Answers

The criterion to satisfy for providing the new shape is that 'The new shape should be compatible with the original shape'

numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). It simply means that it is an unknown dimension and we want numpy to figure it out. And numpy will figure this by looking at the 'length of the array and remaining dimensions' and making sure it satisfies the above mentioned criteria

Now see the example.

z = np.array([[1, 2, 3, 4],          [5, 6, 7, 8],          [9, 10, 11, 12]]) z.shape (3, 4) 

Now trying to reshape with (-1) . Result new shape is (12,) and is compatible with original shape (3,4)

z.reshape(-1) array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]) 

Now trying to reshape with (-1, 1) . We have provided column as 1 but rows as unknown . So we get result new shape as (12, 1).again compatible with original shape(3,4)

z.reshape(-1,1) array([[ 1],    [ 2],    [ 3],    [ 4],    [ 5],    [ 6],    [ 7],    [ 8],    [ 9],    [10],    [11],    [12]]) 

The above is consistent with numpy advice/error message, to use reshape(-1,1) for a single feature; i.e. single column

Reshape your data using array.reshape(-1, 1) if your data has a single feature

New shape as (-1, 2). row unknown, column 2. we get result new shape as (6, 2)

z.reshape(-1, 2) array([[ 1,  2],    [ 3,  4],    [ 5,  6],    [ 7,  8],    [ 9, 10],    [11, 12]]) 

Now trying to keep column as unknown. New shape as (1,-1). i.e, row is 1, column unknown. we get result new shape as (1, 12)

z.reshape(1,-1) array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]]) 

The above is consistent with numpy advice/error message, to use reshape(1,-1) for a single sample; i.e. single row

Reshape your data using array.reshape(1, -1) if it contains a single sample

New shape (2, -1). Row 2, column unknown. we get result new shape as (2,6)

z.reshape(2, -1) array([[ 1,  2,  3,  4,  5,  6],    [ 7,  8,  9, 10, 11, 12]]) 

New shape as (3, -1). Row 3, column unknown. we get result new shape as (3,4)

z.reshape(3, -1) array([[ 1,  2,  3,  4],    [ 5,  6,  7,  8],    [ 9, 10, 11, 12]]) 

And finally, if we try to provide both dimension as unknown i.e new shape as (-1,-1). It will throw an error

z.reshape(-1, -1) ValueError: can only specify one unknown dimension 
like image 99
Julu Ahamed Avatar answered Dec 21 '22 02:12

Julu Ahamed


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, Numpy gives the option of using -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/

like image 25
Anuj Gupta Avatar answered Dec 21 '22 04:12

Anuj Gupta