1. Numpy axis#
import numpy as np
import matplotlib.pyplot as plt
Often when we have a long sequence of data we would like to evaluate specific sub-sets of it. For example, if we have 10 years worth of hourly measurements, but would like to evaluate the monthly or weekly characteristics. Often we can store our data in a structured way and then use some indexing capabilities of Numpy to evaluate it in a smart way. Check out the following simple tips, then try and apply it in practice.
\(\text{Task 1.1:}\)
Read and run the cells below, making sure you understand what is happening (i.e., completing evaluations on the rows and columns of the matrix).
First, let’s start by collecting a “long” sequence of data.
A = np.array([1, 20, 300, 1, 2, 3])
print(A)
It is easy to restructure it into a matrix form; in this case, 2 rows and 3 columns.
B = np.reshape(A, (2, 3))
print(B)
In Numpy, “axes” are used to specify the structure of an array using the axis keyword argument. For this assignment, we are particularly interested in performing operations along the 0th and 1st axes of the array, which correspond to the columns and rows, respectively. Check it out:
B.mean(axis=0)
Looking along the other axis:
B.mean(axis=1)
And you can do it for other methods too!
B.std(axis=1)
\(\text{Task 1.2:}\)
Read the simple story below and use the tips described above to complete the partially completed code cell.
Suppose you and a group of friends would like to evaluate your financial decisions, and you decide to review how many coins you spend at PSOR, the CEG student pub, to practice your Python skills. You have assembled data on the number of coins purchased per month, for several years, starting from when you first met in September, through August, 3 years later.
coins = [46, 28, 16, 27,
22, 24, 31, 12,
32, 36, 12, 0,
41, 27, 21, 26,
21, 19, 18, 35,
14, 34, 8, 0,
53, 34, 23, 35,
28, 26, 18, 13,
12, 14, 34, 0]
coins_matrix = np.reshape(coins, (3, 12))
print(coins_matrix)
np.set_printoptions(precision=1)
average_monthly_coins =
average_monthly_coins_per_year =
average_coins_september =
average_coins_january =
max_coins_month =
max_coins_year =
print(f'The average number of coins spent per month is: {average_monthly_coins:.1f}')
print('The average number of coins spent per month for each year is:', average_monthly_coins_per_year)
print(f'The average number of coins spent each september: {average_coins_september:.1f}')
print(f'The average number of coins spent each january: {average_coins_january:.1f}')
print(f'Max coins spent in any month: {max_coins_month:.1f}')
print(f'Max coins spent in any year: {max_coins_year:.1f}')
By Robert Lanzafame, Delft University of Technology. CC BY 4.0, more info on the Credits page of Workbook.