Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy splitting multidimensional arrays

I have a multidimensional numpy array that I want to split based on a particular column.

Ex. [[1,0,2,3],[1,2,3,4],[2,3,4,5]] Say I want to split this array by the 2nd column with the expression x <=2. Then I would get two arrays [[1,0,2,3],[1,2,3,4]] and [[2,3,4,5]].

I am currently using this statement, which I don't think is correct.

splits = np.split(S, np.where(S[:, a] <= t)[0][:1]) #splits S based on t

#a is the column number
like image 816
mrQWERTY Avatar asked Feb 19 '16 04:02

mrQWERTY


People also ask

How do you split a multi dimensional array in Python?

array_split() method in Python is used to split a 2D array into multiple sub-arrays of equal size.

How do I split a 2d NumPy array?

Use the hsplit() method to split the 2-D array into three 2-D arrays along rows. Note: Similar alternates to vstack() and dstack() are available as vsplit() and dsplit() .

How do I divide an array in NumPy?

divide() in Python. numpy. divide(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise).

How do you split an array into two parts in Python?

array_split() method in Python is used to split an array into multiple sub-arrays of equal size.


1 Answers

>>> import numpy as np
>>> a = np.asarray([[1,0,2,3],[1,2,3,4],[2,3,4,5]])
>>> a
array([[1, 0, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5]])
>>> split1 = a[a[:,1] <= 2, :]
array([[1, 0, 2, 3],
       [1, 2, 3, 4]])
>>> split2 = a[a[:,1] > 2, :]
array([[2, 3, 4, 5]])
like image 62
orange Avatar answered Oct 06 '22 00:10

orange