-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprophet_forecasting.py
324 lines (217 loc) · 8.41 KB
/
prophet_forecasting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
"""Prophet_Forecasting.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Je3Ka2PGLBnb5d0cJ5lbwLG9oDsDXw9R
---
## Using FB Prophet
Project Goals
- Find unusual patterns in Google search traffic
- Mine search traffic data for seasonality
- Relate search traffic to stock price patterns
- Create time series model with Prophet
- Forecast revenue by using time series models
"""
# Install the required libraries
!pip install pystan
!pip install pystan~=2.14
!pip install fbprophet
!pip install hvplot
!pip install holoviews
import seaborn as sn
# Commented out IPython magic to ensure Python compatibility.
# Import the required libraries and dependencies
import pandas as pd
from fbprophet import Prophet
import holoviews as hv
import hvplot.pandas
import datetime as dt
# %matplotlib inline
"""---
Find Unusual Patterns in Google Search Traffic (hourly)
"""
# Upload "google_hourly_search_trends.csv", then store in a DF
from google.colab import files
uploaded = files.upload()
# Set "Date" as the Index.
df_trends = pd.read_csv(
"google_hourly_search_trends.csv",
index_col='Date',
parse_dates=True,
infer_datetime_format=True,
).dropna()
# Review the DF
display(df_trends.head())
display(df_trends.tail())
# Review data types
df_trends.info()
# Holoviews extension
hv.extension('bokeh')
# Slice DF for just May 2020
df_may_2020 = df_trends["2020-05-01":"2020-05-31"]
# Plot
df_may_2020.hvplot()
# Calculate sum of the total search traffic for May 2020
traffic_may_2020 = df_may_2020["Search Trends"].sum()
# View the traffic_may_2020 value
traffic_may_2020
# Calcluate monhtly median search traffic
median_monthly_traffic = df_trends["Search Trends"].groupby(by=[df_trends.index.year, df_trends.index.month]).sum().median()
# View the median_monthly_traffic value
median_monthly_traffic
# Compare seach traffic in May 2020 to overall monthly median
print(f"May 2020's Median Search Trafic was {traffic_may_2020}")
print(f"The Average Monthly Median Search Trafic was {median_monthly_traffic}")
print(f"May 2020's median monthly value was greater by {traffic_may_2020 - median_monthly_traffic}")
# Holoviews extension
hv.extension('bokeh')
# Group hourly search data to plot avg traffic by day of week
daily_trends = df_trends["Search Trends"].groupby(by=[df_trends.index.dayofweek]).mean()
daily_trends.hvplot()
# Holoviews extension
hv.extension('bokeh')
# Use heatmap for day of week search traffic.
df_trends.hvplot.heatmap(y = 'Date.dayofweek', x='Date.hour', ylabel = 'Day of Week', xlabel = 'Hour of day', C='Search Trends', title = 'Heatmap of Daily / Hourly Search Traffic')
# Holoviews extension
hv.extension('bokeh')
# Group hourly search data for avg traffic by the week of year
weekly_traffic = df_trends["Search Trends"].groupby(by=[df_trends.index.weekofyear]).mean()
weekly_traffic.plot()
# Upload the stock price file and store in DF
from google.colab import files
uploaded = files.upload()
# Set the "date" column as the Datetime Index.
df_stock = pd.read_csv(
"stock_price.csv",
index_col='date',
parse_dates=True,
infer_datetime_format=True,
).dropna()
# View the first and last five rows of the DataFrame
display(df_stock.head())
display(df_stock.tail())
# Holoviews extension
hv.extension('bokeh')
# Visualize Closing price data
df_stock["close"].hvplot()
# Concatenate stock DataFrame trends DataFrame
stock_trends = pd.concat(([df_stock, df_trends]), axis = 1).dropna()
# View the first and last five rows of the DataFrame
display(stock_trends.head())
display(stock_trends.tail())
# For combined DF, slice for the first half of 2020 (2020-01 through 2020-06)
first_half_2020 = stock_trends["2020-01-01":"2020-06-30"]
# View the first and last five rows of first_half_2020 DataFrame
display(first_half_2020.head())
display(first_half_2020.tail())
# Holoviews extension
hv.extension('bokeh')
# Visualize the close and Search Trends data
first_half_2020.hvplot(shared_axes=False, subplots=True).cols(1)
# Create new column that shifts the Search Trends information by one hour
stock_trends['Lagged Search Trends'] = stock_trends['Search Trends'].shift(1)
stock_trends.head()
# Create new column that calculates the standard deviation of the closing stock price return data over a 4 period rolling window
stock_trends['Stock Volatility'] = stock_trends["close"].rolling(4).std()
stock_trends
# Holoviews extension
hv.extension('bokeh')
# Use hvPlot to visualize stock volatility
stock_trends['Stock Volatility'].hvplot()
# Create new column to calculate hourly return percentage of the closing price
stock_trends['Hourly Stock Return'] = stock_trends["close"].pct_change()
# View the first and last five rows of the mercado_stock_trends_df DataFrame
display(stock_trends.head(5))
display(stock_trends.tail(5))
# Construct correlation table of Stock Volatility, Lagged Search Trends, and Hourly Stock Return
corrMatrix = stock_trends[["Lagged Search Trends", "Stock Volatility", "Hourly Stock Return"]].corr()
sn.heatmap(corrMatrix, annot=True)
# Reset the index
prophet_df = df_trends.reset_index()
# Label the columns ds and y
prophet_df.columns = ['ds', 'y']
# Drop an NaN values
prophet_df = prophet_df.dropna()
# View the first and last five rows
display(prophet_df.head(5))
display(prophet_df.tail(5))
# Call Prophet function, store as an object
model_trends = Prophet()
# Fit time-series model.
model_trends.fit(prophet_df)
# Create future DF to hold predictions
future_trends = model_trends.make_future_dataframe(periods=2000, freq='H')
# View the last five rows
display(future_trends.tail(5))
# Make predictions for the trend data
forecast_trends = model_trends.predict(future_trends)
# Display the first five rows
display(forecast_trends.head(5))
# Plot Prophet predictions
model_trends.plot(forecast_trends)
# Set index to the ds datetime column
forecast_trends = forecast_trends.set_index("ds")
# View yhat,yhat_lower and yhat_upper columns only
forecast_trends[["yhat", "yhat_lower", "yhat_upper"]].head()
# Holoviews extension
hv.extension('bokeh')
# Visualize
forecast_trends[['yhat', 'yhat_lower', 'yhat_upper']].iloc[-2000:,:].hvplot()
from fbprophet.plot import plot_components
# Reset index in the forecast_trends DataFrame
forecast_trends = forecast_trends.reset_index()
# Visualize the forecast results
figures_trends = model_trends.plot_components(forecast_trends)
# Upload "daily_revenue.csv" and store in DF
from google.colab import files
uploaded = files.upload()
# Set "date" column as DatetimeIndex
df_sales = pd.read_csv(
"daily_revenue.csv",
index_col='date',
parse_dates=True,
infer_datetime_format=True,
).dropna()
# Review DF
df_sales.head()
# Holoviews extension
hv.extension('bokeh')
# Visualize daily sales
df_sales["Daily Sales"].hvplot()
# Apply FB Prophet model.
# Reset index so date becomes column
sales_prophet_df = df_sales.reset_index()
# Adjust columns names
sales_prophet_df.columns = ['ds', 'y']
# Visualize DF
sales_prophet_df.head()
# Create model
sales_prophet_model = Prophet()
# Fit model
sales_prophet_model.fit(sales_prophet_df)
# Predict sales for 1 quarter into future.
sales_prophet_future = sales_prophet_model.make_future_dataframe(periods=3400, freq='H')
# Display the last five rows
display(sales_prophet_future.tail(5))
# Make predictions sales each day over the next quarter
sales_prophet_forecast = sales_prophet_model.predict(sales_prophet_future)
# Display the first 5 rows
display(sales_prophet_forecast.head(5))
# Analyze with plot_components function
sales_prophet_components = sales_prophet_model.plot_components(sales_prophet_forecast)
# Holoviews extension
hv.extension('bokeh')
# Plot the predictions for Sales
sales_prophet_forecast[['yhat', 'yhat_lower', 'yhat_upper']].hvplot()
# Set the ds column as Index
sales_prophet_forecast = sales_prophet_forecast.set_index("ds")
# Display the first and last five rows
display(sales_prophet_forecast.head(5))
display(sales_prophet_forecast.tail(5))
# Create sales forecast for next quarter
sales_forecast_quarter = sales_prophet_forecast[['yhat', 'yhat_lower', 'yhat_upper']].loc['2020-07-01':'2020-09-30']
sales_forecast_quarter = sales_forecast_quarter.rename(columns={"yhat": "most likely", "yhat_lower": "worst case", "yhat_upper": "best case"})
# Review the last five rows of the DataFrame
sales_forecast_quarter.tail(5)
# Display summed values for all rows in DF
sales_forecast_quarter.sum()