Open In App

Splitting Arrays in NumPy

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

NumPy arrays are an essential tool for scientific computing. However, at times, it becomes necessary to manipulate and analyze specific parts of the data. This is where array splitting comes into play, allowing you to break down an array into smaller sub-arrays, making the data more manageable. It is similar to slicing but on a larger scale. NumPy provides various methods that are specifically designed for different use cases and scenarios.

NumPy Splitting Array

Array splitting in NumPy is like a slice of cake. Think of each element in a NumPy array as a slice of cake. Splitting divides this “cake” into smaller “slices” (sub-arrays), often along specific dimensions or based on certain criteria. We can split horizontally, vertically, or even diagonally depending on our needs.

The split(), hsplit(), vsplit(), and dsplit() functions are important tools for dividing arrays along various axes and dimensions. These functions are particularly useful when working with one-dimensional arrays, matrices, or high-dimensional datasets. NumPy’s array-splitting capabilities are crucial for enhancing the efficiency and flexibility of data processing workflows.

Key concepts and terminology

Here are some important terms to understand when splitting arrays:

  • Axis: The dimension along which the array is split (e.g., rows, columns, depth).
  • Sub-arrays: The smaller arrays resulting from the split.
  • Splitting methods: Different functions in NumPy for splitting arrays (e.g., np.split(), np.vsplit(), np.hsplit(), etc.).
  • Equal vs. Unequal splits: Whether the sub-arrays have the same size or not.

Python3




import numpy as np
Arr = np.array([1, 2, 3, 4, 5, 6])
array = np.array_split(arr, 3)
print(array)


Output:

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

Splitting NumPy Arrays in Python

There are many methods to Split Numpy Array in Python using different functions some of them are mentioned below:

  • Split numpy array using numpy.split()
  • Split numpy array using numpy.array_split()
  • Splitting NumPy 2D Arrays
  • Split numpy array using numpy.vsplit()
  • Split numpy array using numpyhsplit()
  • Split numpy arrayusing numpy.dsplit()

1. Splitting Arrays Into Equal Parts using numpy.split()

numpy.split() is a function that divides an array into equal parts along a specified axis. The code imports NumPy creates an array of numbers (0-5), and then splits it in half (horizontally) using np.split(). The output shows the original array and the two resulting sub-arrays, each containing 3 elements.

Python3




import numpy as np
 
# Creating an example array
array = np.arange(6)
 
# Splitting the array into 2 equal parts along the first axis (axis=0)
result = np.split(array, 2)
 
print("Array:")
print(array)
print("\nResult after numpy.split():")
print(result)


Output:

Array:
[0 1 2 3 4 5]
Result after numpy.split():
[array([0, 1, 2]), array([3, 4, 5])]

2. Unequal Splitting of Arrays using numpy.array_split()

numpy.array_split() splitting into equal or nearly equal sub-arrays or is similar to numpy.split(), but it allows for uneven splitting of arrays. This is useful when the array cannot be evenly divided by the specified number of splits. numpy.array_split(array, 4) splits the array into four parts, accommodating the uneven division.

Python3




import numpy as np
 
# Creating an example array
array = np.arange(13)
 
# Splitting the array into 4 unequal parts along the first axis (axis=0)
result = np.array_split(array, 4)
 
print("Array:")
print(array)
print("\nResult after numpy.array_split():")
print(result)


Output:

Array:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12]
Result after numpy.array_split():
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]

3. Splitting NumPy 2D Arrays

This example showcases the application of numpy.split() in dividing a 2D array into equal parts along a specified axis. Similar concepts can be applied to numpy.array_split for uneven splitting. numpy.split ( array, 3, axis=1 ) splits the array into three equal parts along the second axis.

Python3




import numpy as np
 
# Creating a 2D array
array = np.array([[3, 2, 1], [8, 9, 7], [4, 6, 5]])
 
# Splitting the array into 3 equal parts along the second axis (axis=1)
result = np.split(array, 3, axis=1)
 
print("2D Array:")
print(original_array)
print("\nResult after numpy.split() along axis=1:")
print(result)


Output:

2D Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Result after numpy.split() along axis=1:
[array([[3],
[8],
[4]]), array([[2],
[9],
[6]]), array([[1],
[7],
[5]])]

4. Vertical Splitting of Arrays using numpy.vsplit()

Vertical splitting (row-wise) with numpy.vsplit() divides an array along the vertical axis (axis=0), creating subarrays. This is particularly useful for matrices and multi-dimensional arrays. numpy.vsplit( matrix, 2) splits the matrix into two equal parts along the vertical axis (axis=0).

Python3




import numpy as np
 
# Creating an example matrix
matrix = np.array([[1, 2, 3],
                            [4, 5, 6],
                            [7, 8, 9],
                            [10, 11, 12]])
 
# Vertical splitting into 2 subarrays along axis=0
result = np.vsplit(matrix, 2)
 
print("Matrix:")
print(matrix)
print("\nResult after numpy.vsplit():")
print(result)


Output:

Matrix:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Result after numpy.vsplit():
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]])]

5. Horizontal Splitting of Arrays using numpy.hsplit()

Horizontal splitting (column-wise) with numpy.hsplit() divides an array along the horizontal axis (axis=1), creating subarrays. This operation is valuable in data processing tasks. numpy.hsplit ( array, 2) splits the array into two equal parts along the horizontal axis (axis=1).

Python3




import numpy as np
 
# Creating an example 2D array
array = np.array([[1, 2, 3, 4],
                           [5, 6, 7, 8],
                           [9, 10, 11, 12]])
 
# Horizontal splitting into 2 subarrays along axis=1
result = np.hsplit(array, 2)
 
print("2D Array:")
print(array)
print("\nResult after numpy.hsplit():")
print(result)


Output:

2D Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Result after numpy.hsplit():
[array([[ 1, 2],
[ 5, 6],
[ 9, 10]]), array([[ 3, 4],
[ 7, 8],
[11, 12]])]

6. Splitting Arrays Along the Third Axis using numpy.dsplit()

numpy.dsplit() is used for splitting arrays along the third axis (axis=2), applicable to 3D arrays and beyond. numpy.dsplit (original_3d_array, 2) splits the array into two equal parts along the third axis (axis=2).

Python3




import numpy as np
 
# Creating an example 3D array
original_3d_array = np.arange(24).reshape((2, 3, 4))
 
# Splitting along axis=2 (third axis)
result = np.dsplit(original_3d_array, 2)
 
print("Original 3D Array:")
print(original_3d_array)
print("\nResult after numpy.dsplit():")
print(result)


Output:

Original 3D Array:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Result after numpy.dsplit():
[array([[[ 0, 1],
[ 4, 5],
[ 8, 9]],
[[12, 13],
[16, 17],
[20, 21]]]), array([[[ 2, 3],
[ 6, 7],
[10, 11]],
[[14, 15],
[18, 19],
[22, 23]]])]

Conclusion

Splitting arrays in NumPy in Python allows for manipulating large datasets or feeding data into machine learning models. With the right tools, you can extract valuable insights from your data. Remember to strategically divide your array to meet your specific needs. Start exploring and splitting your arrays today for more efficient and insightful data analysis!



Last Updated : 22 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads