NumPy for relatively simple computations
I often see that people use NumPy package for doing relatively simple math. Isn't this redundant? Why they cannot use just pure Python?
I often see that people use NumPy package for doing relatively simple math. Isn't this redundant? Why they cannot use just pure Python?
Because NumPy gives a performance boost. It is faster and more efficient to do the same computations using NumPy compared with pure Python.
Why NumPy is more efficient?
The computations in NumPy are highly optimized. One of the most important things is that NumPy works with vectorized computations which are more efficient that the same computations in an unvectorized form.
And what vectorization means?
It can be described as mathematical operations applied to the entire arrays (for example, NumPy arrays) instead of the individual elements of the arrays.
There is a good article on Wikipedia - https://en.wikipedia.org/wiki/Array_programming . Also, you can read more about NumPy, for example, this chapter of the NumPy tutorial - https://www.oreilly.com/library/view/python-for-data/9781449323592/ch04.html .
Can there be situations where NumPy is less effective than pure Python?
Probably any computation is more optimized in NumPy. Maybe some very simple calculations will take the same time in Python and in NumPy. But NumPy is not only optimized for speed, it also gives memory benefits. So, I would recommend using NumPy for all mathematical computations.
Just drop us an email to ...
Show more