/Curriculum/AdvancedArray Transformation

44. Advanced Array ManipulationAdvanced

Master np.tile(), np.repeat(), np.roll(), np.flip(), np.rot90(), and np.r_ / np.c_.

15 mins

Concept Overview

Advanced manipulation functions provide geometric transformations: `np.tile` repeats patterns as tiles, `np.repeat` duplicates individual elements, and `np.rot90` rotates matrices by 90 degrees.

Hardware Mental Model

`tile` is laying down duplicate square floor tiles. `repeat` is stuttering every single number twice (`[1, 1, 2, 2]`). `rot90` is turning a photo 90 degrees counter-clockwise.

Key Concepts (1)Click snippet to load in editor

`np.repeat([1, 2], 2)` produces `[1, 1, 2, 2]`. `np.tile([1, 2], 2)` produces `[1, 2, 1, 2]`.

t = np.tile(arr, 3); r = np.repeat(arr, 3)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:np.tile(arr, 2) # when expecting [1, 1, 2, 2]
✓ Correct:np.repeat(arr, 2)

Remember: `repeat` duplicates each item consecutively; `tile` repeats the full pattern block.

Pro Tip: Use `repeat` for element replication and `tile` for grid tiling.

📌 Quick Revision

Core takeaway points from this topic
`np.repeat(arr, N)`: Repeats each element N times consecutively.
`np.tile(arr, N)`: Repeats the entire array block N times.
`np.rot90(mat, k=1)`: Rotates 2D matrix by 90 degrees.
`np.flip(arr, axis)`: Reverses elements along specified axis.
Editor: 44. Advanced Array Manipulation
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Repeat Array Elements

Create `arr = np.array([10, 20])`, repeat each element 3 times with `np.repeat(arr, 3)`, and print.

Expected Output Target:[10 10 10 20 20 20]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state