How to slice 2d array into smaller 2d arrays with Python?

Sometimes, we want to slice 2d array into smaller 2d arrays with Python.

In this article, we’ll look at how to slice 2d array into smaller 2d arrays with Python.

How to slice 2d array into smaller 2d arrays with Python?

To slice 2d array into smaller 2d arrays with Python, we can use the NumPy split method.

For instance, we write

a = np.arange(30).reshape([5,6])
a1 = np.split(a,3,axis=1) 

to create an array with arange.

Then we call reshape to reshape the returned array to a 5×6 2d array.

Next, we call split with array a, 3 and the axis` argument set to 1 to get a list of 3 5×2 arrays.

Conclusion

To slice 2d array into smaller 2d arrays with Python, we can use the NumPy reshape method.