ULG 971001 - Software (VU 2) - python, git, docker

Introduction to NumPy

NumPy is a Python package for scientific computing with an open source licence (BSD license). It provides a multidemsional array object and a large collection on high-level mathematical routines for fast operations on these arrays. This includes logical operations, shape manipulation, sorting, selecting, basic linear algebra (BLAS), basic statistical operations, random simulation and much more. It is wildly used and the basis for many other packages. It was introduced to allow fast array computation in python and it is keeping its promises ever since.

NumPy itself is build on other components such as Basic Linear Algebra Subroutines written in Fortran, C, CUDA or whatever required for the specific task. For this purpose it can be linked against Intel Math Kernel Library or OpenBLAS.

These notes are heavily inspired by the notes of Gregor Ehrensperger from previous years in this class.

First steps

We load the package as per usual and by common convention we give it the namenp.

import numpy as np

We can create arrays with a multitude of function

# 1D-array with 5 integers
X = np.array([1, 2, 3, 5, 7])

# 2x3 2D-array with floating point numbers
Y = np.array([[1.1, 2.1, 3.1], [1.2, 2.2, 3.2]])

# We can also create an array of zeros
Z = np.zeros((2, 3))

# Or an equally spaced array of 6 elements between 1 and 10
A = np.linspace(1, 10, 6)

Now that we have our arrays we also need to talk about how to access the elements.

Python uses 0 based indexing!

We start of with one dimensional arrays:

>>> X = np.array([1, 2, 3, 5, 7])
>>> X[2]
3

>>> X[0]
1

>>> X[-1]
7

>>> X[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 6 is out of bounds for axis 0 with size 5

and extend to two dimensional arrays as:

>>> Y = np.array([[1.1, 2.1, 3.1], [1.2, 2.2, 3.2]])
# element at row and column 0
>>> Y[0, 0]
1.1

# element at row 0 and column 2
>>> Y[0, 2]
3.1

# element at row 1 and second to last column
>>> X[1, -2]
2.2

Of course we can also use slicing, i.e. accessing subarrays, with the following rules (per dimension) x[start:stop:step]

  • start is by default 0

  • stop is by default the length of the dimension

  • step is by default 1

This allows us to do do the following.

>>> X = np.array([1, 2, 3, 5, 7])

>>> X[0:3:1]
array([1, 2, 3])

>>> X[:3]
array([1, 2, 3])

>>> X[3:]
array([5, 7])

>>> X[::2]
array([1, 3, 7])

>>> Y = np.array([[1.1, 2.1, 3.1], [1.2, 2.2, 3.2]])
# first row
>>> Y[0, :]
array([1.1, 2.1, 3.1])

# last row
>>> Y[-1, :]
array([1.2, 2.2, 3.2])

# every second column
>>> Y[:, ::2]
array([[1.1, 3.1],
       [1.2, 3.2]])
Please solve the following exercises:

  1. Create the following matrix as an numpy array

A=(012345678910111213141516171819). A = \begin{pmatrix} 0 & 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8 & 9\\ 10 & 11 & 12 & 13 & 14\\ 15 & 16 & 17 & 18 & 19\\ \end{pmatrix}.

Hint look into the NumPy functions arange and reshape.

  1. How do you access the element 7?

  2. How do you access the entire third column?

  3. Return an array with only the odd rows and even columns:

Solution

>>> A  = np.reshape(np.arange(0, 20), (4, 5))
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

>>> A[1,2]
7

>>> A[:, 2]
array([ 2,  7, 12, 17])

>>> A[1::2, ::2]
array([[ 5,  7,  9],
       [15, 17, 19]])

\,

Link to the session notes

Session Notes

CC BY-NC-SA 4.0 Peter Kandolf. Last modified: January 19, 2024. Website built with Franklin.jl and the Julia programming language.