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
array_split() method in Python is used to split a 2D array into multiple sub-arrays of equal size.
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() .
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).
array_split() method in Python is used to split an array into multiple sub-arrays of equal size.
>>> 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]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With