The definition of the avg( x,y ) function is easy to understand as it uses only things we’ve seen already. It prints out the first and second numbers which the function gets when it is invoked. Printing these out isn’t necessary to work out the average at all, but we’ve done it to make it really clear what is happening inside the function. The next bit calculates ( x + y )/2.0 and assigns the value to the variable named a . We again print the average just to help us see what’s going on in the code. The last statement says “return a”. This is is the end of the function and tells Python what to throw out as the functions output, just like machines we considered earlier.

When we ran this code, it didn’t seem to do anything. There were no numbers produced. That’s because we only defined the function, but haven’t used it yet. What has actually happened is that Python has noted this function and will keep it ready for when we want to use it.

In the next cell enter “avg(2,4)” to invoke this function with the inputs 2 and 4. By the way, invoking a function is called calling a function in the world of computer programming. The output should be what we expect, with the function printing a statement about the two input values and the average it calculated. You’ll also see the answer on it’s own, because calling the function in an interactive Python sessions prints out the returned value. The following shows the function definition and the results of calling it with avg(2,4) and also bigger values (200, 301). Have a play and experiment with your own inputs.

You may have noticed that the function code which calculates the average divides the sum of the two inputs by 2.0 and not just 2. Why is this? Well this is a peculiarity of Python which I don’t like. If we used just “2” the result would be rounded down to the nearest whole number, because Python considers just “2” as an integer. This would be fine for avg(2,4) because 6/2 is 3, a whole number. But for avg(200,301) the average is 501/2 which should be 250.5 but would be rounded down to 250. This is all just very silly I think, but worth thinking about if your own code isn’t behaving quite right. Dividing by “2.0” tells Python we really want to stick with numbers that can have fractional parts, and we don’t want it to round down to whole numbers.

Let’s take a step back and congratulate ourselves. We’ve defined a reusable function, one of the most important and powerful elements of both mathematics and in computer programming.

We’ll use reusable functions when we code our own neural network. For example, it makes sense to make a reusable function that does the sigmoid activation function calculation so we can call on it many times.

Arrays

Arrays are just tables of values, and they come in really handy. Like tables, you refer to particular cells according to the row and column number. If you think of spreadsheets, you’ll know that cells are referred to in this way, B1 or C5 for example, and the values in those cells can be used in calculations, C3+D7 for example.

When we get to coding our neural network, we’ll use arrays to represent the matrices of input signals, weights and output signals too. And not just those, we’ll use them to represent the signals inside the neural networks as they feed forward, and also the errors as they propagate backwards. So let’s get familiar with them. Enter and run the following code.

import numpy

What does this do? The import command tells Python to bring additional powers from elsewhere, to add new tools to it’s belt. Sometimes these additional tools are part of Python but aren’t made immediately ready to use, to keep Python lean and mean, and only carry extra stuff if you intend to use it. Often these additional tools not core parts of Python, but are created by others as useful extras, contributed for everyone to use. Here we’ve imported an additional set of tools packaged into a module named numpy . Numpy is really popular, and contains some useful stuff, like arrays and an ability to do calculations with them.