Working with arrays#
In this sheet, you will learn how to perform some simple manipulations on
arrays, as we did in the first semester with C++ vector
. In
Python, such arrays can be represented by array
from the
NumPy
library (usually abbreviated as np
):
import numpy as np
Two-Dimensional Arrays#
Here is a two-dimensional array with two rows and four columns:
T = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
T
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Note that we constructed the array with the np.array
function from a
list of two lists giving respectively the content of the first and second row
of the array. Then, we assigned the obtained array to the variable T
.
The sizes of this array can be found with:
T.shape
(2, 4)
You will recall that C++ vector
are intrinsically one-
dimensional arrays, and that we emulate two-dimensional arrays with arrays of
arrays. Here, on the other hand, numpy array
allow us to explicitly construct
two-dimensional arrays.
Exercise#
Inspired by the example above, construct a three-row, three-column table containing the integers from 1 to 9 from left to right and top to bottom, as in the following figure:
1 2 3
4 5 6
7 8 9
### BEGIN SOLUTION
T2 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
### END SOLUTION
T2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
We are testing the table’s format:
assert T2.shape == (3,3)
as well as its content:
assert [ T2[i,j] for i in range(3) for j in range(3) ] == [1, 2, 3, 4, 5, 6, 7, 8, 9]
Here’s how to access the content of an individual cell in the table:
T2[1,2]
np.int64(6)
This cell is in the second row and third column: indeed, as in C++, rows
and columns are numbered starting from 0.
If you want to extract an entire row, or an entire column, you replace the coordinate
that you want to leave free by :
. Thus, in the following example, we extract the
second column (therefore of index \(1\)) by leaving the row index free, and fixing
the column index to 1
:
T2[:,1]
array([2, 5, 8])
Extract the second row from the table and assign it to the variable li
, then display
its contents:
### BEGIN SOLUTION
li = T2[1,:]
li
### END SOLUTION
array([4, 5, 6])
assert isinstance(li, np.ndarray)
assert li.shape == (3,)
assert list(li) == [4,5,6]
Three-dimensional and higher-dimensional arrays#
For the moment, we have used two-dimensional arrays. Later,
especially for representing images, we will need arrays of greater
dimension: a single number is not enough to represent a pixel.
Numpy
allows you to represent arrays of any dimension. Here is an array of
dimension 3:
T3D = np.array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]],
[[10,11,12], [13,14,15], [16,17,18]],
[[19,20,21], [22,23,24], [25,26,27]]
])
It can be seen as a three-layer array:
To access a cell of the array, we use T[i,j,k]
, where i
is the row number,
j
is the column number, and k
is the layer number containing the cell.
As with lists, we can extract sub-arrays with slicing operators.
(slicing).
Reminder: slices (slices)
When L
is a list, the operation L[start:stop:step]
allows extracting a
slice of L
, a list containing all the elements of L
with an index between start
(inclusive) and stop
(exclusive) in steps of step
. By default, start
is 0
, stop
is
len(L)
, and step
is 1
. Thus, L[:3]
contains the first three elements of L
(with index i<3
) while L[3:]
contains the following elements (with index 3<=i
).
More generally, this operation applies to most objects indexed by integers.
We can thus extract these layers as follows:
T3D[:,:,0]
array([[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]])
T3D[:,:,1]
array([[ 2, 5, 8],
[11, 14, 17],
[20, 23, 26]])
T3D[:,:,2]
array([[ 3, 6, 9],
[12, 15, 18],
[21, 24, 27]])
Exercises#
Extract the first column from the second layer of T3D
and store it in the
variable C
:
### BEGIN SOLUTION
C = T3D[:,0,1]
### END SOLUTION
C
array([ 2, 11, 20])
Note that this is a one-dimensional array, hence the inline notation!
assert list(C) == [2, 11, 20]
Now, extract a table containing the first column of each of the three
layers of T3D
and store it in the variable C
. Note that we want these
columns to be well represented by columns in C
!
Todo
Ajouter une indication: Cela se fait en un seul appel à T[…,…,…]. Pour chacune des trois coordonnées (numéro de ligne, de colonne, de couche) décidez si vous souhaitez la fixer ou la laisser libre.
### BEGIN SOLUTION
C = T3D[:,0,:]
### END SOLUTION
C
array([[ 1, 2, 3],
[10, 11, 12],
[19, 20, 21]])
for i in range(3):
assert np.array_equal(T3D[:,0,i], C[:,i])
Simple Statistics on Arrays#
Numpy allows us to perform simple statistics on arrays. Let’s return to our array
T
:
T
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Calculate manually:
the average of each column of
T
;the average of each row of
T
;the average of all the elements of the array
T
.
Compare your results with the following calculations. What does each command below calculate?
T.mean(axis=0)
array([3., 4., 5., 6.])
ANSWER HERE
T.mean(axis=1)
array([2.5, 6.5])
ANSWER HERE
T.mean()
np.float64(4.5)
ANSWER HERE
Conclusion#
So, you have seen all the elements of manipulating NumPy
arrays that we
will need today.