“In the next cell, the following code.
a = numpy.zeros( [3,2] )
This uses the imported numpy module to create an array of shape 3 by 2, with all the cells set to the value zero and assigns the whole thing to a variable named a . We then print a . We can see this array full of zeros in what looks like a table with 3 rows and 2 columns.
Now let’s modify the contents of this array and change some of those zeros to other values. The following code shows how you can refer to specific cells to overwrite them with new values. It’s just like referring to spreadsheet cells or street map grid references.
a[0,0] = 1
a[0,1] = 2
a[1,0] = 9
a[2,1] = 12
The first line updates the cell at row zero and column zero with the value 1, overwriting whatever was there before. The other lines are similar updates, with a final printout with “print(a)”. The following shows us what the array looks like after these changes.
Now that we know how to set the value of cells in an array, how do we look them up without printing out the entire array? We’ve been doing it already. We simply use the expressions like a [1,2] or a [2,1] to refer to the content of these cells which we can print or assign to other variables. The code shows us doing just this.
v = a[1,0]
You can see from the output that the first print instruction produced the value 2.0 which is what’s inside the cell at [0,1]. Next the value inside a [1,0] is assigned to the variable v and this variable is printed. We get the expected 9.0 printed out.
The column and row numbering starts from 0 and not 1. The top left is at [0,0] not [1,1]. This also means that the bottom right is at [2,1] not [3,2]. This catches me out sometimes because I keep forgetting that many things in the computer world begin with 0 and not 1.If we tried to refer to a [3,2] we’d get an error message telling us we were trying to locate a cell which didn’t exist. We’d get the same if we mixed up our columns and rows. Let’s try accessing a [0,2] which doesn’t exist just to see what error message is reported.
Arrays, or matrices, will be useful for neural networks because we can simplify the instructions to do the many many calculations for feeding signals forward and errors backwards through a network. We saw this in part 1 of this guide.
Just like large tables or lists of numbers, looking at large arrays isn’t that insightful. Visualising them helps us quickly get an idea of the general meaning. One way of plotting 2-dimensional arrays of numbers is think of them as flat 2-dimensional surfaces, coloured according to the value at each cell in the array. You can choose how you turn a value inside a cell into a colour. You might choose to simply turn the value into a colour according to a colour scale, or you might colour everything white except values above a certain threshold which would be black.
Let’s try plotting the small 3 by 2 array we created above.
Before we can do this, we need to extend Python’s abilities to plot graphics. We do this by importing additional Python code that others have written. You can think of this as borrowing food recipe books from your frien d to add to your own bookshelf, so that your bookshelf now has extra content enabling you to prepare more kinds of dishes than you could before.
The following shows how we import graphics plotting capability.
The “matplotlib.pyplot” is the name of the new “recipe book” that we’re borrowing. You might come across phrases like “import a module” or “import a library”. These are just the names given to the additional Python code you’re importing. If you get into Python you’ll often import additional capabilities to make your life easier by reusing other’s useful code. You might even create your own useful code to share with others!
One more thing, we need to be firm with IPython about plotting any graphics in the notebook, and not try to plot it in a separate external window. We issue this explicit order as follows: