39

Vectorization in Python

 4 years ago
source link: https://towardsdatascience.com/vectorization-in-python-46486819d3a?gi=7952a2893e8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PyTrix#1: Speeding up our Python Code

May 7 ·4min read

Python is a popular language for Data Science. With it’s easy to learn (and read) syntax it makes getting up and running with the language much more accessible for newbies. However, without getting into the details, Python is an interpreted language which means it runs much slower than a compiled language , like C.

When we perform Deep Learning it’s likely we are using large amounts of data because that is when Deep Learning thrives.

N3eauaU.png!web

Figure 1: Photo from Andrew Ng Deep Learning Specialization on Coursera

Why am I saying all of this? Great question!

If we have large amounts of data and slow python code, we are more than likely going to end up with a model that runs at snails pace because our code is not computationally optimal... What was man’s solution to this great disaster? Vectorization! B-)

What is Vectorization?

To put it in layman’s terms, It speeds up Python code without the need for looping, indexing, etc., and in Data Science we use Numpy to do this — Numpy is the de facto framework for scientific programming. Technically, we still perform these operations when we implement the vectorized form in Numpy, but just not in Python — under the hood. Instead, the operations are done in optimised, pre-compiled C code — see the Documentation for more information on this.

“This practice of replacing explicit loops with array expressions is commonly referred to as vectorisation. In general, vectorized array operations will often be one or two (or more) orders of magnitude faster than their pure python equivalents, with the biggest impact seen in any kind of numerical computations” — McKinney, 2012, p. 97

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK