π§ βYou donβt just work with data β you reshape it to fit your purpose.β
Welcome to Chapter 7 of your NumPy mastery journey!
So far, you’ve learned how to create, slice, broadcast, and summarize arrays. But what happens when the structure of the data isnβt quite what you need?
In data science and scientific computing, reshaping and rearranging arrays is as important as analyzing them.
Imagine:
-
Turning a 1D array into a matrix
-
Flattening a 2D matrix into a single row
-
Stacking arrays vertically or splitting them into parts
Thatβs what this chapter is all about: reshape, rearrange, and dominate.
π What Weβll Cover
-
reshape()
,flatten()
,ravel()
, andtranspose()
-
Concatenating arrays with
hstack()
andvstack()
-
Splitting arrays into parts
-
Practical use cases in machine learning, image processing, and more
π§± 1. Reshaping Arrays with reshape()
NumPy arrays are flexible β you can change their shape without changing the data.
πΉ Syntax:
π Example:
import numpy as np a = np.array([1, 2, 3, 4, 5, 6]) reshaped = a.reshape((2, 3)) print(reshaped)
Output:
π§» 2. Flattening Arrays
πΉ flatten()
β Always returns a copy
πΉ ravel()
β Returns a view (no copy if possible)
π 3. Transposing with transpose()
and .T
The transpose flips rows and columns. Itβs crucial in:
-
Linear algebra
-
Matrix multiplication
-
Neural networks
π Example:
Output:
[[1 3] [2 4]]
It flips rows β columns
and vice versa.
π 4. Concatenating Arrays
Want to stack arrays side-by-side or top-to-bottom? NumPyβs got you.
πΈ Horizontal Stack: hstack()
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(np.hstack((a, b))) # [1 2 3 4 5 6]
πΈ Vertical Stack: vstack()
Output:
πΈ Column Stack (convert 1D β 2D then stack column-wise)
print(np.column_stack((a, b)))
Output:
[[1 4] [2 5] [3 6]]
π Useful in dataset creation: combining features or labels.
βοΈ 5. Splitting Arrays
Just as you can merge arrays, you can split them too.
πΉ np.split()
β Split into equal parts
πΉ np.hsplit()
and np.vsplit()
β For 2D arrays
π‘ Real-Life Applications of Reshaping and Splitting
Task | Function |
---|---|
Prepare image data for ML | reshape() into (samples, height, width, channels) |
Flatten before training a model | flatten() or ravel() |
Stack features and labels | hstack() , column_stack() |
Create batches of data | split() |
Normalize transposed data | transpose() + mean() |
π¨ Bonus: Reshape with -1 (Let NumPy Calculate)
Output:
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
β οΈ Common Pitfalls to Avoid
Mistake | Fix |
---|---|
Reshaping to incompatible shape | Make sure new_shape.total_elements == original.size |
Assuming ravel() returns a copy |
It returns a view! |
Using reshape() without keeping track of original dimensions |
Always print .shape before and after |
Concatenating arrays of mismatched shape | Ensure axes align properly |
π Summary Table: Reshaping & Manipulation
Function | Purpose |
---|---|
reshape() |
Change shape of an array |
flatten() |
1D copy of array |
ravel() |
1D view of array |
transpose() / .T |
Swap rows and columns |
hstack() / vstack() |
Stack horizontally/vertically |
column_stack() |
Combine column-wise |
split() / hsplit() / vsplit() |
Split arrays into parts |
π Wrapping Up Chapter 7
Congratulations! π
You now hold the power to reshape arrays like a master sculptor.
Whether youβre preparing data for machine learning, formatting images for neural networks, or combining CSV columns β reshaping and manipulating arrays is the secret sauce behind every smart data pipeline.
π In Chapter 8: Advanced Indexing Techniques
Weβll explore:
-
Boolean masks
-
np.where()
,np.select()
-
Fancy slicing and condition-based replacement