NUMPY Tutorial



NumPy ARRAY SPLIT


🔹 NumPy Array Split

Splitting an array means dividing it into multiple smaller arrays. NumPy provides powerful functions to split arrays easily along specified axes.

✅ Key Functions for Splitting Arrays

  • np.split() — Split an array into multiple sub-arrays along a specified axis.
  • np.array_split() — Similar to split(), but can split into unequal parts if needed.
  • np.hsplit() — Split an array horizontally (column-wise).
  • np.vsplit() — Split an array vertically (row-wise).
  • np.dsplit() — Split along the 3rd axis (depth) for 3D arrays.

🎯 Basic Syntax

sub_arrays = np.split(array, indices_or_sections, axis=axis_value)

indices_or_sections can be:

  • An integer — Number of equal parts to split into (array size along axis must be divisible by this number for split()).
  • A list of indices — Indices where to split the array.

🧰 Important Notes

  • np.split() requires the array to be divisible into equal parts unless you specify indices.
  • np.array_split() can split into unequal parts, so it's safer to use if unsure.

✅ Examples

import numpy as np

# Example 1: Split 1D array into 3 equal parts
arr = np.array([10, 20, 30, 40, 50, 60])
split_arr = np.split(arr, 3)
print("Split 1D array into 3 equal parts:")
for sub in split_arr:
    print(sub)
# Output:
# [10 20]
# [30 40]
# [50 60]

# Example 2: Split 1D array into unequal parts using indices
split_arr2 = np.split(arr, [2, 4])  # Split at indices 2 and 4
print("\nSplit 1D array at indices 2 and 4:")
for sub in split_arr2:
    print(sub)
# Output:
# [10 20]
# [30 40]
# [50 60]

# Example 3: Using array_split to split into unequal parts safely
arr2 = np.array([1, 2, 3, 4, 5])
split_arr3 = np.array_split(arr2, 3)
print("\nArray split into 3 parts (unequal allowed):")
for sub in split_arr3:
    print(sub)
# Output:
# [1 2]
# [3 4]
# [5]

# Example 4: Split 2D array vertically (row-wise)
arr_2d = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]])
vsplit_arr = np.vsplit(arr_2d, 3)
print("\nVertical split of 2D array into 3 parts:")
for sub in vsplit_arr:
    print(sub)
# Output:
# [[1 2 3 4]]
# [[5 6 7 8]]
# [[9 10 11 12]]

# Example 5: Split 2D array horizontally (column-wise)
hsplit_arr = np.hsplit(arr_2d, 2)
print("\nHorizontal split of 2D array into 2 parts:")
for sub in hsplit_arr:
    print(sub)
# Output:
# [[ 1  2]
#  [ 5  6]
#  [ 9 10]]
# [[ 3  4]
#  [ 7  8]
#  [11 12]]

⚠️ Common Errors

  • ValueError: array split does not result in an equal division — happens when np.split() is used to split into unequal parts. Use np.array_split() instead.
  • Make sure split indices are within valid range.

🔎 Summary

  • np.split() splits arrays into equal parts or at specified indices.
  • np.array_split() is more flexible, allowing unequal splits.
  • np.vsplit() and np.hsplit() are convenient for 2D arrays.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review