NumPy Vectorization

This lesson teaches Numpy vectorization and explains it with a simple example using object-oriented, procedural and vectorized approach.

Vectorization, in simple words, means optimizing the algorithm so that it can run multiple operations from a single instruction. NumPy is all about vectorization. If you are familiar with Python, this is the main difficulty you’ll face because you’ll need to change your way of thinking and your new friends (among others) are named “vectors”, “arrays”, “views” or "ufuncs".

Note: A custom magic command timeit is used in all codes. It’s a tool for measuring the execution time of small code snippets.

Object Oriented Approach

Let’s take a very simple example, a random walk. One possible object-oriented approach would be to define a RandomWalker class and write a walk method that would return the current position after each (random) step. It’s nice, it’s readable, but it is slow:

Get hands-on with 1400+ tech skills courses.