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
Have done some work to come up with a Cython implementation of _assert_all_finite as the current check is rather slow. Have come up with this implementation. This simply uses the C isfinite (or isinf for allow_nan=True) function in a tight loop; returning False whenever isfinite returns False and returning True otherwise. As such it has a very small, constant memory overhead.
For smallish data (0-8000 element float64 arrays), this is comparable to the naive NumPy implementation in time and faster than the current scikit-learn implementation. Should add that the Cython implementation uses considerably less memory than either implementation. For large sized arrays, scikit-learn's performance improves to ~2x faster than the NumPy or Cython implementation. Expect this is mostly due to how the caches are utilized. So this can be improved further.
Would be interested in getting some feedback about whether such a contribution would be interesting to scikit-learn. If so, can investigate further improvements to this implementation.
Because we are have check by default, and it can be relatively slow (#11487 (comment)) in some cases, it would be definitely nice to make it faster.
Ideally, it would be great to have such an improvement in numpy (with possible temporary backport in scikit-learn), there is related discussion in numpy/numpy#6909 Maybe they could be interested by such a contribution?
Are you saying that the proposed implementation is faster than np.isfinite(X.sum()) ? That would take too much memory, would it? If I understand correctly, things become slower when this evaluates to False (e.g. there are non finite elements) and we also have to run np.isinf(X).any() ?
Generally it seems like a good idea to contribute this stuff back to NumPy. Not sure they would take this implementation as it is customized for the case of searching the full array for the first invalid value and returning. Also while they do have a little bit of Cython code, they might prefer C for something like this in a prominent code path.
For small arrays (~1000 elements or less) it appears to be ~3.5x faster than the current scikit-learn implementation. As the array size gets larger, imagine other things like cache pressure, which this implementation does nothing with currently, become an issue and then NumPy-based implementations take over. Maybe using NumPy iterators with buffering would improve the large array case. Generally open to suggestions here.
This behaves more like l.index(...) in the sense that it scans the array for a match to an unacceptable value (inf and possibly nan). If it finds a match, it returns False ending the scan. If it doesn't find a match, it will scan the full array and then return True. So the memory usage is constant and actually quite small (e.g. the index, the current value, length, step size, some pointers, some flags, etc.).
Activity
rth commentedon Jul 25, 2018
Because we are have check by default, and it can be relatively slow (#11487 (comment)) in some cases, it would be definitely nice to make it faster.
Ideally, it would be great to have such an improvement in numpy (with possible temporary backport in scikit-learn), there is related discussion in numpy/numpy#6909 Maybe they could be interested by such a contribution?
Are you saying that the proposed implementation is faster than
np.isfinite(X.sum())? That would take too much memory, would it? If I understand correctly, things become slower when this evaluates to False (e.g. there are non finite elements) and we also have to runnp.isinf(X).any()?jakirkham commentedon Jul 26, 2018
Generally it seems like a good idea to contribute this stuff back to NumPy. Not sure they would take this implementation as it is customized for the case of searching the full array for the first invalid value and returning. Also while they do have a little bit of Cython code, they might prefer C for something like this in a prominent code path.
For small arrays (~1000 elements or less) it appears to be ~3.5x faster than the current scikit-learn implementation. As the array size gets larger, imagine other things like cache pressure, which this implementation does nothing with currently, become an issue and then NumPy-based implementations take over. Maybe using NumPy iterators with buffering would improve the large array case. Generally open to suggestions here.
This behaves more like
l.index(...)in the sense that it scans the array for a match to an unacceptable value (infand possiblynan). If it finds a match, it returnsFalseending the scan. If it doesn't find a match, it will scan the full array and then returnTrue. So the memory usage is constant and actually quite small (e.g. the index, the current value, length, step size, some pointers, some flags, etc.).GaelVaroquaux commentedon Jul 29, 2018
@jakirkham : I would be +1 for inclusion of this in scikit-learn (and of course, encourage contributing upstream)
jnothman commentedon Jul 31, 2018
A pull request here?
Micky774 commentedon Apr 16, 2022
Hey there @jakirkham, just wanted to check if you intended to open a PR for your proposed solution.
jakirkham commentedon May 26, 2022
Sorry for the slow reply @Micky774. Not atm, but am really happy to see you picking this up. Thanks for digging into this! 🙏
lorentzenchr commentedon Jul 7, 2022
Now that #23197 is merged, should we open an issue upstream in numpy?
jakirkham commentedon Jul 7, 2022
It was already raised with NumPy. Please see issue ( numpy/numpy#11622 ), which is cross-linked above
lorentzenchr commentedon Jul 7, 2022
Thanks. I did not see it.