Exercise for the course Python for MATLAB users, by Olivier Verdier

This exercise comes from the scipy lecture notes.

In [2]:
import numpy as np
import matplotlib.pyplot as plt
In [3]:
%matplotlib inline

The population of Hares, Lynxes and carrots in Canada is in the file population.txt. Download that file and place it in this directory.

Once this is done, execute the code below to load the data in the variable data.

Important remark: you should not use any for loop in this exercise! :-)

In [4]:
data = np.loadtxt('populations.txt')
In [3]:
year, hares, lynxes, carrots = data.T  # trick: columns to variables
In [53]:
year, hares, lynxes, carrots = data.T
populations = data[:,1:]
In [54]:
data
Out[54]:
array([[  1900.,  30000.,   4000.,  48300.],
       [  1901.,  47200.,   6100.,  48200.],
       [  1902.,  70200.,   9800.,  41500.],
       [  1903.,  77400.,  35200.,  38200.],
       [  1904.,  36300.,  59400.,  40600.],
       [  1905.,  20600.,  41700.,  39800.],
       [  1906.,  18100.,  19000.,  38600.],
       [  1907.,  21400.,  13000.,  42300.],
       [  1908.,  22000.,   8300.,  44500.],
       [  1909.,  25400.,   9100.,  42100.],
       [  1910.,  27100.,   7400.,  46000.],
       [  1911.,  40300.,   8000.,  46800.],
       [  1912.,  57000.,  12300.,  43800.],
       [  1913.,  76600.,  19500.,  40900.],
       [  1914.,  52300.,  45700.,  39400.],
       [  1915.,  19500.,  51100.,  39000.],
       [  1916.,  11200.,  29700.,  36700.],
       [  1917.,   7600.,  15800.,  41800.],
       [  1918.,  14600.,   9700.,  43300.],
       [  1919.,  16200.,  10100.,  41300.],
       [  1920.,  24700.,   8600.,  47300.]])

Plot the data (year vs each population item), using label and legend.

Compute the mean (np.mean?) and standard deviation (np.std?) of each piece of data

Which year each species had the largest population? (np.argmax? and use fancy indexing)

Which year any of the population is over 50000?

The top two years for each species when they had the lowest population

You may use argsort, which gives the indices for which the array value would be sorted.

In [15]:
np.argsort?

Compare (plot) the change in hare population (see np.gradient?) and the number of lynxes.