Getting index of first occurrence in each row
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Getting Index Of First Occurrence In Each Row
00:34 Accepted Answer Score 1
00:56 Answer 2 Score 3
01:24 Answer 3 Score 0
01:38 Thank you
--
Full question
https://stackoverflow.com/questions/2386...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #numpy #boolean
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Getting Index Of First Occurrence In Each Row
00:34 Accepted Answer Score 1
00:56 Answer 2 Score 3
01:24 Answer 3 Score 0
01:38 Thank you
--
Full question
https://stackoverflow.com/questions/2386...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #numpy #boolean
#avk47
ANSWER 1
Score 3
Cannot test right now, but I think this should work
arr.argmax(axis=1).T
argmax on bools shortcircuits in numpy 1.9, so it should be preferred to where or nonzero for this use case.
EDIT OK, so the above solution doesn't work, but the approach with argmax is still useful:
In [23]: mult = np.product(arr.shape[:-1])
In [24]: np.column_stack(np.unravel_index(arr.shape[-1]*np.arange(mult) +
....: arr.argmax(axis=-1).ravel(),
....: arr.shape))
Out[24]:
array([[0, 0, 0],
[0, 1, 0],
[1, 0, 2],
[1, 1, 0],
[2, 0, 0],
[2, 1, 0]])
ACCEPTED ANSWER
Score 1
It seems you want np.where() combined with the solution of this answer to find unique rows:
b = np.array(np.where(a)).T
#array([[0, 0, 0],
# [0, 0, 1],
# [0, 1, 0],
# [1, 0, 2],
# [1, 0, 4],
# [1, 1, 0],
# [2, 0, 0],
# [2, 1, 0]], dtype=int64)
c = b[:,:2]
d = np.ascontiguousarray(c).view(np.dtype((np.void, c.dtype.itemsize * c.shape[1])))
_, idx = np.unique(d, return_index=True)
b[idx]
#array([[0, 0, 0],
# [0, 1, 0],
# [1, 0, 2],
# [1, 1, 0],
# [2, 0, 0],
# [2, 1, 0]], dtype=int64)
ANSWER 3
Score 0
Adding onto other answers, if you want it to give you the index of the first col that's True, and return n where n = # cols in a if a row doesn't contain True:
first_occs = np.argmax(a, axis=1)
all_zeros = ~a.any(axis=1).astype(int)
first_occs_modified = first_occs + all_zeros * a.shape[1]