Instagram
youtube
Facebook
Twitter

Split a NumPy Array Horizontally

A Split a NumPy Array Horizontally?

Code Explanation:
Array Initialization: A 2D NumPy array arr with shape (2, 4) is defined.
Horizontal Split: np.hsplit(arr, 2) splits the array into 2 equal parts column-wise.
Equal Columns Requirement: The total number of columns (4) must be divisible by 2.
Result: Two arrays of shape (2, 2) are created from the original array.
Output: Prints a list containing the split arrays.

 

Program:

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

result = np.hsplit(arr, 2)

print(result)