NumPy reshape(): How to Reshape Arrays in Python
Learn how to reshape NumPy arrays with explicit dimensions or -1, choose C or F order, and understand when reshape returns a view or copy.

Use arr.reshape(new_shape) or np.reshape(arr, new_shape) to give a NumPy array a new shape while keeping its values and traversal order. The requested dimensions must multiply to the original number of elements, unless one dimension is -1 so NumPy can infer it. Reshape does not transpose axes. By default, it reads and fills values in C order, where the last index changes fastest.
This guide covers the method and function forms, shape arithmetic, inferred dimensions, C/F/A order, views and copies, common mistakes, and practical troubleshooting. The examples use NumPy 2.x syntax; shape is preferred over the deprecated newshape keyword.
1. How do I reshape a NumPy array?
Import NumPy, create or load an array, then call reshape with a compatible shape. For six values, for example, shapes such as (2, 3) and (3, 2) are valid because each has six positions.

import numpy as np
values = np.arange(6)
rows_and_columns = values.reshape(2, 3)
print(values) # [0 1 2 3 4 5]
print(rows_and_columns)
# [[0 1 2]
# [3 4 5]]
print(rows_and_columns.shape) # (2, 3)
The call returns a new array object with the requested shape. It does not change the shape of values in place. The underlying data may be shared with the original or copied, depending on the array’s strides and requested order.
Method syntax and function syntax
The method form is concise when you already have an array. The top-level function is useful when writing a transformation around an input value or when you want to specify function options such as order and copy.
import numpy as np
arr = np.arange(6)
a = arr.reshape((2, 3))
b = np.reshape(arr, (2, 3))
assert np.array_equal(a, b)
With the method, dimensions can be passed separately or as one tuple: arr.reshape(2, 3) and arr.reshape((2, 3)). Using a tuple often makes the target shape clear when it is stored in a variable.
2. How do I reshape an array to rows and columns?
Choose the row and column counts so their product equals arr.size. For a 12-element array, (3, 4) produces three rows of four values and (4, 3) produces four rows of three values.
import numpy as np
arr = np.arange(12)
matrix = arr.reshape(3, 4)
print(matrix)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
For a multidimensional target, the same product rule applies. An array with 30 values can become (2, 5, 3), because 2 × 5 × 3 = 30. The number of dimensions is not restricted to two.
Use -1 to infer one dimension
Put -1 in exactly one dimension when you want NumPy to calculate its size from the total number of elements and the other dimensions.
import numpy as np
arr = np.arange(30)
by_two_groups = arr.reshape(2, -1, 3)
print(by_two_groups.shape) # (2, 5, 3)
Here, the inferred dimension is five because 2 × 5 × 3 = 30. Only one dimension can be inferred: arr.reshape(-1, -1) is ambiguous. Inference does not pad or discard values; it only fills in a dimension that makes the element count match.
3. What does order mean in NumPy reshape?
The order option controls the index traversal used to read values from the input and place them into the output. It is about indexing order, not a guarantee of the returned array’s physical memory layout.
| Order | Traversal | When to use it |
|---|---|---|
'C' (default) |
Last index changes fastest, like row-by-row traversal. | Most ordinary Python and NumPy reshaping. |
'F' |
First index changes fastest, like column-by-column traversal. | Matching data or conventions that specify Fortran-style indexing. |
'A' |
Uses F indexing if the input is Fortran contiguous; otherwise C indexing. | When traversal should follow the input’s contiguity convention. |
For a concrete comparison, start with a 3-by-2 array and reshape it to 2-by-3:
import numpy as np
x = np.array([[0, 1],
[2, 3],
[4, 5]])
print(np.reshape(x, (2, 3)))
# [[0 1 2]
# [3 4 5]]
print(np.reshape(x, (2, 3), order='F'))
# [[0 4 3]
# [2 1 5]]
The values differ because the traversal order differs. order='F' does not simply mean “make this array column-major in memory.” If you need a particular contiguous layout for another library, check the result’s flags or explicitly create the required layout.
4. Does NumPy reshape return a view or a copy?
It can return a view when the existing data and strides support the requested shape. If they do not, NumPy may copy the data. Do not assume every reshape is zero-copy, and do not assume the result is always independent of the original.

