This guide is obsolete, it was written during the early stages when I was learning python. A more up to date guide is available here:

Other python guides including installation of python are available in:

Table of contents

- Perquisites
- Function sort and argsort
- Function amax, argmax and maximum
- Find the Maximum Value Between Two Scalars, Single Vector or Two Vectors
- Function amax Find the Maximum in Each Row or Column of a Matrix
- Function argmax Find the Index of the Maximum Value of a Vector
- Function argmax Find the Index of the Maximum Values for each Column or Row in a Matrix
- Function maximum Find the Maximum Value of Two Matrices

- Find the Maximum Value Between Two Scalars, Single Vector or Two Vectors
- Function amin, argmin and minimum
- Function amin Find the Minimum Value Between Two Scalars, Single Vector or Two Vectors
- Function amin Find the Minimum in Each Row or Column of a Matrix
- Function argmin Find the Index of the Minimum Value of a Vector
- Function argmin Find the Index of the Minimum Values for each Column or Row in a Matrix
- Function minimum Find the Minimum Value of Two Matrices

- Function amin Find the Minimum Value Between Two Scalars, Single Vector or Two Vectors
- Function sum
- Function prod
- Function mean
- Function median
- Function mode
- Function var
- Function std
- Function cumsum
- Function cumprod
- Function diff

## Perquisites

In this guide, we are going to use the numeric Python library NumPy. To use it we'll import it as np as standard:

## Function sort and argsort

### Function sort and argsort – sort values in a Vector or find the argument indexes of lowest to highest values

Let's us create a vector:

We can use the functions sort and argsort to sort the values from lowest to highest or alternatively to list the order of the arguments from lowest to highest.

[1 3 2]

[1 2 3]

sort listed the values from lowest to highest. If we want highest to lowest we can flip the result.

[1 3 2]

[3 2 1]

[1 3 2]

[0 2 1]

argsort, tells us from v, the element with the lowest value. In this case it is 1 at index 0, then the second lowest value, in this case it is 1 at index 2 and finally the third lowest value, in this case it is 3 at index 1.

### Function sort and argsort Sort or find the argument indexes of lowest to highest values for either all Elements in a Matrix or across columns or rows

Let's create a new matrix:

[[3 6 2]

[8 1 4]

[5 9 7]]

We can sort out all elements in this matrix by flattening it to convert it to a vector. In essence, the second row is added to the end of the first row and the third row is added to the end of the second row and so on and so forth.

[3 6 2 8 1 4 5 9 7]

Then we can use sort and sort out the flattened matrix (which is now a vector).

[1 2 3 4 5 6 7 8 9]

If we wanted the reverse order we could flip this result as we done earlier for the row vector.

Alternatively we can use argsort to get the index of the lowest to highest value:

[3 6 2 8 1 4 5 9 7]

[4 2 0 5 6 1 8 3 7]

We can look at sorting a matrix by columns or rows. This is done by using the input argument argument axis. An axis of 0 will work on columns and an axis of 1 will work on rows. Many numpy functions will work on the entire matrix without an axis argument, (others will default to axis 0 and the matrix needs to be flattened to work on the entire matrix). Let's look at Columns:

[[3 6 2]

[8 1 4]

[5 9 7]]

[[3 1 2]

[5 6 4]

[8 9 7]]

If we wanted the result in reverse order, we could use the function flipud to slip the columns:

[[8 9 7]

[5 6 4]

[3 1 2]]

Now let's look at Rows:

[[3 6 2]

[8 1 4]

[5 9 7]]

[[2 3 6]

[1 4 8]

[5 7 9]]

This time if we want the reverse order we can use the function fliplr to flip the rows:

[[6 3 2]

[8 4 1]

[9 7 5]]

We can now also have a look at using argsort, for each column. Note that it has the same form as sort.

[[3 6 2]

[8 1 4]

[5 9 7]]

[[0 1 0]

[2 0 1]

[1 2 2]]

Now we see that each column has an index 0, 1 and 2 where 0 is the lowest value in each column, 1 the next lowest value in column and 2 the next lowest value. We can see that although each column has a 0,1 and 2 value, they are not all in the same place.

We can work on rows:

[[3 6 2]

[8 1 4]

[5 9 7]]

[[2 0 1]

[1 2 0]

[0 2 1]]

