Open In App

Splitting Arrays in NumPy

Last Updated : 22 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
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!



Previous Article
Next Article

Similar Reads

Benefit of NumPy arrays over Python arrays
The need for NumPy arises when we are working with multi-dimensional arrays. The traditional array module does not support multi-dimensional arrays. Let's first try to create a single-dimensional array (i.e one row & multiple columns) in Python without installing NumPy Package to get a more clear picture. C/C++ Code from array import * arr = ar
1 min read
Python | Splitting string to list of characters
Sometimes we need to work with just the lists and hence strings might need to be converted into lists. It has to be converted in list of characters for certain tasks to be performed. This is generally required in Machine Learning to preprocess data and text classifications. Let's discuss certain ways in which this task is performed. Method #1: Usin
5 min read
Python | Splitting string to list of characters
Sometimes we get a string and we need to split it into the individual processing. This is quite a common utility and has application in many domains, be it Machine Learning or Web Development. Having shorthands to it can be helpful. Let's discuss certain ways in which this can be done. Method #1 : Using list() This is the simplest way to achieve th
2 min read
Python | Splitting list on empty string
Sometimes, we may face an issue in which we require to split a list to list of list on the blank character sent as deliminator. This kind of problem can be used to send messages or can be used in cases where it is desired to have list of list of native list. Let's discuss certain ways in which this can be done. Method #1 : Using index() and list sl
4 min read
Python | Splitting operators in String
Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let's discuss certain ways in which this problem can be performed. Method #1 : Using re.split() This task can be solved using the split functionality provided by Python regex
7 min read
Python | Splitting Text and Number in string
Sometimes, we have a string, which is composed of text and number (or vice-versa), without any specific distinction between the two. There might be a requirement in which we require to separate the text from the number. Let's discuss certain ways in which this can be performed. Method #1 : Using re.compile() + re.match() + re.groups() The combinati
7 min read
Python | Splitting string list by strings
Sometimes, while working with Python strings, we might have a problem in which we need to perform a split on a string. But we can have a more complex problem of having a front and rear string and need to perform a split on them. This can be multiple pairs for split. Let's discuss certain way to solve this particular problem. Method : Using loop + i
3 min read
Splitting stereo audio to mono with PyDub
Splitting a stereo audio file into multiple mono audio files is very useful if you are trying to process or transcribe the stereo audio files. This is because stereo audio has 2 audio sources on different channels which makes it very hard to process the file. Splitting the stereo audio file into mono audio files makes this job easier. For this arti
2 min read
Python3 Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
Given an array A[] of length N, where N is an even number, the task is to answer Q independent queries where each query consists of a positive integer K representing the number of circular shifts performed on the array and find the sum of elements by performing Bitwise OR operation on the divided array.Note: Each query begins with the original arra
5 min read
Splitting and Merging Channels with Python-OpenCV
In this article, we will learn how to split a multi-channel image into separate channels and combine those separate channels into a multi-channel image using OpenCV in Python. To do this, we use cv2.split() and cv2.merge() functions respectively. Image Used: Splitting Channels cv2.split() is used to split coloured/multi-channel image into separate
2 min read
Practice Tags :
three90RightbarBannerImg