@@ -0,0 +1,1542 @@
+import numpy as np%0Aimport pandas as pd%0Afrom scipy import interpolate, poly1d%0Aimport matplotlib.pyplot as plt%0A %0A   %0Adef main():%0A    df = pd.read_csv('greenland-mass-change.csv')%0A    all_xs = df%5B'year'%5D%0A    all_ys = df%5B'mass change'%5D%0A   %0A    train = df%5Bdf%5B'year'%5D %3C 2012%5D%0A    test  = df%5Bdf%5B'year'%5D %3E 2012%5D%0A    train_xs = train%5B'year'%5D%0A    train_ys = train%5B'mass change'%5D%0A    test_xs  = test%5B'year'%5D%0A %0A    spline = interpolate.UnivariateSpline(all_xs, all_ys)%0A %0A    # Found 3 experimentally. See climate_change_analysis_polynomial_fit.py%0A    coefficients = np.polyfit(train_xs, train_ys, 3)%0A    trend_fn = poly1d(coefficients)%0A %0A    earliest = min(train%5B'year'%5D)%0A    latest = max(train%5B'year'%5D)%0A %0A    seasonal_fluctuations = %5B%5D%0A    for n, t in enumerate(test_xs):%0A        start = (t - int(t)) + int(earliest)%0A        if start %3C earliest:%0A            start = start + 1%0A        end = (t - int(t)) + int(latest)%0A        if end %3E latest:%0A            end = end - 1%0A        steps = %5B(s + start) for s in range(int(end - start))%5D%0A        spline_samples = %5Bspline(s) for s in steps%5D%0A        relative_trend = trend_fn(steps)%0A        samples = %5Bs - r for s, r in zip(spline_samples, relative_trend)%5D%0A           %0A        seasonal_fluctuations.append((samples, steps))%0A %0A    for n, (f, s) in enumerate(seasonal_fluctuations):%0A        plt.plot(s, f)%0A   %0A    plt.title('Greenland mass change')%0A    plt.xlabel('Time')%0A    plt.ylabel('Mass')%0A    plt.grid(True)%0A    plt.show()%0A    plt.savefig('greenland-mass-change.png')%0A %0Aif __name__ == '__main__':%0A    main()%0A

Determine whether linear or polynomial fit is best for seasonal change