Overview
In Python, an array is a data structure used to store multiple items of the same type. Arrays are useful when dealing with many values of the same data type. An array needs to explicitly import the array module for declaration.
A 2D array is simply an array of arrays. The numpy.array_split() method in Python is used to split a 2D array into multiple sub-arrays of equal size.
Parameters
array_split() takes the following parameters:
- array (required): Represents the input array.
- indices_or_section (required): Represents the number of splits to be returned.
- axis (optional): The axis along which the values are appended.
To split a 2D array, pass in the array and specify the number of splits you want.
Example
Now, let’s split a 2D array into three sections or indices.
Code
import numpy as np
array1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])
# splitting the array into three indexes
new_array1 = np.array_split(array1, 3)
print('The arrays splitted into 3 sections are:', new_array1)
Example
Explanation
- Line 1: We import the numpy module.
- Line 3: We create an array array1.
- Line 5: We use the numpy.array_split() method to split the given array into three sections, and then assign the result to a variable named new_array1.
- Line 6: We print the split 2D array, new_array1.
RELATED TAGS
python
numpy
communitycreator
CONTRIBUTOR
Splitting NumPy Arrays
Splitting is reverse operation of Joining.
Joining merges multiple arrays into one and Splitting breaks one array into multiple.
We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.
Example
Split the array in 3 parts:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)
Try it Yourself »
Note: The return value is an array containing three arrays.
If the array has less elements than required, it will adjust from the end accordingly.
Example
Split the array in 4 parts:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 4)
print(newarr)
Try it Yourself »
Note: We also have the method split() available but it will not adjust the elements when elements are less in source array for splitting like in example above, array_split() worked properly but split() would fail.
Split Into Arrays
The return value of the array_split() method is an array containing each of the split as an array.
If you split an array into 3 arrays, you can access them from the result just like any array element:
Example
Access the splitted arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
Try it Yourself »
Splitting 2-D Arrays
Use the same syntax when splitting 2-D arrays.
Use the array_split() method, pass in the array you want to split and the number of splits you want to do.
Example
Split the 2-D array into three 2-D arrays.
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.array_split(arr, 3)
print(newarr)
Try it Yourself »
The example above returns three 2-D arrays.
Let's look at another example, this time each element in the 2-D arrays contains 3 elements.
Example
Split the 2-D array into three 2-D arrays.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3)
print(newarr)
Try it Yourself »
The example above returns three 2-D arrays.
In addition, you can specify which axis you want to do the split around.
The example below also returns three 2-D arrays, but they are split along the row (axis=1).
Example
Split the 2-D array into three 2-D arrays along rows.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)
Try it Yourself »
An alternate solution is using hsplit() opposite of hstack()
Example
Use the hsplit() method to split the 2-D array into three 2-D arrays along rows.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.hsplit(arr, 3)
print(newarr)
Try it Yourself »
Note: Similar alternates to vstack() and dstack() are available as vsplit() and dsplit().