How to invert a permutation array in numpy

Short answer def invert_permutation(p): “””Return an array s with which np.array_equal(arr[p][s], arr) is True. The array_like argument p must be some permutation of 0, 1, …, len(p)-1. “”” p = np.asanyarray(p) # in case p is a tuple, etc. s = np.empty_like(p) s[p] = np.arange(p.size) return s Sorting is an overkill here. This is just … Read more

How do I efficiently find which elements of a list are in another list?

I thought it would be useful to actually time some of the solutions presented here on a larger sample input. For this input and on my machine, I find Cardstdani’s approach to be the fastest, followed by the numpy isin() approach. Setup 1 import random list_1 = [random.randint(1, 10_000) for i in range(100_000)] list_2 = … Read more

AVX2 what is the most efficient way to pack left based on a mask?

AVX2 + BMI2. See my other answer for AVX512. (Update: saved a pdep in 64bit builds.) We can use AVX2 vpermps (_mm256_permutevar8x32_ps) (or the integer equivalent, vpermd) to do a lane-crossing variable-shuffle. We can generate masks on the fly, since BMI2 pext (Parallel Bits Extract) provides us with a bitwise version of the operation we … Read more

NumPy version of “Exponential weighted moving average”, equivalent to pandas.ewm().mean()

I think I have finally cracked it! Here’s a vectorized version of numpy_ewma function that’s claimed to be producing the correct results from @RaduS’s post – def numpy_ewma_vectorized(data, window): alpha = 2 /(window + 1.0) alpha_rev = 1-alpha scale = 1/alpha_rev n = data.shape[0] r = np.arange(n) scale_arr = scale**r offset = data[0]*alpha_rev**(r+1) pw0 = … Read more

Using Numpy Vectorize on Functions that Return Vectors

np.vectorize is just a convenience function. It doesn’t actually make code run any faster. If it isn’t convenient to use np.vectorize, simply write your own function that works as you wish. The purpose of np.vectorize is to transform functions which are not numpy-aware (e.g. take floats as input and return floats as output) into functions … Read more