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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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.
🌐
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....
🌐
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 ...
🌐
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.
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/

🌐
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....
🌐
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 ...
🌐
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
🌐
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.
🌐
Sharp Sight
sharpsight.ai › blog › numpy-reshape-python
How to use Numpy reshape - Sharp Sight
July 24, 2021 - NumPy arrays are an important component of the Python data science ecosystem. When working with NumPy arrays, you’re going to need to be able to perform basic data manipulation. In particular, you may need to change the “shape” of the data; you may need to change how the data are arranged in the NumPy array. To do this, you can use the NumPy reshape ...