This time we see there is a 0,1 and 2 in each row.

## Function amax, argmax and maximum

### Find the Maximum Value Between Two Scalars, Single Vector or Two Vectors

Lets have a look at two scalars and compare them to see what one is maximum. To this we can use the function amax.

2

For a scalar there is only a single axis, the 0 axis. Note [s1,s2] is also a vector so we employ the same method to find the maximum value in a single vector. Lets create:

3

Or combining the top two lines into a single line:

3

We can instead compare two vectors:

[2 2 4]

### Function amax Find the Maximum in Each Row or Column of a Matrix

Lets create a matrix M where:

We can calculate the maximum in each column using the following. Note that we set axis=0, so we operate on each column:

[13 15 18]

We can change the axis to 1 to instead find the maximum in each row:

[ 5 11 18]

### Function argmax Find the Index of the Maximum Value of a Vector

Returning to the case of the single vector we seen before, we may be interested in knowing not only the maximum value, but also the location of the maximum value. To do this we can use the function argmax which has a similar form to amax:

3

2

The maximum value is 3, at index 2 (recall that we are using zero order indexing).

3

### Function argmax Find the Index of the Maximum Values for each Column or Row in a Matrix

Lets return to matrix M where:

We can calculate the maximum in each column using the same line as code as before. Note that once again we set axis=0, so we operate on each column. The function argmax takes the same form as amax:

[13 15 18]

[2 2 2]

In this case, the maximum values are all in position 2, recall once again that we were using 0 order indexing.

We can change the axis to 1 to instead find the maximum and maximum location in each row:

[ 5 11 18]

[2 2 2]

### Function maximum Find the Maximum Value of Two Matrices

Let's create two matrices M and N where:

To create these arrays in Python, we can use:

To find the maximum value between the two matrices

[[ 2 4 6]

[ 8 10 12]

[14 16 18]]

## Function amin, argmin and minimum

### Function amin Find the Minimum Value Between Two Scalars, Single Vector or Two Vectors

Lets have a look at two scalars and compare them to see what one is minimum. To this we can use the function amin.

1

For a scalar there is only a single axis, the 0 axis. Lets look at a vector:

1

Or combining the top two lines into a single line:

1

We can instead compare two vectors:

[1 1 3]

### Function amin Find the Minimum in Each Row or Column of a Matrix

Lets create a matrix M where:

We can calculate the minimum in each column using the following. Note that we set axis=0, so we operate on each column:

[1 4 5]

We can change the axis to 1 to instead find the minimum in each row:

[ 5 11 18]

### Function argmin Find the Index of the Minimum Value of a Vector

Returning to the case of the single vector we seen before, we may be interested in knowing not only the maximum value, but also the location of the minimum value. To do this we can use the function argmin which has a similar form to amin:

1

0

The minimum value is 1, at index 0 (recall that we are using zero order indexing).

1

### Function argmin Find the Index of the Minimum Values for each Column or Row in a Matrix

Lets return to matrix M where:

We can calculate the mainimum in each column using the same line as code as before. Note that once again we set axis=0, so we operate on each column. The function argmin takes the same form as amin:

[1 4 5]

[0 0 0]

In this case, the minimum values are all in position 0, recall once again that we were using 0 order indexing.

We can change the axis to 1 to instead find the minimum and minimum location in each row:

[1 7 13]

[0 0 0]

### Function minimum Find the Minimum Value of Two Matrices

Let's create two matrices M and N where:

To create these arrays in Python, we can use:

To find the maximum value between the two matrices

[[ 1 3 5]

[ 7 9 11]

[13 15 17]]

## Function sum

### Function sum Find the Sum of all Elements in a Vector

[1 3 2]

6

### Function sum Find the Sum of all Elements in a Matrix or across columns or rows

Let's create a new matrix:

We can create this using:

[[1 2 3]

[4 5 6]

[7 8 9]]

Or we can instead use the functions arange and reshape. Recall arange creates a vector of integer numbers starting from 0 (zero order indexing) and we go up to but don't include the value we enter.

[0 1 2 3 4 5 6 7 8]

We want to start at 1 instead of zero so we can plus 1:

[1 2 3 4 5 6 7 8 9]

Alternatively we could use two input arguments to specify a Start and Stop:

