Reshape and Flatten Arrays
Changing dimensions without losing data.
Reshape and Flatten Arrays is a free Data Science Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)Rows Times Columns
The new shape only works if the dimensions multiply to the original size. Here 3 times 4 equals 12, so the reshape fits perfectly.
Let NumPy Do the Math
Pass -1 for one dimension and NumPy computes it for you. It is a handy shortcut when you only know one side of the grid.
a = np.arange(12)
b = a.reshape(3, -1) # becomes 3 x 4Going to 1D With ravel
Need a flat line of numbers again? ravel collapses any array into a single dimension, reading values row by row.
grid = np.arange(6).reshape(2, 3)
flat = grid.ravel() # [0 1 2 3 4 5]flatten Makes a Copy
The flatten method also returns 1D, but always as a fresh copy. Editing the result never touches the original array.
flat = grid.flatten()
flat[0] = 99 # grid is untouchedravel May Share Memory
Unlike flatten, ravel often returns a view that shares memory with the source. Change the view and the original can change too.
Add a Dimension
A 1D array can grow into a column with reshape. Turning shape (3,) into (3, 1) is common before stacking or matrix math.
v = np.array([1, 2, 3])
col = v.reshape(3, 1)Transpose Flips Axes
The .T attribute swaps rows and columns. A 2 by 3 array instantly becomes 3 by 2, mirroring the data across its diagonal.
grid = np.arange(6).reshape(2, 3)
turned = grid.T # shape 3 x 2Shape Must Stay Consistent
You cannot reshape 10 numbers into a 3 by 4 grid. The element count on both sides must match, so plan dimensions before you reshape.
Why Reshaping Matters
Models and plotting tools expect specific shapes. Reshaping lets one block of numbers serve a vector, a matrix, or an image as needed.
Quick Check
You want a guaranteed independent 1D copy of an array.
Reshape Recap
You reshaped data while keeping its values, used -1 for auto sizing, and learned that flatten copies while ravel may share memory. 🎯
Frequently asked questions
Is the “Reshape and Flatten Arrays” lesson free?
Yes — the full text of “Reshape and Flatten Arrays” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reshape and Flatten Arrays”?
Changing dimensions without losing data. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reshape and Flatten Arrays” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reshape and Flatten Arrays
- Sum, Mean, and the Axis Trick
- Boolean Masks for Selection
- Random Numbers and Seeds