import numpy as np
Load in the data for the exercise:
with np.load("weather_data.npz") as weather:
data = weather["rain_history"]
Plot the data to see what range of values we're dealing with:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data)
The mean should be around 50 or 60:
np.mean(data)
And the standard deviation looks to be about 10:
np.std(data)
We can write a function which calculates based on an array:
def variation(x):
return np.std(x) / np.mean(x)
And call it like any other function:
variation(data)
Because we're using the NumPy functions, it also works on Python lists of numbers:
variation([50, 60, 51, 49, 53])
As well as single values:
variation(7)