Home » Splitting Arrays in NumPy tutorial

Splitting Arrays in NumPy tutorial

Spring Framework Basics Video Course
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
1 Year Subscription

In NumPy, array splitting refers to dividing a single array into multiple sub-arrays.

This is useful in various applications where data needs to be partitioned for further processing, such as machine learning or data analysis.

NumPy provides multiple ways to split arrays, allowing you to choose the most appropriate method based on the array's dimensions and the desired split pattern.

In this tutorial, we will cover:

  1. Basic Splitting of 1D Arrays
  2. Splitting 2D Arrays Horizontally and Vertically
  3. Using array_split for Uneven Splits
  4. Splitting 3D Arrays

Let's explore each type of splitting with examples.

1. Basic Splitting of 1D Arrays

To split a 1D array, you can use the np.split() function, which divides an array into equal parts.

You specify the array and the number of parts you want.

Example 1: Splitting a 1D Array into Equal Parts

import numpy as np

# Create a 1D array
array_1d = np.array([10, 20, 30, 40, 50, 60])

# Split into 3 equal parts
split_array = np.split(array_1d, 3)
print("Split 1D array into 3 parts:\n", split_array)

Output:

Split 1D array into 3 parts:
 [array([10, 20]), array([30, 40]), array([50, 60])]
  • Explanation: Each sub-array has 2 elements since the original array has 6 elements split into 3 equal parts.

Example 2: Splitting a 1D Array with array_split

If the array cannot be evenly divided, np.split() will raise an error. Instead, you can use np.array_split(), which allows for uneven splits.

# Split into 4 parts (uneven)
split_array_uneven = np.array_split(array_1d, 4)
print("\nSplit 1D array into 4 uneven parts:\n", split_array_uneven)

Output:

Split 1D array into 4 uneven parts:
 [array([10, 20]), array([30, 40]), array([50]), array([60])]
  • Explanation: array_split() distributes elements as evenly as possible, allowing uneven splitting.

2. Splitting 2D Arrays Horizontally and Vertically

For 2D arrays, you can split them either along rows (vertically) or columns (horizontally). NumPy provides vsplit for vertical splitting and hsplit for horizontal splitting.

Example 3: Splitting a 2D Array Horizontally with hsplit

hsplit splits an array along the columns.

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

# Split into 2 equal parts horizontally (along columns)
horizontal_split = np.hsplit(array_2d, 2)
print("Horizontal split into 2 parts:\n", horizontal_split)

Output:

Horizontal split into 2 parts:
 [array([[1, 2],
        [5, 6]]), array([[3, 4],
        [7, 8]])]
  • Explanation: hsplit splits the array into 2 equal parts along the columns.

Example 4: Splitting a 2D Array Vertically with vsplit

vsplit splits an array along the rows.

# Split into 2 equal parts vertically (along rows)
vertical_split = np.vsplit(array_2d, 2)
print("\nVertical split into 2 parts:\n", vertical_split)

Output:

Vertical split into 2 parts:
 [array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]])]
  • Explanation: vsplit splits the array into 2 equal parts along the rows.

Example 5: Using split for Horizontal and Vertical Splitting

You can also use np.split() by specifying the axis parameter (axis=1 for columns and axis=0 for rows).

# Horizontal split using split with axis=1 (columns)
horizontal_split_axis = np.split(array_2d, 2, axis=1)
print("\nHorizontal split using split with axis=1:\n", horizontal_split_axis)

# Vertical split using split with axis=0 (rows)
vertical_split_axis = np.split(array_2d, 2, axis=0)
print("\nVertical split using split with axis=0:\n", vertical_split_axis)

3. Using array_split for Uneven Splits

If you want to split an array into uneven parts, you can use np.array_split. This is especially useful when the array dimensions cannot be divided evenly.

Example 6: Uneven Horizontal Split with array_split

# Split a 2D array into 3 uneven horizontal parts
horizontal_uneven_split = np.array_split(array_2d, 3, axis=1)
print("Uneven horizontal split into 3 parts:\n", horizontal_uneven_split)

Output:

Uneven horizontal split into 3 parts:
 [array([[1, 2],
        [5, 6]]), array([[3],
        [7]]), array([[4],
        [8]])]

Example 7: Uneven Vertical Split with array_split

# Split a 2D array into 3 uneven vertical parts
vertical_uneven_split = np.array_split(array_2d, 3, axis=0)
print("\nUneven vertical split into 3 parts:\n", vertical_uneven_split)

Output:

Uneven vertical split into 3 parts:
 [array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]]), array([], shape=(0, 4), dtype=int64)]
  • Explanation: array_split will fill any leftover parts with empty arrays if the split count exceeds the rows or columns available.

4. Splitting 3D Arrays

Splitting works similarly for 3D arrays, where you can split along different axes (depth, rows, or columns).

Example 8: Splitting a 3D Array along Axis 0 (Depth)

# Create a 3D array
array_3d = np.array([
    [[1, 2, 3], [4, 5, 6]],
    [[7, 8, 9], [10, 11, 12]],
    [[13, 14, 15], [16, 17, 18]]
])

# Split along the depth (axis=0) into 3 parts
split_3d_depth = np.array_split(array_3d, 3, axis=0)
print("3D array split along depth (axis=0):\n", split_3d_depth)

Output:

3D array split along depth (axis=0):
 [array([[[ 1,  2,  3],
        [ 4,  5,  6]]]), array([[[ 7,  8,  9],
        [10, 11, 12]]]), array([[[13, 14, 15],
        [16, 17, 18]]])]
  • Explanation: Here, array_3d is split into 3 sub-arrays along the depth (first dimension).

Example 9: Splitting a 3D Array along Axis 1 (Rows)

# Split along rows (axis=1)
split_3d_rows = np.array_split(array_3d, 2, axis=1)
print("\n3D array split along rows (axis=1):\n", split_3d_rows)

Output:

3D array split along rows (axis=1):
 [array([[[ 1,  2,  3]],
        [[ 7,  8,  9]],
        [[13, 14, 15]]]), array([[[ 4,  5,  6]],
        [[10, 11, 12]],
        [[16, 17, 18]]])]
  • Explanation: Splitting along rows (axis=1) divides each 2D slice of the 3D array.

Example 10: Splitting a 3D Array along Axis 2 (Columns)

# Split along columns (axis=2)
split_3d_columns = np.array_split(array_3d, 3, axis=2)
print("\n3D array split along columns (axis=2):\n", split_3d_columns)

Output:

3D array split along columns (axis=2):
 [array([[[ 1],
        [ 4]],

       [[ 7],
        [10]],

       [[13],
        [16]]]), array([[[ 2],
        [

 5]],

       [[ 8],
        [11]],

       [[14],
        [17]]]), array([[[ 3],
        [ 6]],

       [[ 9],
        [12]],

       [[15],
        [18]]])]
  • Explanation: Here, splitting along columns (axis=2) divides each element of the innermost dimension into separate arrays.

Summary of Array Splitting in NumPy

Function Description
split Splits an array into equal parts; raises an error if an uneven split is attempted.
array_split Allows uneven splitting if the array cannot be split equally.
hsplit Splits an array horizontally along columns (axis=1).
vsplit Splits an array vertically along rows (axis=0).
3D Splitting array_split with axes 0, 1, or 2 splits along depth, rows, or columns for 3D arrays.

Conclusion

In this tutorial, we explored various ways to split arrays in NumPy, including:

  • Splitting 1D arrays using split and array_split.
  • Splitting 2D arrays horizontally and vertically with hsplit and vsplit.
  • Using array_split for uneven splits.
  • Splitting 3D arrays along different axes.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More