[1 2 3 4 5 6 7 8 9]

We have a vector of 9 elements opposed to a 3 by 3 matrix. We can use the function reshape to make this matrix:

Combining arange and reshape:

[[1 2 3]

[4 5 6]

[7 8 9]]

[[1 2 3]

[4 5 6]

[7 8 9]]

Let's look at the sum of all elements:

45

Let's have a look at the sum of columns and the sum of rows, recall axis=0 works on columns and axis=1 works on rows:

[12 15 18]

[ 6 15 24]

## Function prod

### Function prod Find the Product of all Elements in a Vector

[1 3 2]

6

### Function prod Find the Product of all Elements in a Matrix or across columns or rows

[[1 2 3]

[4 5 6]

[7 8 9]]

The function prod has a similar form to the sum function except it calculates the product of all elements opposed to the sum of all elements (1×2×3 opposed to 1+2+3).

Let's look at the product of all elements:

362880

Let's have a look at the product of columns and the product of rows, recall axis=0 works on columns and axis=1 works on rows:

[ 28 80 162]

[ 6 120 504]

## Function mean

### Function mean Find the Mean or Average of all Elements in a Vector

The mean or average of all values in a vector is defined as:

Recall that we are using 0 indexing so in the sum we start at element 0 and go up in integer steps to *m* but don't include *m*. For the vector with three elements, this becomes:

The mean is:

Let's create the simple vector:

And find it's mean:

[1 3 2]

2.0

### Function mean Find the Mean or Average of all Elements in a Matrix or across columns or rows

Let's continue with the matrix M:

[[1 2 3]

[4 5 6]

[7 8 9]]

Let's look at the mean of all elements. The mean is the sum of all elements divided by the number of elements.

5.0

Recall that the sum before was 45 and there are 9 elements. 45 divided by 9 gives the value 5. Let's have a look at the mean of columns and the mean of rows, recall axis=0 works on columns and axis=1 works on rows:

[4. 5. 6.]

Once again this is the same as the sum divided by the number of elements, in this case each column has 3 elements.

[2 5 8]

Once again this is the same as the sum divided by the number of elements, in this case each row has 3 elements.

There are other metrics we can look at such as the median and the mode. First lets look at the limitations with using the mean matrix M:

[[ 7 1 4 3]

[ 2 9 100 8]

[ 10 5 6 11]]

This data contains a clear outlier, let's look at the mean of the the matrix and then each column and row using:

13.833333333333334

[ 6.33333333 5. 36.66666667 7.33333333]

[ 3.75 29.75 8. ]

This gives a number that is substantially larger than all the usual elements due to the presence of the outlier. The 2nd element of the mean of all columns and 1st element of all rows are also clear outliers influenced by this outlier.

## Function median

### Function mean Find the Mean or Average of all Elements in a Vector

To calculate the median, in a vector, we sort out all the elements in a vector from the lowest to the highest value and then take the middle number. For instance:

If there are an even number of elements for instance:

Then the median is the middle value of the the two middle values:

Let's return to the vector:

Let's sort it out lowest to highest and then calculate its median:

[1 3 2]

[1 2 3]

2.0

### Function median Find the Median of all Elements in a Matrix or across columns or rows

Let's us look at this M again. Instead of looking at the mean, we will take the median, that is the middle number:

[[ 7 1 4 3]

[ 2 9 100 8]

[ 10 5 6 11]]

Let's look at the median of all elements. The median simply lists all elements in order and takes the middle value or the mean of the two middle values if there are an even number of values. To have a look at how this works, we can flatten the matrix and then sort the flattened matrix look at all the elements in order:

[ 7 1 4 3 2 9 100 8 10 5 6 11]

[ 1 2 3 4 5 6 7 8 9 10 11 100]

Here the middle values are 6 and 7, the mean of 6 and 7 is 6.5.

6.5

As we can see this value is far more representative of this dataset than the value 13.8333 which was much more heavily influenced by the outlier.

Let's have a look at the median of columns and the median of rows, recall axis=0 works on columns and axis=1 works on rows:

[[ 2 1 4 3]

[ 7 5 6 8]

[ 10 9 100 11]]

[7. 5. 6. 8.]

Once again the 2nd element 6, is far more representative of the data set than value of 36.6667 which was highly influenced by the outlier.

