force maximum and minimum value in an array (clip and optimization tests)
you have an multi-dimensional array (we test with 3D here, but it could be « nD »), and you don’t want value to be bigger than a maximum, or smaller than a minimum.
I found (and tested) 3 different ways using NumPy:
- np.choose (with a trick in building choice matrix)
- use indexing to change directly the value for the « bad » values
- numpy.clip() (yes, I’ve seen that it existed after finding the 2 previous)
so here is the Python code for testing the speed
guess which one is the fastests ?
np.clip obviously! numpy is not programmed by dummies, but by clever guys 🙂
and the factor is good : np.clip takes 4.1s on my computer, indexing is nearly 4 times slower with 15.6s and np.choose is really slow with more than 28s. The ratio is about the same for 10 times smaller matrix.