π§© Combining and Splitting Arrays in NumPy
The LEGO Story
Imagine you have LEGO blocks. Sometimes you want to snap blocks together to make something bigger. Other times, you want to break apart a big creation into smaller pieces. And sometimes, you want to copy the same pattern over and over.
NumPy arrays work exactly like LEGOs!
π Combining Arrays
What Does βCombiningβ Mean?
You have two small toy trains. You connect them to make one long train!
import numpy as np
train1 = np.array([1, 2, 3])
train2 = np.array([4, 5, 6])
# Connect them end-to-end
long_train = np.concatenate([train1, train2])
print(long_train)
# Output: [1 2 3 4 5 6]
Three Ways to Combine
1. concatenate β Join Along Existing Axis
Like adding more train cars to your train.
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
# Stack vertically (axis=0)
result = np.concatenate([a, b], axis=0)
print(result)
# [[1 2]
# [3 4]
# [5 6]]
2. vstack β Stack Vertically (On Top)
Stack pancakes on a plate!
pancake1 = np.array([1, 2, 3])
pancake2 = np.array([4, 5, 6])
stack = np.vstack([pancake1, pancake2])
print(stack)
# [[1 2 3]
# [4 5 6]]
3. hstack β Stack Horizontally (Side by Side)
Put two photos next to each other in an album.
photo1 = np.array([[1], [2]])
photo2 = np.array([[3], [4]])
album = np.hstack([photo1, photo2])
print(album)
# [[1 3]
# [2 4]]
Quick Memory Trick
| Function | Direction | Think Of |
|---|---|---|
vstack |
β¬οΈ Down | Stacking plates |
hstack |
β‘οΈ Right | Books on shelf |
concatenate |
Any axis | Flexible glue |
βοΈ Splitting Arrays
What Does βSplittingβ Mean?
You have a chocolate bar with 6 squares. You break it into smaller pieces to share!
chocolate = np.array([1, 2, 3, 4, 5, 6])
# Break into 3 equal pieces
pieces = np.split(chocolate, 3)
print(pieces)
# [array([1, 2]), array([3, 4]), array([5, 6])]
Three Ways to Split
1. split β Equal Parts
Like cutting a pizza into equal slices.
pizza = np.array([1, 2, 3, 4, 5, 6])
# Cut into 2 equal halves
halves = np.split(pizza, 2)
print(halves)
# [array([1, 2, 3]), array([4, 5, 6])]
2. vsplit β Split Vertically (Rows)
Cut a sandwich into top and bottom layers.
sandwich = np.array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
layers = np.vsplit(sandwich, 2)
print(layers[0]) # Top half
# [[1 2]
# [3 4]]
3. hsplit β Split Horizontally (Columns)
Tear a page in half left-to-right.
page = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
halves = np.hsplit(page, 2)
print(halves[0]) # Left half
# [[1 2]
# [5 6]]
Split at Specific Positions
Donβt want equal pieces? Tell NumPy WHERE to cut!
ribbon = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# Cut at positions 2 and 5
pieces = np.split(ribbon, [2, 5])
print(pieces)
# [array([1, 2]),
# array([3, 4, 5]),
# array([6, 7, 8])]
π Tiling and Repeating
What Does βTilingβ Mean?
Think of bathroom tiles. You take ONE tile design and repeat it to cover the whole floor!
tile β Copy the Whole Pattern
tile_design = np.array([1, 2])
# Repeat 3 times
floor = np.tile(tile_design, 3)
print(floor)
# [1 2 1 2 1 2]
Tile in 2D β Making a Grid
pattern = np.array([[1, 2],
[3, 4]])
# Repeat 2 rows, 3 columns
wallpaper = np.tile(pattern, (2, 3))
print(wallpaper)
# [[1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]]
repeat β Copy Each Element
Different from tile! Each item gets copied individually.
beads = np.array([1, 2, 3])
# Each bead appears 2 times
necklace = np.repeat(beads, 2)
print(necklace)
# [1 1 2 2 3 3]
Tile vs Repeat β The Big Difference
colors = np.array([1, 2])
# TILE: Copy the whole group
print(np.tile(colors, 3))
# [1 2 1 2 1 2] β Pattern repeats
# REPEAT: Copy each one
print(np.repeat(colors, 3))
# [1 1 1 2 2 2] β Each item repeats
| Function | What It Does | Example Result |
|---|---|---|
tile |
Repeat whole array | [π΄π΅][π΄π΅][π΄π΅] |
repeat |
Repeat each item | π΄π΄π΄π΅π΅π΅ |
π― Real-Life Examples
Example 1: Building a Game Board
# Start with one cell pattern
cell = np.array([[0, 1],
[1, 0]])
# Tile to make 4x4 checkerboard
board = np.tile(cell, (2, 2))
print(board)
# [[0 1 0 1]
# [1 0 1 0]
# [0 1 0 1]
# [1 0 1 0]]
Example 2: Splitting Test Scores
all_scores = np.array([85, 90, 78, 92, 88, 76])
# Split into 3 groups
groups = np.split(all_scores, 3)
print("Group 1:", groups[0]) # [85, 90]
print("Group 2:", groups[1]) # [78, 92]
print("Group 3:", groups[2]) # [88, 76]
Example 3: Combining Image Channels
red = np.array([[255, 0]])
green = np.array([[0, 255]])
blue = np.array([[0, 0]])
# Stack to make RGB
rgb = np.vstack([red, green, blue])
print(rgb)
# [[255 0]
# [ 0 255]
# [ 0 0]]
π§ Summary Flow
graph LR A["NumPy Arrays"] --> B["Combining"] A --> C["Splitting"] A --> D["Tiling & Repeating"] B --> B1["concatenate<br/>Any axis"] B --> B2["vstack<br/>Vertical"] B --> B3["hstack<br/>Horizontal"] C --> C1["split<br/>Equal parts"] C --> C2["vsplit<br/>Row splits"] C --> C3["hsplit<br/>Column splits"] D --> D1["tile<br/>Copy pattern"] D --> D2["repeat<br/>Copy elements"]
π‘ Key Takeaways
- Combine arrays with
concatenate,vstack,hstack - Split arrays with
split,vsplit,hsplit - Tile copies the whole pattern
- Repeat copies each element individually
- Think LEGOs: snap together, break apart, copy patterns!
Youβve got this! π
