# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import numpy as np

from sklearn.metrics import mean_absolute_error as mea

from fbprophet import Prophet

import matplotlib.pyplot as plt
# %matplotlib inline

# +
path = 'https://raw.githubusercontent.com/dataworkshop/5dwchallenge_2019/master/challenge5/input/mauna_loa.csv'
df = pd.read_csv(path)

df.head()

# +
df['date'] = pd.to_datetime(df[['year', 'month', 'day']])

df['ppm_fixed'] = df['ppm'].map(lambda x: np.nan if x<0 else x)
df['ppm_fixed'].fillna(method='backfill', inplace=True)
plt.plot(df.date, df.ppm_fixed);

# +
cut_year = 2008

train = df[df.year < cut_year]
test = df[ df.year >= cut_year ]

plt.plot(train.date, train.ppm_fixed, label='train');
plt.plot(test.date, test.ppm_fixed, label = 'test');
# -

# # Prophet

# +
fb_df = train[ ['date', 'ppm_fixed']].copy()
fb_df.columns = ['ds', 'y']

fb_df.head()

# +
m = Prophet()

m.fit(fb_df)
# -

future = m.make_future_dataframe(periods=len(test), freq='W', include_history=False)
future.tail()

test.tail()

forecast = m.predict(future)
forecast.head()

# +
train = df[df.year < cut_year]
test = df[ df.year >= cut_year ]

#plt.plot(train.date, train.ppm_fixed, label='train');
plt.plot(test.date, test.ppm_fixed, label = 'test');
plt.plot(test.date, forecast.yhat, label = 'forecast');
plt.legend();

# -

m.plot(forecast);



StarcoderdataJupyterScriptsDedupFiltered