Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a numpy array to 2d array based on postive/neagtive changes

Tags:

python

numpy

I have a numpy 1D array:

import numpy as np

arr = np.array([1,  1,  3, -2, -1,  2,  0,  2,  1,  1, -3, -1,  2])

I want split it into another two-dimensional array, based on changes in positive and negative values of array's elements(0 is placed in the range of positive values). But the original order of elements should be maintained.

The desired result is:

new_arr = [[1, 1, 3], [-2, -1], [2, 0, 2, 1, 1], [-3, -1], [2]]
like image 342
Sun Jar Avatar asked Nov 01 '25 11:11

Sun Jar


1 Answers

You could use array_split, diff, nonzero:

np.array_split(arr, np.nonzero(np.diff(arr>=0))[0]+1)

Ouptut:

[array([1, 1, 3]),
 array([-2, -1]),
 array([2, 0, 2, 1, 1]),
 array([-3, -1]),
 array([2])]

Intermediates:

# arr>0
[ True  True  True False False  True False  True  True  True False False  True]

# np.diff(arr>=0)
[False False  True False  True False False False False  True False  True]

# np.nonzero(np.diff(arr>=0))[0]+1
[ 3  5 10 12]

And for lists as output:

out = list(map(list, np.array_split(arr, np.nonzero(np.diff(arr>=0))[0]+1)))

Output:

[[1, 1, 3], [-2, -1], [2, 0, 2, 1, 1], [-3, -1], [2]]

Or using itertools.groupby:

from itertools import groupby

out = [list(g) for _,g in groupby(arr, key=lambda x: x>=0)]

Output:

[[1, 1], [3, -2], [-1, 2, 0, 2, 1], [1, -3], [-1, 2]]

comparison of approaches

comparison array split numpy itertools groupby

like image 96
mozway Avatar answered Nov 03 '25 02:11

mozway