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
Answer from Julu Ahamed on Stack Overflow
Top answer
1 of 12
900

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
2 of 12
118

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/

🌐
Coding Blocks
discuss.codingblocks.com › machine learning
What is meaning of reshape(-1,1) - Machine Learning - Coding Blocks Discussion Forum
August 22, 2019 - I know reshape function but i am not able to understand why we are writing (-1,1)
🌐
GeeksforGeeks
geeksforgeeks.org › python › what-does-1-mean-in-numpy-reshape
What does -1 mean in numpy reshape? - GeeksforGeeks
July 23, 2025 - This represents that we do not have to specify any number for one of that dimensions in the reshape method. We pass -1 as the value for the unknown dimension, and NumPy will calculate this unknown.
🌐
Saturn Cloud
saturncloud.io › blog › understanding-the-differences-between-numpy-reshape1-1-and-reshape1-1
Understanding the Differences Between Numpy Reshape(-1, 1) and Reshape(1, -1) | Saturn Cloud Blog
December 27, 2023 - When you use reshape(-1, 1), you are asking numpy to reshape your array with 1 column and as many rows as necessary to accommodate the data.
🌐
YouTube
youtube.com › technologycult
What does numpy reshape(-1,1) and (1,-1) means? - YouTube
What does numpy reshape(-1,1) and (1,-1) means?1. Reshape your data features.reshape(-1, 1) if the dataset has a single feature or column2. Reshape the date ...
Published   November 29, 2021
Views   7K
🌐
YouTube
youtube.com › bhavesh bhatt
What does numpy reshape(-1 1) mean? - YouTube
Reshape your data either X.reshape(-1, 1) if your data has a single feature/column and X.reshape(1, -1) if it contains a single sample. If you are getting th...
Published   March 3, 2019
Views   30K
Find elsewhere
🌐
Gitbook
stephanosterburg.gitbook.io › scrapbook › coding › python › what-does-1-mean-in-numpy-reshape
What does -1 mean in numpy reshape? | scrapbook
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.
🌐
TutorialsPoint
tutorialspoint.com › what-does-1-mean-in-numpy-reshape
What does -1 Mean in Numpy Reshape?
August 17, 2023 - In conclusion, the value -1 in NumPy's reshape() function is used to represent an unknown dimension. It allows NumPy to automatically calculate the size of that dimension based on the other dimensions and the size of the input array. This feature is particularly useful when reshaping arrays ...
Top answer
1 of 2
22

in numpy, creating a matrix of 100X100 items is like this:

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

numpy internally stores all these 10000 items in an array of 10000 items regardless of the shape of this object, this allows us to change the shape of this array into any dimensions as long as the number of items on the array does not change

for example, reshaping our object to 10X1000 is ok as we keep the 10000 items:

x = x.reshape(10, 1000)

reshaping to 10X2000 wont work as we does not have enough items on the list

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

so back to the -1 question, what it does is the notation for unknown dimension, meaning: let numpy fill the missing dimension with the correct value so my array remain with the same number of items.

so this:

x = x.reshape(10, 1000)

is equivalent to this:

x = x.reshape(10, -1) 

internally what numpy does is just calculating 10000 / 10 to get the missing dimension.

-1 can even be on the start of the array or in the middle.

the above two examples are equivalent to this:

x = x.reshape(-1, 1000)

if we will try to mark two dimensions as unknown, numpy will raise an exception as it cannot know what we are meaning as there are more than one way to reshape the array.

x = x.reshape(-1, -1)
ValueError: can only specify one unknown dimension
2 of 2
18

It means, that the size of the dimension, for which you passed -1, is being inferred. Thus,

A.reshape(-1, 28*28)

means, "reshape A so that its second dimension has a size of 28*28 and calculate the correct size of the first dimension".

See documentation of reshape.

🌐
Codingem
codingem.com › home › numpy reshape(-1) meaning
NumPy reshape(-1) Meaning - codingem.com
July 10, 2025 - The -1 in the reshape() function specifies an "unknown dimension". This dimension is then determined automatically by the reshape() function.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.reshape.html
numpy.reshape — NumPy v2.4 Manual
One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions. ... Read the elements of a using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using ...
🌐
Machine Learning Plus
machinelearningplus.com › python › numpy-reshape
Numpy Reshape - How to reshape arrays and what does -1 mean? - Machine Learning Plus
March 8, 2022 - But what if you wish to reshape to an unknown dimension? You can use -1 for the unknown dimension.
🌐
YouTube
youtube.com › watch
Easy Python | Understanding Reshape(-1, 1) and Reshape(1, -1) in Python - YouTube
reshape(-1, 1) reshapes an array to a two-dimensional array with 1 column and as many rows as necessary.reshape(1, -1) reshapes an array to a two-dimensional...
Published   December 29, 2025
🌐
Oreate AI
oreateai.com › blog › unpacking-the-magic-of-numpys-reshape1-1-your-datas-best-friend › 27614c383c3ea0e4a1e579670e739cef
Unpacking the Magic of NumPy's `Reshape(-1, 1)`: Your Data's Best Friend - Oreate AI Blog
January 26, 2026 - So, what does reshape(-1, 1) specifically do? It tells NumPy: "I want my data to be arranged into columns, and I want exactly one column." The -1 then automatically determines how many rows are needed to accommodate all your original elements.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.reshape.html
numpy.reshape — NumPy v2.3 Manual
One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions. ... Read the elements of a using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using ...
🌐
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .reshape()
Python:NumPy | Built-in Functions | .reshape() | Codecademy
May 26, 2025 - .reshape(-1) returns a view of the original array when possible, meaning changes to the reshaped array may affect the original.