A mini NumPy tutorial

Here are a couple of little motivating examples about why we need vectorization when doing any kind of scientific computing. You will see what we mean by vectorization in the following example.

Let's perform a couple of simple calculations with Python. We have two examples:

  • First, let's say we have some distances and times and we would like to calculate the speeds:
distances = [10, 15, 17, 26]times = [0.3, 0.47, 0.55, 1.20]# Calculate speeds with Pythonspeeds = []for i in range(4):    speeds.append(distances[i]/times[i])speeds

Here we have the speeds:

[33.333333333333336,
 31.914893617021278,
 30.909090909090907,
 21.666666666666668]

An alternative to accomplish the same in Python methodology would be the following:

# An ...

Get Hands-On Predictive Analytics with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.