Python and NumPy: List vs NumPy Arrays

Tutorial Video

Let’s look at basic numeric operations of Scalars, Vectors and Matrices using Python. In order to do this we need to use the Numeric Python NumPy:

Python

Scalars

In Python scalars can be created using numbers:

Python
3

They can also be made using square brackets:

Python
3

Addition of numbers can be carried out using:

Python
8

However if square brackets are enclosed around the scalars, instead of adding the sum of the two scalars, they are instead concatenated together into a single array:

Python
[3,5]

Vectors

This may be useful in some cases. However supposing we want to add two 1 row by 4 columns vectors together:

Python
[1, 2, 3, 4, 5, 6, 7, 8]

We find that once again we concatenate them instead of adding them together. For numeric operations, it is hence recommended to use a NumPy array opposed to a Python list:

Python
array([ 6,  8, 10, 12])
Python

These show up in the Variable Explorer, note the types are list and int32 respectively:

When a is examine din the variable explorer, each element in the list has its own Type (in this case all integers), each has a size, in this case each individual element has a size of 1 and these can be customised:

In contrast the NumPy array is totally numerical, and each cell can only contain one value.

So for instance, the following elements in the list can be updated:

Python

Trying the same for the NumPy array gives an error:

Python
Traceback (most recent call last):

  File "<ipython-input-5-f1938797665c>", line 1, in <module>
    b[0]='g'

ValueError: invalid literal for int() with base 10: 'g'
Python
Traceback (most recent call last):

  File "<ipython-input-6-316b8a7bcf3b>", line 1, in <module>
    b[1]=[1,2,3]

ValueError: setting an array element with a sequence.

In essence we are only allowed a scalar numeric value for each cell in the NumPy Array whereas a list can be made more complicated.

Matrix

Instead of a vector, we can create a matrix. It is again possible to do both as a list and as NumPy array:

Python

Note the matrix is input using the form:

Python

The inner square brackets surrounding Row0 corresponds to the single dimension of a Row vector, i.e. Row0 by itself is a Row Vector. The outside square brackets however correspond to the fact that, Row0, alongside Row1, together make up a Matrix.

In other words if on the outside there is 1 square bracket we have a 1D object and if there are two square brackets on the outside we have a 2D object.

Once again we can see the difference between the list and the NumPy Array.

3D Stack

To make a 3D array or stack of matrices we can use:

Python

Note this 3D Stack is of the form:

Python

Once again we can see the difference between the List and the NumPy Array:

The NumPy Array is listed as a Stack and has dimensions of 2 pages, 2 rows and 4 columns. By default axis 0 is selected an one may go through each slice (showing all elements of axis 0):

It is worth looking at the shape of the NumPy Arrays b, d and f using:

Python
np.shape(1)
Out[8]: ()

np.shape(b)
Out[8]: (4,)

np.shape(d)
Out[9]: (2, 4)

np.shape(f)
Out[10]: (2, 2, 4)

Here we see the Scalar gives no dimensions. This is because there is only 1 Row and 1 Column and thus there is no need to index any values in it as the entire scalar is just a single value. The Row Vector has 4 Columns. It also has 1 Row but because it is a single Row there is once again no need to index it. The Matrix has 2 Rows by 4 Columns and the Stack has 2 Pages, 2 Rows and 4 Columns.

This is perhaps seen more clearly with g, which has a different size in all the dimensions:

Python

Typically we can think of a 3D array as a book, where every page is the same size and we flip through the book to get to the next page. In this case each page is the matrix [p,:,:] this is along axes 0.

However it is also possible to change the axis, in this analogy it is better to think of the 3D array as a 3D object. We can look at axis 1 where we view the matrix [:,r,:] or axis 2 where we view the matrix [:,:,c].

This Stack has 2 Pages, 3 Rows and 4 Columns. The value 8 is on the 0th Page, 1st row and 3rd column. It can be indexed using:

Python
8

Or alternatively:

Python
8

Check your understanding: Index into g to yield the numbers 19, 17 and 2 respectively.

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.