Python: matrix matrix splitting

I have a shape matrix 4 x 129. I am trying to make a horizontal split as 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 he cannot split it by 13. I don’t want to make another padding in the null column and divide by 13.

I want to break the matrix xinto 13 small matrices, where each 12 split should have size 4 x 10, and the last should have size 4 x 9.

Is there any way to do this?

+4
source share
2 answers

You can pass indexes for split, in which case you can create them simply with np.arange():

>>> a = np.hsplit(x, np.arange(12, 129, 12))
>>> 
>>> a[0].shape
(4, 12)
>>> a[-1].shape
(4, 9)
+5
source

, , np.hsplit int , , , .

:

temp_array = [(i+1)*10 for i in range((129-1)//10)]
y = np.hsplit(x,temp_array)

:

y = np.hsplit(x,[(i+1)*10 for i in range((129-1)//10)])

: -1 , x, ;

Edit2: temp_array -

temp_array = np.arange(10,129,10)

:

y = np.hsplit(x,np.arange(10,129,10))

Edit3: python3 ( python2): int- float. // / , , int

: ('splitting_array') , 0 .
, 'splitting_array'. numpy.split, _ , "splitting_array".
, _ , 10.

+2

All Articles