In this python numpy program, we will split an array of 10 elements into 3 arrays using NumPy.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements using np.split().
- Print the output.
import numpy as np
x = np.arange(1, 11)
print("Original array:",x)
print("After splitting:")
print(np.split(x, [2, 5]))
Output :
Original array: [ 1 2 3 4 5 6 7 8 9 10]
After splitting:
[array([1, 2]), array([3, 4, 5]), array([ 6, 7, 8, 9, 10])]