0PricingLogin
Data Science Academy · Lesson

Reshape and Flatten Arrays

Changing dimensions without losing data.

Same Data, New Shape

An array is just numbers plus a shape. Reshaping rearranges those numbers into new dimensions without copying or changing a single value.

Calling reshape

Use reshape to lay 12 numbers out as a 3 by 4 grid. The total count must stay the same, or NumPy raises an error.

import numpy as np
a = np.arange(12)
b = a.reshape(3, 4)

All lessons in this course

  1. Reshape and Flatten Arrays
  2. Sum, Mean, and the Axis Trick
  3. Boolean Masks for Selection
  4. Random Numbers and Seeds
← Back to Data Science Academy