Rangooski Rangooski - 1 year ago 238
Python Question

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 [17]: x=np.arange(129)

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 matrix, 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 ?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download