Combining and Splitting Arrays

Back

Loading concept...

🧩 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

  1. Combine arrays with concatenate, vstack, hstack
  2. Split arrays with split, vsplit, hsplit
  3. Tile copies the whole pattern
  4. Repeat copies each element individually
  5. Think LEGOs: snap together, break apart, copy patterns!

You’ve got this! πŸš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.