Lecture 3 : Scientific computing with Python

April, 2022 - François HU

Master of Science in Artificial Intelligence Systems - EPITA

This lecture is available here: https://curiousml.github.io/

image.png

Table of contents

Introduction

  1. Introduction to NumPy
  2. Python List VS NumPy Array
  3. Array creation
  4. Array modification
  5. Indexing and slicing
  6. Operation on arrays
  7. Basic mathematics/statistics
  8. [optional] Basic linear algebra

Exercices

Introduction

So far we have seen (almost) only built-in python objects. Let us now study some external packages: by providing additional tools, these packages can be seen as an "extention" of Python to specific domains such as software engineering, scientific computing, statistics or data science.

The present lecture is about scientific computing with python. In a nutshell, this course is centered on exploring the package NumPy that provides both array manipulation tools for python and a collection of modules for various mathematical purposes.

How to install a python package in Ananconda? You have access to pip and conda for installing packages from The Python Package Index (PyPI) a repository of software for the Python programming language. For example let us install the package NumPy:

  1. Open the anaconda prompt
  2. the following command can be used to install any Python package from PyPI in Anaconda :
    pip install package_name
    In order to install numpy, replace package_name by numpy

1. Introduction to NumPy

NumPy (can be written as numpy) is the core library for scientific computing in Python. This package provides

A numpy array is a collection of items, all of the same type. As a recall, the Python core library provided Lists. A list is the Python equivalent of an array, but is resizeable and can contain elements of different types. We can find some similiraties with these two data types:

Then why do we need bother studying numpy array ? Why not using the list instead ? The answer is performance.

2. Python List VS NumPy Array

Remark: NumPy Arrays are recommended when working with longer sequences of homogenous data.

The following sections provide an introduction to NumPy.

Let us first import the package numpy and let us rename it as np

3. Array creation

From an existing list

you can transform a list into an array (and vice-versa) thanks to the function array of numpy.

From numpy routines

One can also use some numy functions to create a numpy array with existing items (these items could be modified later). see the documentation for further details on the parameters.

We can also generate random values (of an array) with the sub module random of numpy:

multidimensional array with random integers betweeen low (inclusive) and high (exclusive)

If we want to regenerate the same random values, we have to set the random seed of the numpy pseudo-random number generator.

Information about an array object

These functions create an array object. After its initialization, it has some useful attributes

dtype the type of items

4. Array modification

Shape modification

After its creation, you can modify its shape with many methods of array. One of the most popular is resize.

if one want to "resize" or "ravel" an array object without modifying it but creating a copy, there are methods reshape and flatten.

Note that when we reshape an array object, we can omit one dimension (and replace the value by -1) and let the method compute the value of it.

We can also transpose an array using the attribute T

Repetition and concatenation

Arrays can also be repeated and concatenated. Let us recall A and B previously created

These last two operations can also be done by stacking arrays vertically or horizontally.

5. Indexing and slicing

As a list, array object can be indexed, sliced and iterated over.

Basic indexing, slicing and iterating

like lists, the first item of the array corresponds to the first row

like lists, the items in the grid can be accessed by

alternatively, in numpy arrays we can use

Advanced indexing with Arrays

There are two fancy way to index/slice your numpy array:

  1. specify the wanted indices with lists (or arrays)
  2. request the wanted indices with booleans (in this context a.k.a masks)
  1. with lists
  1. with booleans

In numpy array, it is possible to compare a numpy array with a scalar (integer for example).

We can consider these values as a mask:

This method is quite useful for assignments

Useful numpy methods: where, any all all

6. Operation on arrays

In numpy, operations on arrays are elementwise. The result is returned in a new array.

Basic operations

This approach is called broadcasting.

Broadcasting

One of the most advanced feature of numpy array is broadcasting: the possibilty to do operations with arrays of different shapes with fast implementations.

The main rule of broadcasting is that the dimensions of two arrays are compatible when:

  1. they are equal;
  2. one of them is 1;
  3. one of them is a value/scalar (see above). In the second case, numpy operates as though the items in the axis of dimension 1 were identically repeated to meet the dimension of the other array.

Example for the following arrays:

7. Basic mathematics/statistics

As stated in the introduction, a major feature of numpy is to provide mathematical methods with fast implementations.

Operations

Mathematical functions

Numpy provides some functions like cos, sin, exp or log (see Universal functions for more built-in functions). It also provides constants such as $\pi$.

8. [optional] Basic linear algebra

As stated before, numpy provides many efficient linear algebra operations. Fun fact: numpy has built-in Matrix object but users generally prefer to handle arrays with routines from linear algebra. Some examples are provided below:

Exercices

Exercice 1.

Generate the following arrays thanks to numpy methods and without explicit loops and without manually fill in all the numbers (let us name them respectively arr1, arr2 and arr3):

[[ 0  4  8 12 16]
 [ 1  5  9 13 17]
 [ 2  6 10 14 18]
 [ 3  7 11 15 19]]
[[ 0  1  4  9 16]
 [ 1  4  9 16 25]
 [ 4  9 16 25 36]
 [ 9 16 25 36 49]]
[[0 1 0 1 0 1]
 [1 0 1 0 1 0]
 [0 1 0 1 0 1]
 [1 0 1 0 1 0]
 [0 1 0 1 0 1]
 [1 0 1 0 1 0]]

[optional] Exercice 2.

Standardize each array (arr1, arr2 and arr3) such that for each array:

Exercice 3.

From arr1 extract the submatrix:

[[ 6 18 10]
 [ 7 19 11]
 [ 5 17 9]]

Exercice 4.

Replace all the items with value 0 of arr3 by -1

Exercice 4 bis

Create a random vector of size 20 (all values between $[0, 1)$) and replace:

[optional] Exercice 5.

Let $A$ be a matrix and $b$ be a vector defined by:

>>> V = np.random.rand(3, 3)
>>> A = V + V.T
>>> b = np.random.rand(3)

Solve the linear system $Ax = b$.