[[ 1 3 4 7]

[ 2 8 9 100]

[ 5 6 10 11]]

[3.5 8.5 8.]

## Function mode

### Function mode Find the Mode of all Elements in a Vector

The mode is the most frequently occurring value in a data set of discrete values.

Let's create the vector:

Let's look at the mode of the vector, note the mode although commonly used, for some reason is not included in NumPy so we have to import SciPy and load it from stats:

[1 1 2 1 1 3 1 1 1]

[1 1 1 1 1 1 1 2 3]

ModeResult(mode=array([1]), count=array([7]))

### Function mode Find the Mode of all Elements in a Matrix or across columns or rows

Let's us look at another matrix M of discrete values:

[[ 1 2 9 9]

[ 1 9 14 9]

[11 9 14 14]]

Let's look at the mode. The mode simply lists all elements in order and takes the most frequent value. Clothe sizes are in discrete sizes, if a fashion designer wants to test how well a certain design sells, they are likely to first make it available in the mode size.

Let's look at the mode of all elements:

[ 1 2 9 9 1 9 14 9 11 9 14 14]

[ 1 1 2 9 9 9 9 9 11 14 14 14]

ModeResult(mode=array([9]), count=array([5]))

Here in the entire dataset, the mode is 9 and it occurs 5 times.

Let's have a look at the mode of columns and the median of rows, recall axis=0 works on columns and axis=1 works on rows. In real data, this could be samples taken in different countries for instance:

[[ 1 2 9 9]

[ 1 9 14 9]

[11 9 14 14]]

ModeResult(mode=array([[ 1, 9, 14, 9]]), count=array([[2, 2, 2, 2]]))

[[ 1 2 9 9]

[ 1 9 9 14]

[ 9 11 14 14]]

ModeResult(mode=array([[ 9],

[ 9],

[14]]), count=array([[2],

[2],

[2]]))

## Function var

### Function var Find the Variance of all Elements in a Vector

The variance in Python is defined as:

Let's have a look at the formula above. Firstly let's look at the numerator only. In the term in the brackets, we are looking at the difference between a value and its mean. If we take the case of the vector:

In this case we can first calculate the mean:

The mean value is 2, the difference between 1 and 2 is -1, the difference between 2 and 2 is 0 and the difference between 3 and 2 is 1. Clearly if we sum these together however we get 0, which says the difference between our average data and the mean is zero, as expected because the definition of the mean is the value of our average data. To get a metric of the difference between values and a mean, we want a value that is always positive irregardless if it is above or below the mean. For this reason, we instead take the sum of the square of the difference terms which will always return a positive value. This gives us:

In other words as some points are above the mean and some points are below the mean, all difference terms are squared to give a positive number indicating a difference. We can calculate the variance of all elements of the matrix or using axis=0,1 to look at the variance of each column or row respectively.

Next clearly, as the number of points increases, this value be definition would also increase, we need a normalisation factor which compensates for this. By default, this variance factor is merely the number of points which gives us the equation below:

In Python to get this we would type:

[1 2 3]

0.6666666666666666

In the above, the number of degrees of freedom or numerator was set to equal the number of points. This means for data with only a single point the variance is 0. In such data the mean of a single value equals the value of the single value. Therefore their difference and square of their difference is 0. 0 divided by 1 is zero, meaning a single point has no variance. However in reality if we only measure a data point once, we cannot be sure that it is the correct value or an outlier. For this reason the denominator is set to the number of points minus 1. In python this is done by the addition of an additional input argument delta degrees of freedom (ddof) which is set to 1 opposed to the default value of 0. If we take the case of a single data point now, the numerator is 0 but the denominator becomes 1-1 which is also 0 and the variance 0 divided by 0 is undefined, which is more realistic of a sample size of 1 (opposed to saying it perfectly matches the mean and has no variance). In general the number of degrees of freedom is set equal to the number of points minus 1. Of course for a large number of points the difference between n and n-1 becomes subtle.

If we think of the number of points as nodes on a rope. We are holding the first node in our hand, it is our reference point (height of our hand). All other nodes on the string are free to vary relative to the height of our hand. For a rope with 1 node, there is no variation, the only node is at the height of our hand. For a rope with 2 nodes, the first node is fixed to the height of our hand but the second point can vary, it has one degree of freedom which is the number of nodes 2, minus 1. Likewise if the rope has 3 nodes, the node we are holding in our hand is fixed and the remaining 2 are free which is the number of nodes 3, minus the fixed point 1.