This matters if you mutate either object later. Use np.shares_memory on the arrays in question when sharing matters to your program:
import numpy as np
arr = np.arange(12)
view_or_result = arr.reshape(3, 4)
print(np.shares_memory(arr, view_or_result))
For np.reshape, the current API has a copy option:
| Value | Behavior |
|---|---|
None (default) |
Copy only if required by the requested order. |
True |
Always make a copy. |
False |
Require a no-copy reshape; raise ValueError if it cannot be done. |
import numpy as np
arr = np.arange(12)
independent = np.reshape(arr, (3, 4), copy=True)
# Raises ValueError if this requested reshape would need a copy:
no_copy = np.reshape(arr, (3, 4), copy=False)
These options are available in current NumPy. If you need to support older NumPy versions, check the version-specific API before relying on copy. The returned array is not guaranteed to be C- or Fortran-contiguous just because you selected a reshape order.
5. Reshape versus transpose, resize, and flatten
These operations solve different problems, and substituting one for another can silently change the meaning of data.
| Operation | What it does |
|---|---|
reshape |
Returns an array object with a different shape while preserving values in the selected traversal. |
transpose or .T |
Permutes axes; it changes which axis is in each position. |
resize |
Changes an array’s shape and size in place. |
ravel |
Flattens an array to one dimension; that flattened traversal can then be reshaped. |
For example, transposing a 2-by-3 array gives a 3-by-2 array with its axes swapped. Reshaping the same six values to 3-by-2 instead reads and fills values according to the selected order. The resulting shapes may match while the arrangements differ.
import numpy as np
x = np.array([[0, 1, 2],
[3, 4, 5]])
print(x.T) # axes swapped
print(x.reshape(3, 2)) # values traversed in C order
6. Complete examples for arrays you already have
Reshape loaded data after checking its size
When reading data from a file or another library, inspect the number of elements before choosing a target shape. This small pattern gives a clear error at the point where the assumption is made.
import numpy as np
arr = np.asarray([10, 11, 12, 13, 14, 15])
rows, columns = 2, 3
if arr.size != rows * columns:
raise ValueError(
f"Expected {rows * columns} values, got {arr.size}"
)
matrix = arr.reshape(rows, columns)
print(matrix)
Reshape a non-contiguous slice
Slicing can create an array with nonstandard strides. A reshape may still work, but whether it shares memory depends on the actual layout and target. Check rather than infer from how the array was created.
import numpy as np
base = np.arange(24).reshape(4, 6)
sliced = base[:, ::2]
result = sliced.reshape(2, 6)
print(sliced.shape) # (4, 3)
print(result.shape) # (2, 6)
print(np.shares_memory(sliced, result))
7. Troubleshooting common reshape errors
| Symptom | Likely cause | Fix |
|---|---|---|
ValueError: cannot reshape array |
The requested dimensions do not multiply to the source element count. | Print arr.size and calculate the target product. Use -1 for one unknown dimension. |
| Cannot infer shape / invalid inferred dimensions | More than one dimension is -1, or known dimensions cannot divide the element count. |
Use one -1 only and verify the product of the other dimensions. |
| Values appear in an unexpected order | Reshape was mistaken for transpose, or the traversal order differs from the data convention. | Check whether axes need permutation; use order='F' only when F traversal is intended. |
| Mutating the result also changes the input | The reshape result is a view sharing memory. | Use copy=True with np.reshape when independent storage is required. |
copy=False raises |
The requested shape/order cannot be represented without copying. | Allow a copy with copy=None, or redesign the downstream operation around the existing strides. |
| Downstream code rejects array layout | Reshape order does not promise memory contiguity. | Inspect arr.flags; explicitly make a contiguous array if the downstream API requires one. |
Code rejects newshape or emits a deprecation warning |
The keyword was deprecated in NumPy 2.1 in favor of shape. |
Use np.reshape(a, shape=(...)) or the method form. |
8. Performance, reliability, and cost notes
A reshape that can be represented with compatible strides is typically a lightweight view operation; one that cannot be represented that way needs a data copy, which takes time and additional memory proportional to the array size. Large arrays make the difference more noticeable. If memory pressure or latency matters, inspect sharing and benchmark with representative arrays rather than assuming the operation is free.
Reshape does not validate the semantic meaning of dimensions. A shape can be mathematically valid but wrong for your model, image, or file format. Name dimensions, check expected sizes at boundaries, and test a small example whose value arrangement is easy to verify. NumPy itself has no per-call charge for reshape; the practical cost is compute time and memory use in your application.
9. Browser screenshots for documentation and debugging
Developers sometimes need a screenshot of documentation, a bug report, or a rendered data view alongside code examples. If your workflow uses an actual browser, you can set one up locally; when you only need the image or PDF, ScreenshotNeo provides a website screenshot API and MCP server from ScreenshotNeo. Its request options include viewport and device presets, full-page or CSS-element capture, waits, custom CSS/JavaScript, cookies, headers, and PDF output. See the API documentation for parameters and setup.
Or skip the browser setup
Make a GET request with a URL to receive a screenshot. This runnable cURL example saves a WebP image:
curl -G "https://api.screenshotneo.com/v1/shot" \
-d access_key=YOUR_API_KEY \
--data-urlencode url=https://numpy.org/doc/stable/reference/generated/numpy.reshape.html \
-o reshape-docs.webp
Python version:
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={
"access_key": "YOUR_API_KEY",
"url": "https://numpy.org/doc/stable/reference/generated/numpy.reshape.html",
},
timeout=90,
)
r.raise_for_status()
open("reshape-docs.webp", "wb").write(r.content)
Node.js version:
const q = new URLSearchParams({
access_key: 'YOUR_API_KEY',
url: 'https://numpy.org/doc/stable/reference/generated/numpy.reshape.html'
});
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await import('node:fs/promises').then(fs => fs.writeFile('reshape-docs.webp', Buffer.from(await res.arrayBuffer())));
ScreenshotNeo accepts cookie or consent banners as a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server gives AI agents tools named take_screenshot, get_page_info, and capture_pdf. The Free plan includes 1,000 shots each month with no card; paid plans start at $5 for 3,000 shots, and every feature is on every plan. Create a free account for 1,000 screenshots a month, no card required.
10. NumPy reshape FAQ
Does reshape change the original array?
No. It returns another array object. The result may share the original’s data, so check memory sharing if later mutations matter.
Can I reshape an empty array?
Yes, when the requested dimensions are compatible with zero elements. Be careful with -1 when the remaining dimensions do not provide enough information to infer a unique size.
Can reshape add or remove values?
No. Reshape reorganizes the existing element count. Use a different operation if you need padding, truncation, or data generation.
Should I use arr.reshape or np.reshape?
Both are standard forms. Use the method when working directly with an array; use the function when its options make the transformation clearer.


