You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We could use boolean indexing instead of multiplication, and may even make it faster if done properly:
In [18]: arr = np.random.rand(1000, 1000)
In [19]: %timeit np.tri(1000) * arr # this is was np.triu currently does
100 loops, best of 3: 8.74 ms per loop
In [28]: %%timeit # this would be the boolean approach
....: mask = ~np.tri(1000, dtype=bool)
....: out = arr.copy()
....: out[mask] = 0
....:
100 loops, best of 3: 7 ms per loop
I have only done limited testing, but haven't found any faster way of doing this while keeping a pure Python implementation. Ideas are welcome! There may be an extra ms to shave if we change np.tri to take an inverse kwarg so that it produces the negated mask directly.
Anyway, if there is no major objection to this approach, I'll have a PR ready later today.
In numpy 1.8.1, I've just found some strange behaviour.
Summary: when calling np.triu on a matrix, inf entries in the discarded region get converted to nan. This broke my code unexpectedly.
Also posted on stackexchange: https://stackoverflow.com/questions/24678932/numpy-triu-generates-nan-when-called-on-matrices-with-infinite-values
The text was updated successfully, but these errors were encountered: