COVID19 California Data + Facebook Prophet

In [1]:
import pandas as pd
from fbprophet import Prophet 
df = pd.read_csv('https://raw.githubusercontent.com/danielcaraway/data/master/ca_train.csv')
df.head()
Out[1]:
Id Province/State Country/Region Lat Long Date ConfirmedCases Fatalities
0 1 California US 36.1162 -119.6816 2020-01-22 0.0 0.0
1 2 California US 36.1162 -119.6816 2020-01-23 0.0 0.0
2 3 California US 36.1162 -119.6816 2020-01-24 0.0 0.0
3 4 California US 36.1162 -119.6816 2020-01-25 0.0 0.0
4 5 California US 36.1162 -119.6816 2020-01-26 0.0 0.0

STEP 1: Split into DS & Y

We're starting with ConfirmedCases as Y

In [0]:
sm = df[['Date', 'ConfirmedCases']]
sm.columns = ['ds','y']
In [7]:
m = Prophet()
m.fit(sm)
INFO:numexpr.utils:NumExpr defaulting to 2 threads.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Out[7]:
<fbprophet.forecaster.Prophet at 0x7f88cdc23d30>
In [16]:
future = m.make_future_dataframe(periods=365)
future.tail()
Out[16]:
ds
417 2021-03-14
418 2021-03-15
419 2021-03-16
420 2021-03-17
421 2021-03-18
In [17]:
forecast = m.predict(future)
fig1 = m.plot(forecast)

* NOTE: Wow that is ridiculously unhelpful. Clearly 365 was too much. Turning it into a function for more rapid testing

In [0]:
def get_prof_preds_for(df, n):
  m = Prophet()
  m.fit(df)
  future = m.make_future_dataframe(periods=n)
  forecast = m.predict(future)
  fig1 = m.plot(forecast)
In [19]:
get_prof_preds_for(sm, 60)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
In [20]:
get_prof_preds_for(sm, 30)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
In [21]:
get_prof_preds_for(sm, 10)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.

Trimming the training data

In [50]:
sm
Out[50]:
ds y
0 2020-01-22 0.0
1 2020-01-23 0.0
2 2020-01-24 0.0
3 2020-01-25 0.0
4 2020-01-26 0.0
5 2020-01-27 0.0
6 2020-01-28 0.0
7 2020-01-29 0.0
8 2020-01-30 0.0
9 2020-01-31 0.0
10 2020-02-01 0.0
11 2020-02-02 0.0
12 2020-02-03 0.0
13 2020-02-04 0.0
14 2020-02-05 0.0
15 2020-02-06 0.0
16 2020-02-07 0.0
17 2020-02-08 0.0
18 2020-02-09 0.0
19 2020-02-10 0.0
20 2020-02-11 0.0
21 2020-02-12 0.0
22 2020-02-13 0.0
23 2020-02-14 0.0
24 2020-02-15 0.0
25 2020-02-16 0.0
26 2020-02-17 0.0
27 2020-02-18 0.0
28 2020-02-19 0.0
29 2020-02-20 0.0
30 2020-02-21 0.0
31 2020-02-22 0.0
32 2020-02-23 0.0
33 2020-02-24 0.0
34 2020-02-25 0.0
35 2020-02-26 0.0
36 2020-02-27 0.0
37 2020-02-28 0.0
38 2020-02-29 0.0
39 2020-03-01 0.0
40 2020-03-02 0.0
41 2020-03-03 0.0
42 2020-03-04 0.0
43 2020-03-05 0.0
44 2020-03-06 0.0
45 2020-03-07 0.0
46 2020-03-08 0.0
47 2020-03-09 0.0
48 2020-03-10 144.0
49 2020-03-11 177.0
50 2020-03-12 221.0
51 2020-03-13 282.0
52 2020-03-14 340.0
53 2020-03-15 426.0
54 2020-03-16 557.0
55 2020-03-17 698.0
56 2020-03-18 751.0
In [25]:
get_prof_preds_for(sm[20:],10)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
In [0]:
def get_prof_preds_for(df, n):
  m = Prophet(daily_seasonality=True)
  m.fit(df)
  future = m.make_future_dataframe(periods=n)
  forecast = m.predict(future)
  fig1 = m.plot(forecast)
In [30]:
get_prof_preds_for(sm[20:],10)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
In [33]:
get_prof_preds_for(sm[40:],10)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:n_changepoints greater than number of observations. Using 12.

OK looks like we are getting somewhere

In [35]:
get_prof_preds_for(sm[40:],60)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:n_changepoints greater than number of observations. Using 12.
In [39]:
get_prof_preds_for(sm[47:],30]
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:n_changepoints greater than number of observations. Using 7.
In [44]:
get_prof_preds_for(sm[47:],20)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:n_changepoints greater than number of observations. Using 7.

STEP 1B: Try with Fatalities

In [0]:
sm = df[['Date', 'Fatalities']]
sm.columns = ['ds','y']
In [46]:
get_prof_preds_for(sm, 20)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
In [47]:
sm
Out[47]:
ds y
0 2020-01-22 0.0
1 2020-01-23 0.0
2 2020-01-24 0.0
3 2020-01-25 0.0
4 2020-01-26 0.0
5 2020-01-27 0.0
6 2020-01-28 0.0
7 2020-01-29 0.0
8 2020-01-30 0.0
9 2020-01-31 0.0
10 2020-02-01 0.0
11 2020-02-02 0.0
12 2020-02-03 0.0
13 2020-02-04 0.0
14 2020-02-05 0.0
15 2020-02-06 0.0
16 2020-02-07 0.0
17 2020-02-08 0.0
18 2020-02-09 0.0
19 2020-02-10 0.0
20 2020-02-11 0.0
21 2020-02-12 0.0
22 2020-02-13 0.0
23 2020-02-14 0.0
24 2020-02-15 0.0
25 2020-02-16 0.0
26 2020-02-17 0.0
27 2020-02-18 0.0
28 2020-02-19 0.0
29 2020-02-20 0.0
30 2020-02-21 0.0
31 2020-02-22 0.0
32 2020-02-23 0.0
33 2020-02-24 0.0
34 2020-02-25 0.0
35 2020-02-26 0.0
36 2020-02-27 0.0
37 2020-02-28 0.0
38 2020-02-29 0.0
39 2020-03-01 0.0
40 2020-03-02 0.0
41 2020-03-03 0.0
42 2020-03-04 0.0
43 2020-03-05 0.0
44 2020-03-06 0.0
45 2020-03-07 0.0
46 2020-03-08 0.0
47 2020-03-09 0.0
48 2020-03-10 2.0
49 2020-03-11 3.0
50 2020-03-12 4.0
51 2020-03-13 4.0
52 2020-03-14 5.0
53 2020-03-15 6.0
54 2020-03-16 7.0
55 2020-03-17 12.0
56 2020-03-18 13.0
In [48]:
get_prof_preds_for(sm[47:], 20)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:n_changepoints greater than number of observations. Using 7.
In [0]: