-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathapp.py
93 lines (70 loc) · 3.49 KB
/
app.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
import streamlit as st
import pandas as pd
import numpy as np
from prophet import Prophet
from prophet.diagnostics import performance_metrics
from prophet.diagnostics import cross_validation
from prophet.plot import plot_cross_validation_metric
import base64
st.title('📈 Automated Time Series Forecasting')
"""
This data app uses Facebook's open-source Prophet library to automatically generate future forecast values from an imported dataset.
You'll be able to import your data from a CSV file, visualize trends and features, analyze forecast performance, and finally download the created forecast 😵
**In beta mode**
Created by Zach Renwick: https://twitter.com/zachrenwick
Code available here: https://github.com/zachrenwick/streamlit_forecasting_app
"""
"""
### Step 1: Import Data
"""
df = st.file_uploader('Import the time series csv file here. Columns must be labeled ds and y. The input to Prophet is always a dataframe with two columns: ds and y. The ds (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp. The y column must be numeric, and represents the measurement we wish to forecast.', type='csv')
st.info(
f"""
👆 Upload a .csv file first. Sample to try: [peyton_manning_wiki_ts.csv](https://raw.githubusercontent.com/zachrenwick/streamlit_forecasting_app/master/example_data/example_wp_log_peyton_manning.csv)
"""
)
if df is not None:
data = pd.read_csv(df)
data['ds'] = pd.to_datetime(data['ds'],errors='coerce')
st.write(data)
max_date = data['ds'].max()
#st.write(max_date)
"""
### Step 2: Select Forecast Horizon
Keep in mind that forecasts become less accurate with larger forecast horizons.
"""
periods_input = st.number_input('How many periods would you like to forecast into the future?',
min_value = 1, max_value = 365)
if df is not None:
m = Prophet()
m.fit(data)
"""
### Step 3: Visualize Forecast Data
The below visual shows future predicted values. "yhat" is the predicted value, and the upper and lower limits are (by default) 80% confidence intervals.
"""
if df is not None:
future = m.make_future_dataframe(periods=periods_input)
forecast = m.predict(future)
fcst = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']]
fcst_filtered = fcst[fcst['ds'] > max_date]
st.write(fcst_filtered)
"""
The next visual shows the actual (black dots) and predicted (blue line) values over time.
"""
fig1 = m.plot(forecast)
st.write(fig1)
"""
The next few visuals show a high level trend of predicted values, day of week trends, and yearly trends (if dataset covers multiple years). The blue shaded area represents upper and lower confidence intervals.
"""
fig2 = m.plot_components(forecast)
st.write(fig2)
"""
### Step 4: Download the Forecast Data
The below link allows you to download the newly created forecast to your computer for further analysis and use.
"""
if df is not None:
csv_exp = fcst_filtered.to_csv(index=False)
# When no file name is given, pandas returns the CSV as a string, nice.
b64 = base64.b64encode(csv_exp.encode()).decode() # some strings <-> bytes conversions necessary here
href = f'<a href="data:file/csv;base64,{b64}">Download CSV File</a> (right-click and save as ** <forecast_name>.csv**)'
st.markdown(href, unsafe_allow_html=True)