When you use b = a.reshape((5,4,5)) you just create a different view on the same data used by the array a. (ie changes to the elements of a will appear in b). reshape() does not copy data in this case, so it is a very fast operation. Slicing b and slicing a accesses the same memory, so there shouldn't be any need for a different syntax for the b array (just use a[:10]). If you have created a copy of the data, perhaps with np.resize(), and discarded a, just reshape b: b.reshape((20,5))[:10].

Answer from xnx on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.reshape.html
numpy.reshape โ€” NumPy v2.4 Manual
You can think of reshaping as first raveling the array (using the given index order), then inserting the elements from the raveled array into the new array using the same kind of index ordering as was used for the raveling.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-reshape-python
numpy.reshape() in Python - GeeksforGeeks
January 13, 2025 - In Python, numpy.reshape() function is used to give a new shape to an existing NumPy array without changing its data.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:numpy โ€บ built-in functions โ€บ .reshape()
Python:NumPy | Built-in Functions | .reshape() | Codecademy
May 26, 2025 - The .reshape() method assigns a new shape to a NumPy array without changing its data. It returns a new array object with the specified shape, while maintaining the same data elements of the original array.
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ numpy-reshape
Numpy reshape() - function for reshaping arrays - Flexiple
March 15, 2022 - The numpy.reshape() function allows us to reshape an array in Python. Reshaping basically means, changing the shape of an array.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ reshape
NumPy reshape()
It allows for transforming an array into a new shape while maintaining the same number of elements. The `reshape()` function is used when you need to modify the dimensions of an array to fit a particular structure or to prepare it for operations like matrix multiplication.
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ manipulation โ€บ reshape.php
NumPy: numpy.reshape() function - w3resource
March 24, 2023 - The -1 argument indicates that we want NumPy to automatically determine the number of columns needed based on the total number of elements in the array. The resulting reshaped array has 3 rows and 2 columns, which is the minimum number of columns ...
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ methods โ€บ reshape
NumPy reshape()
Become a certified Python programmer. Try Programiz PRO! ... The reshape() method changes the shape of a NumPy array without changing its data.
๐ŸŒ
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 - As you can see the shape of the ... the original input 2-D array. The np.reshape() function returns the transformed array with the new shape provided in the function....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ reshape-numpy-array
Reshape NumPy Array - Python - GeeksforGeeks
November 18, 2025 - Reshaping in NumPy refers to modifying the dimensions of an existing array without changing its data. The reshape() function is used for this purpose.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_reshape.asp
NumPy Array Reshaping
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.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ np.reshape in python
np.reshape in python | Towards Data Science
January 15, 2025 - We can use -1 in a shape in np.reshape. -1 Is a placeholder and automatically takes the right value so that the input and output shapes end up matching. This is especially helpful if we write a function and we donโ€™t know the exact dimensions of the input array are, but we know for example, that the output should have 2 columns.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ numpy-reshape
Numpy reshape() Function: Python Array Reshaping Guide
January 30, 2024 - Numpy reshape is a versatile function in Pythonโ€™s Numpy library that allows you to change the dimensions of your array without affecting the data it contains. Letโ€™s dive into its basic usage and understand how it works.
๐ŸŒ
Sparrow Computing
sparrow.dev โ€บ home โ€บ blog โ€บ reshaping arrays: how the numpy reshape operation works
Reshaping Arrays: How the NumPy Reshape Operation Works - Sparrow Computing
October 21, 2021 - The NumPy reshape operation changes the shape of an array so that it has a new (but compatible) shape. The rules are: The number of elements stays the same. The order of the elements stays the same[1]. Hereโ€™s a simple example that takes a ...
Top answer
1 of 12
901

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/

๐ŸŒ
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 - Numpy is a powerful library in ... of the most commonly used functions in Numpy is reshape(), which gives a new shape to an array without changing its data....
๐ŸŒ
H2K Infosys
h2kinfosys.com โ€บ blog โ€บ using the numpy reshape and numpy flatten in python
Using the NumPy Reshape and NumPy Flatten in Python
December 17, 2025 - The numpy Reshape and numpy flatten in Python functions are used to change the shape of an array. In this tutorial, we will discuss how to implement them i
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ reshape-numpy-array-in-python
How to Reshape a NumPy Array using np.reshape? -
February 22, 2024 - The reshape function in NumPy allows you to give a new shape to an array without changing its data. It returns a new array with the same data but a different shape. This functionality is particularly useful when working with different dimensions ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ i'm confused about numpy reshape order parameter (c-like vs fortran-like order): is there an eli-15 explanation?
r/learnpython on Reddit: I'm confused about numpy reshape order parameter (C-like vs Fortran-like order): is there an ELI-15 explanation?
February 9, 2022 -

I'm learning numpy and reading the documentation for np.reshape() the order parameter:

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 C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. โ€˜Fโ€™ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the โ€˜Cโ€™ and โ€˜Fโ€™ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. โ€˜Aโ€™ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

Frankly this is like a foreign language to me, and I actually am coming from C as a programmer :?

I was hoping it would be like: if you have a 1d array C will put them in row-wise, and F will put them in column-wise. Obviously it can't be that simple as a general case, but is it that simple if I am reshaping a 1d array into a 2d array?

Anyone have a discussion that is simple at least for the simpler cases like that (I don't need a fully general discussion for nd to nd cases).

Top answer
1 of 2
3
I've been using numpy for years and have never used the order argument on the reshape function. Didn't even know that was a thing. If you have a numpy array (a) that looks like this: a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Then a.shape will return (10,) From here you can use reshape to literally reshape the array into whatever shape you want: a.reshape(5, -1) #returns array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) You could also reshape it like so: a.reshape(-1, 5) # returns array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) In the first case, the new shape is [5, 2], and in the second case the new shape is [2, 5]. -1 indicates to infer that dimension's size based on the size of the array and the other arguments passed to reshape. -1 can only be used as one argument, obviously. And all the arguments have to equal the exact number of values in the array when multiplied together (i.e., I can't a.reshape(3, 4) on an array with 10 elements, but i could on an array with 12 elements). I wouldn't worry too much about the "order" argument unless you are going thru the things above and something doesn't make sense for some particularly esoteric use case.
2 of 2
1
If you are reshaping a 1D array to 2D, Fortran-like (F) will do column-wise and C-like (C) will do row-wise fill. To map it onto the language above if you have a 1D array A and you want to make it a 2D array B. Fortran iterates the first index first: B[1, 1] = A[1]; B[2,1] = A[2]; B[3,1] = A[3]; .... You can see that iterating the first index first results in filling column-wise. C iterates last index first, so: B[1, 1] = A[1]; B[1,2] = A[2]; B[1,3] = A[3]; .... which results in row-wise. Their language generalizes better to higher dimensional arrays. If B was 5 dimensional, Fortran would fill it as: B[1,1,1,1,1] = A[1]; B[2,1,1,1,1] = A[2]; B[3,1,1,1,1] = A[3]; .... And C would fill as: B[1,1,1,1, 1] = A[1]; B[1,1,1,1,2] = A[2]; B[1,1,1,1,3] = A[3]; .... But "row"-wise and "column"-wise aren't as clear here. The option "A" just chooses whichever version is consistent with how data is contiguous in memory for the architecture.
๐ŸŒ
Real Python
realpython.com โ€บ numpy-reshape
Using NumPy reshape() to Change the Shape of an Array โ€“ Real Python
July 21, 2023 - In this tutorial, you'll learn how to use NumPy reshape() to rearrange the data in an array. You'll learn to increase and decrease the number of dimensions and to configure the data in the new array to suit your requirements.