4️⃣🔗 NumPy Array Operations
Estimated reading: 3 minutes 29 views

🧱 NumPy Array Split – Dividing Arrays by Columns or Rows

🧲 Introduction – Why Learn Array Splitting in NumPy?

Splitting arrays in NumPy is essential for segmenting data, processing batches, or slicing datasets for training and testing purposes. Whether you’re working with tabular data, images, or time series, mastering split(), hsplit(), and vsplit() ensures you can divide arrays logically and efficiently.

🎯 In this guide, you’ll learn:

  • How to split 1D, 2D, and 3D arrays
  • The differences between split(), hsplit(), vsplit(), and array_split()
  • How to control number of splits and split indices
  • Real-world examples and edge case handling

✂️ Using np.split() – Basic Slicing by Axis

import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60])
parts = np.split(arr, 3)
print(parts)

👉 Output:

[array([10, 20]), array([30, 40]), array([50, 60])]

📌 Syntax: np.split(array, num_splits, axis=0)
⚠️ All splits must be equal in size or it raises an error.


🧮 Splitting 2D Arrays (Rows and Columns)

🔹 Split by Rows (axis=0)

matrix = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
rows = np.split(matrix, 2, axis=0)
print(rows)

👉 Output:

[array([[1, 2], [3, 4]]), array([[5, 6], [7, 8]])]

🔹 Split by Columns (axis=1)

cols = np.split(matrix, 2, axis=1)
print(cols)

👉 Output:

[array([[1], [3], [5], [7]]), array([[2], [4], [6], [8]])]

📤 Horizontal Split – np.hsplit()

arr = np.array([[1, 2, 3, 4]])
hs = np.hsplit(arr, 2)
print(hs)

👉 Output:

[array([[1, 2]]), array([[3, 4]])]

📌 Shortcut for split(array, sections, axis=1)


📥 Vertical Split – np.vsplit()

arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
vs = np.vsplit(arr, 2)
print(vs)

👉 Output:

[array([[1, 2], [3, 4]]), array([[5, 6], [7, 8]])]

📌 Shortcut for split(array, sections, axis=0)


🧊 Uneven Splits – Use np.array_split()

arr = np.arange(10)
parts = np.array_split(arr, 3)
print(parts)

👉 Output:

[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]

📌 Use array_split() when data isn’t evenly divisible
✅ Prevents ValueError from split()


📐 Split at Custom Indices

arr = np.arange(10)
parts = np.split(arr, [3, 6, 9])
print(parts)

👉 Output:

[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([9])]

📌 Provide a list of split indices instead of number of splits


🚫 Common Pitfalls

  • ❌ Using split() on uneven sizes:
np.split(np.arange(10), 3)  # ❌ ValueError

✅ Use array_split() instead.

  • ❌ Forgetting axis direction:
np.hsplit(arr, 2)  # Requires splitting along columns (axis=1)

📊 Splitting Functions Overview

FunctionPurposeCan Handle Uneven?Axis
split()Equal splits❌ NoAny
array_split()Unequal-safe splits✅ YesAny
hsplit()Horizontal split (columns)❌ Noaxis=1
vsplit()Vertical split (rows)❌ Noaxis=0

🔍 Summary – Key Takeaways

  • Use split() when sizes divide evenly
  • Use array_split() for flexible splitting
  • Use hsplit() and vsplit() for 2D shortcuts
  • Control split points using index lists
  • All return lists of NumPy arrays

⚙️ Real-World Applications

  • Split datasets for training/testing in ML
  • Separate image RGB channels
  • Partition large arrays into chunks for batch processing
  • Divide time-series into sliding windows

❓ FAQs – NumPy Array Split

❓ How many arrays does split() return?
✅ It returns the number you specify — each with equal size.

❓ What if I want unequal splits?
✅ Use np.array_split() instead.

❓ Can I split a 3D array?
✅ Yes, by setting the appropriate axis:

np.split(arr3d, 2, axis=0)

❓ What type does split() return?
✅ It returns a list of NumPy arrays.

❓ Can I split based on values instead of indices?
❌ No. You’ll need to create masks or conditions manually.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

NumPy Array Split

Or Copy Link

CONTENTS
Scroll to Top