I have an array of the form :
[[ 1. , 2., 3., 1., 3., 3., 4. ],
[ 1.3, 2.3, 3.3, 3., 3.3, 3.3, 4.3 ],
[ 1.2, 2.
Look at the docs for splitting an array into multiple sub-arrays.
numpy.hsplit(ary, indices_or_sections)
Split an array into multiple sub-arrays horizontally (column-wise).
So say you have a 4x4 array A:
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 12., 13., 14., 15.]])
split = numpy.hsplit(A,4) =
[array([[ 0.],
[ 4.],
[ 8.],
[ 12.]]), array([[ 1.],
[ 5.],
[ 9.],
[ 13.]]), array([[ 2.],
[ 6.],
[ 10.],
[ 14.]]), array([[ 3.],
[ 7.],
[ 11.],
[ 15.]])]
You can do this in O(NlogN)
time using numpy.argsort
, numpy.array_split
, numpy.diff
and numpy.where
:
>>> indices = np.argsort(arr[:, 3])
>>> arr_temp = arr[indices]
>>> np.array_split(arr_temp, np.where(np.diff(arr_temp[:,3])!=0)[0]+1)
[array([[ 1. , 2. , 3. , 1. , 3. , 3. , 4. ],
[ 1.89, 2.3 , 1. , 1. , 3. , 3. , 4. ],
[ 1.1 , 2.1 , 1. , 1. , 3. , 3. , 4. ],
[ 1.9 , 2.2 , 1. , 1. , 3. , 3. , 4. ],
[ 1.3 , 2.2 , 1. , 1. , 3. , 3. , 4. ],
[ 1.5 , 2.1 , 1. , 1. , 3. , 3. , 4. ],
[ 1.4 , 2.3 , 1. , 1. , 3. , 3. , 4. ]]), array([[ 1.2 , 2.8 , 3.2 , 2. , 3.66 , 3.2 , 4.2 ],
[ 1.2 , 2.7 , 3.2 , 2. , 3.2 , 3.231, 4.2 ],
[ 1.2 , 2.9 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ],
[ 1.2 , 2.9 , 3.2 , 2. , 3.34 , 3.2 , 4.2 ],
[ 1.2 , 2.8 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ],
[ 1.2 , 2.7 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ],
[ 1.2 , 2.2 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ]]), array([[ 1.3 , 2.3 , 3.6 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.89, 2.3 , 3.5 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.3 , 3.5 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.22, 3.6 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.3 , 3.3 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.99, 3.7 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.3 , 3.7 , 3. , 3.3 , 3.3 , 4.3 ]])]
To make a list of arrays:
y = [x[x[:,3]==k] for k in np.unique(x[:,3])]