Getting index of first occurrence in each row
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC D Schuberts Piano Sonata D 850 in D
--
Chapters
00:00 Question
00:42 Accepted answer (Score 1)
01:17 Answer 2 (Score 3)
01:58 Thank you
--
Full question
https://stackoverflow.com/questions/2386...
Accepted answer links:
[the solution of this answer]: https://stackoverflow.com/a/16973510/832...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #numpy #boolean
#avk47
--
Track title: CC D Schuberts Piano Sonata D 850 in D
--
Chapters
00:00 Question
00:42 Accepted answer (Score 1)
01:17 Answer 2 (Score 3)
01:58 Thank you
--
Full question
https://stackoverflow.com/questions/2386...
Accepted answer links:
[the solution of this answer]: https://stackoverflow.com/a/16973510/832...
--
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]