Use your NumPy on Steroids💉⚡

NumPy enables fast operations on its arrays using Vectorization.

You can get it even faster with a quick steroid injection! How? Read on! :zap:

pip install numexpr

NumExpr is a nifty library which boosts the usual numerical expressions evaluation by using lesser memory and multi threading.

All you need to do is to wrap the same expression as a symbolic expression and pass it to the “evaluate” method.

As you see in the code, bigger the array sizes get, relatively faster the execution is!

An operation on an np array with 1e8 elements takes ~1s using normal numpy, whereas numexpr takes hardly 100ms!:rocket:

The evaluate expression is compiled by the python compile function into a byte code program.

Vector registers are then formed which handle the element wise operations.

Numexpr handles multiple chunks of elements parallelly and doesn’t use temporary arrays like numpy to avoid memory wastage.

Results? Almost a 10x boost!:fire:

What do you think about NumExpr?

P.S.: If your data is too large, it is advisable to go for frameworks like Dask. With Numpy, the array initialization itself will take considerable time.

#python #machinelearning #datascience