Run NumPy Online

Reshape a NumPy Array

Reshaping changes the dimensions used to view an array while preserving its values and total element count.

What is Reshape a NumPy Array?

Reshaping changes the dimensions used to view an array while preserving its values and total element count.

Reshape a NumPy array without changing its data.

When should you use it?

  • Perform numerical operations on many values at once.
  • Process arrays, matrices, and scientific data efficiently.
  • Prepare data for data science and machine learning.

Example code

Run code →
main.py
import numpy as np

values = np.arange(1, 7)
matrix = values.reshape(2, 3)

print(matrix)

Expected output

[[1 2 3]
 [4 5 6]]

How it works

Six sequential values can be arranged as two rows by three columns. The requested dimensions must multiply to the original size.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input shape and dtype.
  2. Try negative, floating-point, and missing values.
  3. Apply the operation to larger multidimensional arrays and measure performance.
Run in Python IDE →