Today we’ll be talking about the very basics of mathematical computations in Python using the numpy module. This is the first part of a series of articles devoted to the module.
numpy is a Python module, which, along with the matplotlib, scipy and pandas modules is used for complex mathematical computations.
The module is mostly written in C, so the precompiled functions work fast. It implements advanced data structures like multi-dimensional arrays and matrices.
Installation
numpy, as well as the other aforementioned modules are not part of the Standard Library, so you have to install it. You can choose any way of installing the module you like. Here’s how you can install the module using the standard pip installation:
In the console type:
pip install numpy
and wait for the installation to complete.
If you have the Anaconda distribution of Python on your machine, you’re good to go, all the necessary modules, including the one which is the subject of this article, are already there.
Working with numpy
With the module installed, we can start working with it. You can use any text editor to edit your code, like for example Visual Studio Code, or you can use the web application Jupyter Notebook, which is very comfortable to work with. This series, though, is just about the basics of numpy and the functionality available in VSC will do. So, create a new Python file and start working with the module.
Let’s import the module first:
import numpy as np
We usually use the np alias for the module.
When we work with numpy, we use arrays. They’re very handy to use.
We’ll start with a regular Python list to calculate the distances in kilometers and miles and then we’ll convert the list to a 1-dimensional numpy array and make the same calculations. This way you’ll see how concise and comfortable to use arrays are.
So, here’s our example. First the regular Python list:
import numpy as np
# distances in kilometers
km = [240, 322, 124, 580, 456, 211, 785]
# We can use a list comprehension to make a list of the corresponding
# distances in miles.
mi = [d / 1.609 for d in km]
print(mi)
Here’s the output:
[149.16096954630206, 200.12430080795525, 77.06650093225606, 360.47234307022995, 283.4058421379739, 131.13735239279055, 487.88067122436297]
Let’s convert our list to a numpy array and use it in our calculations:
import numpy as np
# distances in kilometers
km = [240, 322, 124, 580, 456, 211, 785]
# Let's convert the list to a numpy array.
KM = np.array(km)
# Let's print the numpy array.
print("Distances in kilometers: \n", KM)
# Let's now create another numpy array to store the distances in miles.
# We don't need a list comprehension here. The syntax is simpler:
MI = KM / 1.609
# Let's print the new array.
print("Distances in miles: \n", MI)
And here’s the output:
Distances in kilometers:
[240 322 124 580 456 211 785]
Distances in miles:
[149.16096955 200.12430081 77.06650093 360.47234307 283.40584214 131.13735239 487.88067122]
This one example will do for now. We’ll have more examples of numpy in the upcoming parts of the series, so stay tuned.
You can also watch the video version: