Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

Shape Manipulation & Reshape

Learn how to read array shape, reshape arrays, flatten, add/remove dimensions, and transpose matrices in NumPy.

Understanding shape

shape tells the size of each dimension.

Python
import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)  # (2, 3)

Reshape with .reshape()

Reshape changes the dimension layout without changing the underlying data or copying it — it just reinterprets the same flat block of memory with a new shape.

Python
import numpy as np
 
arr = np.arange(1, 13)  # 1..12
mat = arr.reshape(3, 4) # 3 rows, 4 cols
print(mat)
sketch Reshaping (3, 4) into (4, 3) p5.js
The same 12 values, read out in the same order, just wrapped into a different grid shape.

Using -1 (auto infer)

Python
import numpy as np
 
arr = np.arange(12)
mat = arr.reshape(3, -1)
print(mat.shape)  # (3, 4)

Flattening arrays

.ravel() (often a view)

Python
import numpy as np
 
mat = np.array([[1, 2], [3, 4]])
flat = mat.ravel()
print(flat)

.flatten() (always copy)

Python
import numpy as np
 
mat = np.array([[1, 2], [3, 4]])
flat = mat.flatten()
print(flat)

Add or remove dimensions

np.newaxis / None

Convert a 1D array into a column vector:

Python
import numpy as np
 
arr = np.array([1, 2, 3])
col = arr[:, None]
print(col)
print(col.shape)  # (3, 1)

np.expand_dims

Python
import numpy as np
 
arr = np.array([1, 2, 3])
arr2 = np.expand_dims(arr, axis=0)
print(arr2.shape)  # (1, 3)

np.squeeze (remove size-1 dims)

Python
import numpy as np
 
arr = np.array([[[1], [2], [3]]])
print(arr.shape)           # (1, 3, 1)
print(np.squeeze(arr).shape)  # (3,)

Transpose (swap axes)

Transposing is a special case of reshaping — it also returns a view, never a copy.

For 2D:

Python
import numpy as np
 
mat = np.array([[1, 2, 3], [4, 5, 6]])
print(mat.T)
diagram Shape-changing toolbox mermaid
Different tools for growing, shrinking, or rearranging dimensions without touching the underlying data.

Check your understanding

quizCheck yourself
  1. For arr = np.arange(12), what is arr.reshape(3, -1).shape?

    Show answer

    B — (3, 4) — The -1 tells NumPy to infer that axis. 12 elements in 3 rows means 4 columns.

  2. What is the difference between ravel() and flatten()?

    Show answer

    A — ravel() is often a view while flatten() always returns a copy — ravel returns a view when it can, so editing it may change the original. flatten always makes a copy.

  3. For arr = np.array([1, 2, 3]), what is arr[:, None].shape?

    Show answer

    C — (3, 1) — None (np.newaxis) adds an axis, turning the 1D array into a column vector of shape (3, 1). np.expand_dims(arr, axis=0) would give (1, 3).

  4. What is np.squeeze(arr).shape when arr.shape is (1, 3, 1)?

    Show answer

    A — (3,) — squeeze removes every axis of size 1, leaving just (3,).

Next

Continue to: Broadcasting in NumPy to learn how NumPy applies operations between different shapes.

🧪 Try It Yourself

Exercise 1 – Reshape with -1

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Flatten a Matrix

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Column Vector with newaxis

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did