How to split a 1D array into a 2D array in NumPy, dividing the array by the last element?

I have a numpy array let's say

 ([1,2,3,4,5,6,7])

I want to split it into a 2d array so that the last element is in its own array, e.g.

 ([1,2,3,4,5,6],[7])

How exactly did I do this?

+4
source share
1 answer

Use np.split-

np.split(a,[-1])

Run Example -

In [105]: a
Out[105]: array([1, 2, 3, 4, 5, 6, 7])

In [106]: np.split(a,[-1])
Out[106]: [array([1, 2, 3, 4, 5, 6]), array([7])]
+4
source

Source: https://habr.com/ru/post/1669071/


All Articles