Streamlit.io is an open-source project which provides an interactive framework for Data Science experimentations. We have already covered real-time object detection and building a COVID-19 dashboard using the Streamlit API in our previous articles. In this article, we are going to use Streamlit for another use case called time series forecasting.
Before proceeding, refer to this post if you are unaware of the Streamlit framework.
Here, we have used the Anaconda prompt and Python version 3.8.5 for the implementation. Facebook’s Prophet open-source library has been used for forecasting (Python API for Prophet). The code allows the user to upload custom time-series data and visualise the Prophet’s forecast in Streamlit app on a web browser. Stepwise explanation of the code is as follows:
- Import required libraries and classes.
#Import streamlit import streamlit as st #Import NumPy and Pandas for data manipulation import pandas as pd import numpy as np from fbprophet import Prophet from fbprophet.diagnostics import performance_metrics from fbprophet.diagnostics import cross_validation from fbprophet.plot import plot_cross_validation_metric #for encoding binary data to printable ASCII characters and decoding it #back to binary form import base64
- Define the title to be displayed on the top of UI using streamlit.title()
st.title('Time Series Forecasting Using Streamlit')
- Text to be displayed below the title regarding instructions to import the data. (Refer to this page to know the kind of input data frame expected by Prophet)
streamlit.write() is used to write arguments to the app
st.write("IMPORT DATA") st.write("Import the time series CSV file. It should have two columns labelled as 'ds' and 'y'. The 'ds' column should be of DateTime format by Pandas. The 'y' column must be numeric representing the measurement to be forecasted.")
The title and the text will appear in the UI as follows:
- Insert a file uploader widget using streamlit.file_uploader() so that the user can browse and upload .csv data file from his local machine. Store the uploaded data into a data frame called ‘data.’
data = st.file_uploader('Upload here',type='csv')
The file uploader widget will look something like this:
- The .csv file that we have uploaded for the demo can be found here.
if data is not None: appdata = pd.read_csv(data) #read the data fro appdata['ds'] = pd.to_datetime(appdata['ds'],errors='coerce') st.write(data) #display the data max_date = appdata['ds'].max() #compute latest date in the data
Some initial records of the uploaded file will appear below the widget as follows:
- Choose the forecast horizon
st.write("SELECT FORECAST PERIOD") #text displayed
Insert a numeric input widget using streamlit.number_input() so that the user can select the number of days for which he wants to forecast the future.
periods_input = st.number_input('How many days forecast do you want?', min_value = 1, max_value = 365) #The minimum number of days a user can select is one, while the maximum is #365 (yearly forecast)
The section for horizon selection containing the numeric input widget will appear in UI as follows:
- Fit the time series data for making forecast using Facebook Prophet
if data is not None: obj = Prophet() #Instantiate Prophet object obj.fit(appdata) #fit the data
- Visualize the forecasted data
#text to be displayed st.write("VISUALIZE FORECASTED DATA") st.write("The following plot shows future predicted values. 'yhat' is the predicted value; upper and lower limits are 80% confidence intervals by default") if data is not None: future = obj.make_future_dataframe(periods=periods_input) #Prophet.make_future_dataframe() takes the Prophet model object and #extends the time series dataframe for specified period for which user needs #the forecast fcst = obj.predict(future) #make prediction for the extended data forecast = fcst[['ds', 'yhat', 'yhat_lower', 'yhat_upper']] #The predict() method assigns each row in the ‘future’ dataframe a predicted #value denoted as yhat #Choose only the forecasted records (having date after the latest date in #original data) forecast_filtered = forecast[forecast['ds'] > max_date] st.write(forecast_filtered) #Display some forecasted records st.write(“The next visual shows the actual (black dots) and predicted (blue line) values over time.”) figure1 = obj.plot(fcst) #plot the actual and predicted values st.write(figure1) #display the plot #Plot the trends using Prophet.plot_components() st.write("The following plots show a high level trend of predicted values, day of week trends and yearly trends (if dataset contains multiple years’ data).Blue shaded area represents upper and lower confidence intervals.") figure2 = obj.plot_components(fcst) st.write(figure2)
How to run the code?
- In your terminal, install fbprophet Python library
pip install fbprophet
IMP NOTE: If you get issues in installing fbprophet using pip command in Anaconda prompt, install it using conda as follows:
conda install gcc conda install -c conda-forge fbprophet
- Install streamlit as follows:
pip install --upgrade streamlit
- Suppose you have saved the code file by the name my_app.py
Then, run the following command on the terminal, open the Streamlit app in your browser and run your application.
streamlit run FILEPATH
where FILEPATH is the path to the location where you have stored the my_app.py file.
Output:
When the app gets launched, the UI appears as follows:
On uploading the .csv file, suppose we select 100 as the number of days for which forecast should be made. Some of the forecasted records will appear on the UI as follows:
Plot showing actual values (black dots) and predicted values (blue):
Plots showing forecasted future trend:
- Code source
- .py code file for the above implementation
References
Refer to the following sources to know about Streamlit:
- Official website
- Documentation
- Related articles (article1, article2, article3)