w3resource logo

:

Python Exercises

Python NumPy: Split an of array of shape 4x4 it into two arrays along the second axis

Python NumPy: Array Object Exercise-62 with Solution

Write a Python program to split an of array of shape 4x4 it into two arrays along the second axis.
Sample array :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]

Sample Solution:-

Python Code:

import numpy as np
x = np.arange(16).reshape((4, 4))
print("Original array:",x)
print("After splitting horizontally:")
print(np.hsplit(x, [2, 6]))

Sample Output:

Original array: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14]            
After splitting:                                                       
[array([1, 2]), array([3, 4, 5, 6]), array([ 7,  8,  9, 10, 11, 12, 13,
 14])] 

Python Code Editor:

Improve this sample solution and post your code through Disqus