NumPy Splitting Array

Introduction

Splitting arrays in NumPy allows you to divide an array into multiple sub-arrays. This can be useful for various data manipulation tasks. In this chapter, you will learn how to split arrays using different NumPy functions such as split, array_split, hsplit, and vsplit.

Importing NumPy

First, import NumPy in your script or notebook:

import numpy as np

Using split

The split function divides an array into multiple sub-arrays of equal size.

Example: Splitting a 1D Array

# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5, 6])

# Splitting the array into 3 equal parts
result = np.split(arr, 3)
print(result)

Output:

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

Example: Splitting a 2D Array

# Creating a 2D array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

# Splitting the array into 2 equal parts along the second axis (columns)
result = np.split(arr, 2, axis=1)
print(result)

Output:

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

Using array_split

The array_split function is similar to split, but it allows you to specify uneven splits if the array cannot be divided equally.

Example: Splitting a 1D Array into Unequal Parts

# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5, 6, 7])

# Splitting the array into 3 parts
result = np.array_split(arr, 3)
print(result)

Output:

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

Example: Splitting a 2D Array into Unequal Parts

# Creating a 2D array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

# Splitting the array into 3 parts along the second axis (columns)
result = np.array_split(arr, 3, axis=1)
print(result)

Output:

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

Using hsplit

The hsplit function splits an array horizontally along the second axis (columns).

Example: Horizontal Split of a 2D Array

# Creating a 2D array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

# Splitting the array into 2 parts
result = np.hsplit(arr, 2)
print(result)

Output:

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

Using vsplit

The vsplit function splits an array vertically along the first axis (rows).

Example: Vertical Split of a 2D Array

# Creating a 2D array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# Splitting the array into 3 parts
result = np.vsplit(arr, 3)
print(result)

Output:

[array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]]), array([[ 9, 10, 11, 12]])]

Using dsplit

The dsplit function splits an array along the third axis (depth). This is useful for 3D arrays.

Example: Depth-Wise Split of a 3D Array

# Creating a 3D array
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

# Splitting the array into 3 parts along the depth axis
result = np.dsplit(arr, 3)
print(result)

Output:

[array([[[ 1],
         [ 4]],

        [[ 7],
         [10]]]), array([[[ 2],
                          [ 5]],

                         [[ 8],
                          [11]]]), array([[[ 3],
                                           [ 6]],

                                          [[ 9],
                                           [12]]])]

Conclusion

Splitting arrays in NumPy is straightforward with functions like split, array_split, hsplit, vsplit, and dsplit. These functions allow you to divide arrays into sub-arrays along different axes, making it easy to manipulate and analyze your data efficiently.

Leave a Comment

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

Scroll to Top