w3resource

NumPy: Split a given array into multiple sub-arrays vertically

NumPy: Array Object Exercise-131 with Solution

Write a NumPy program to split a given array into multiple sub-arrays vertically (row-wise).

Sample Solution:

Python Code:

import numpy as np    
print("\nOriginal arrays:")
x = np.arange(16.0).reshape(4, 4)
print(x)
new_array1 =  np.vsplit(x, 2)
print("\nSplit an array into multiple sub-arrays vertically:")
print(new_array1)

Sample Output:

Original arrays:
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]]

Split an array into multiple sub-arrays vertically:
[array([[0., 1., 2., 3.],
       [4., 5., 6., 7.]]), array([[ 8.,  9., 10., 11.],
       [12., 13., 14., 15.]])]

Pictorial Presentation:

Python NumPy: Split a given array into multiple sub-arrays vertically.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to stack 1-D arrays as row wise.
Next: Write a NumPy program to split array into multiple sub-arrays along the 3rd axis.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]