Arrays - shape, reshape and flatten

NumPy - The Basics

1 min read

Published Sep 22 2025


11
0
0
0

NumPyPython

shape

  • An attribute that tells you the dimensions (rows, columns, etc.) of an array.
  • Returns a tuple.

Example:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
# → (2, 3) (2 rows, 3 columns)




reshape()

  • Changes the shape of an array without changing its data.
  • The new shape must have the same total number of elements.
  • You can use -1 to let NumPy infer one dimension automatically.

Examples:

arr = np.arange(12)
# [0 1 2 3 4 5 6 7 8 9 10 11]

# Reshape to 3x4
reshaped = arr.reshape(3, 4)
print(reshaped)
# → [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]

# Reshape to 2x2x3
reshaped3d = arr.reshape(2, 2, 3)
print(reshaped3d)
# → [[[ 0 1 2]
# [ 3 4 5]]
# [[ 6 7 8]
# [ 9 10 11]]]

# Let NumPy infer one dimension
auto = arr.reshape(-1, 6)
print(auto.shape)
# → (2, 6)

  • The total number of elements must remain the same.
  • If not, NumPy raises a ValueError.

Example:

arr = np.arange(9)
# 1D array with 9 elements: [0 1 2 3 4 5 6 7 8]

# Attempt to reshape to 2x5 (10 elements) → incompatible
arr.reshape(2, 5)
# → ValueError: cannot reshape array of size 9 into shape (2,5)

  • Reason: You can’t magically create or remove elements.
  • Always make sure that original_size = new_shape_product.
  • For arr.reshape(m, n), m * n must equal arr.size.




flatten()

  • Converts a multi-dimensional array into a 1D array.
  • Returns a copy (so modifying the result won’t affect the original array).

Example:

arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.flatten())
# → [1 2 3 4 5 6]

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact