Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Numpy Matrix split

I have a matrix of shape 4 x 129. I am trying to do a horizontal split like the example shown below:

In [18]: x = np.arange(4*129)

In [19]: x = x.reshape(4, 129)

In [20]: x.shape
Out[20]: (4, 129)

In [21]: y = np.hsplit(x, 13)

ValueError: array split does not result in an equal division

I understand that it can't split it equally into 13. I don't want to do zero pad one more column and divide by 13.

I want to split the x matrix into 13 small matrices, where the each 12 split should be in size of 4 x 10 and the last one should be in size of 4 x 9.

Is there any way to do like this ?

like image 633
Rangooski Avatar asked Mar 11 '23 17:03

Rangooski


1 Answers

You can pass the indices for split and in this case you can create them simply using np.arange():

>>> a = np.hsplit(x, np.arange(12, 129, 12))
>>> 
>>> a[0].shape
(4, 12)
>>> a[-1].shape
(4, 9)
like image 126
Mazdak Avatar answered Mar 20 '23 06:03

Mazdak