The variance can be calculated with n-1 degrees of freedom by using an additional input argument delta degrees of freedom ddof. If we set it to 1, it will give n-1 opposed to the default value of 0.

[1 2 3]

1.0

### Function var Find the Variance of all Elements in a Matrix or across columns or rows

Lets look at the matrix:

array([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

[[1 2 3]

[4 5 6]

[7 8 9]]

Previously we calculated its mean of all elements, columns and rows respectively:

5.0

[4. 5. 6.]

[2. 5. 8.]

We can calculate the variance of all elements using:

Because the negative square of a scalar is equal to the positive square of a scalar, this can be simplified as:

7.5

We can calculate the variance of the columns using:

[6. 6. 6.]

[1. 1. 1.]

## Function std

### Function std – Find the Standard Deviation of all Elements in a Vector

Recall that the formula for the variance (ddof=1) is equal to:

The data is squared so positive and negative points don't cancel each other out. For instance the points 1,2,3 have a mean of 2. 1 is 1 unit below the mean and 3 is 1 unit above the mean. Ignoring the denominator and looking at the numerator, summing -1, 0 and 1 would give 0 stating no variance which is incorrect as the value of 1 and 3 each vary by 2 by 1 unit but they vary in opposite directions. For this reason we tend to square the numerator so we always have a metric to describe the variance with zero variance only occurring when all data points are identical to the mean. As a consequence of this squaring we have lost both direction but more importantly units. The units thus do not relate to the mean, median or mode on linear terms but are rather on square terms with respect to these values. As a result, the square root of the variance is commonly used alongside the mean, median or mode which has the same dimensionality. This is called the standard deviation. In Python the standard deviation function has a very similar form to the variance and the same input arguments. The standard deviation with a Delta Degrees of Freedom of 1 has the form:

[1 3 5]

3.0

4.0

2.0

### Function std – Find the Standard Deviation of all Elements in a Matrix or across columns or rows

2.7386127875258306

[3. 3. 3.]

[1. 1. 1.]

## Function cumsum

### Function cumsum – Find the Cumulative Sum of all Elements in a Vector

The cumulative sum of a vector takes the form:

[1 2 3]

[1 3 6]

### Function cumsum – Find the Cumulative Sum of all Elements in a Matrix or across columns or rows

[[1 2 3]

[4 5 6]

[7 8 9]]

[1 2 3 4 5 6 7 8 9]

[ 1 3 6 10 15 21 28 36 45]

[[1 2 3]

[4 5 6]

[7 8 9]]

[[ 1 2 3]

[ 5 7 9]

[12 15 18]]

[[1 2 3]

[4 5 6]

[7 8 9]]

[[ 1 3 6]

[ 4 9 15]

[ 7 15 24]]

## Function cumprod

### Function cumprod – Find the Cumulative Product of all Elements in a Vector

The cumulative product of a vector takes the form:

[1 2 3]

[1 2 6]

### Function cumprod – Find the Cumulative Product of all Elements in a Matrix or across columns or rows

[[1 2 3]

[4 5 6]

[7 8 9]]

[1 2 3 4 5 6 7 8 9]

[ 1 2 6 24 120 720 5040 40320 362880]

[[1 2 3]

[4 5 6]

[7 8 9]]

[[ 1 2 3]

[ 4 10 18]

[ 28 80 162]]

[[1 2 3]

[4 5 6]

[7 8 9]]

[[ 1 2 6]

[ 4 20 120]

[ 7 56 504]]

## Function diff

### Function diff – Find the Difference of all Elements in a Vector

The diff of a vector takes the following form. Note because we are taking the difference between two points for each element in our new vector, we have one point less than we started:

[1 2 3]

[1 1]

### Function diff – Find the Difference of all Elements in a Matrix or across columns or rows

[1 2 3 4 5 6 7 8 9]

[1 1 1 1 1 1 1 1]

[[1 2 3]

[4 5 6]

[7 8 9]]

[[3 3 3]

[3 3 3]]

[[1 2 3]

[4 5 6]

[7 8 9]]

[[1 1]

[1 1]

[1 1]]