Python challenge: Simple introduction to numpy
This blog post provides a simple introduction to numpy.
What is numpy?
Numpy is a Python library offering a large variety of mathematical functions. It is particularly popular because of its support for matrix-related functionality.
How to install numpy?
Since numpy is not a part of the standard Python installation, it needs to be installed separately. One way of doing so is using pip3:
pip3 install numpy
Using numpy
Creating arrays
There are a couple of functions that can be used to create and initialize arrays The following creates an array with 3 numbers; 3, 5, and 9:
As opposite, we can create an empty array of 3 elements.
In some cases, it will be convenient to initialize an array, in which each element has the same value. For instance, one would like to initiate an array with zeros or ones:
Reading data from a file
The functions demonstrated above are used when arrays are populated “manually”. However, a more common way to populate arrays are by reading the data from a file.
This can be done using numpy´s loadtxt
function.
Suppose we need to read the data from the following file:
Reservations Beers
13 33
2 16
14 32
23 51
The data was collected in a pub/restaurant. It shows the number of reservations along with the number of beers ordered the same day.
Now, let´s say we would like to store each column in the file in a 1-dimensional array:
This reads the context from beer.txt, skips the first row in the textfile and saves the values in each column to an array (X and Y).
Mathematical functions
Numpy provides many different mathematical functions, some of which are shown below:
Matrix operations
Creating a matrix
One way to create a matrix with numpy is using the aforementioned functions zeros
and ones
.
This creates a matrix with 3x3 elements initialised with 0.
If one wants to initialise the matrix with vaues from a file, the function column_stack
can be used:
Manipulating a cell´s value
The value of a cell can be changed using the row and column index of the cell:
Inserting a row
Numpy´s insert function takes a couple of parameters: The first one is the affected matrix, the second is the index where the row is inserted. The third parameter is the value to insert. The last one indicates if a row or a column is inserted. If axis
is equals 0, a row is inserted.
Inserting a column
When inserting a column, the last parameter (axis
) needs to be 1
Deleting rows and columns is done in a similar way.