NumPy Array Split
Learn all about NumPy Array Split in this comprehensive tutorial.
- •Splitting is reverse operation of Joining.
- •The return value of the array_split() method is a list containing each of the split as an array.
- •Use the same syntax when splitting 2-D arrays.
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.
If the array has less elements than required, it will adjust from the end accordingly.
Split Into Arrays
The return value of the array_split() method is a list 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:
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.
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.
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 column (axis=1).
An alternate solution is using hsplit() opposite of hstack()
Module quiz
2 questionsWhich of the following is true about NumPy Array Split?
What is the most common pitfall when working with NumPy Array Split?
Answer all questions to submit.