Forecasting with the FB Prophet Model
Key Takeaways
This video teaches time series forecasting using the FB Prophet model in Python
Full Transcript
in this video we're going to do some forecasting using Facebook's model called profit this model is designed to be super easy to use and very intuitive based on an additive model which basically means that it breaks down the forecast into different components and you'll see how this forecast breaks down into yearly weekly and even hourly Trends in a previous video I did show how you can use XG boost to do forecasting but there are some limitations of doing forecasting that way mainly you can't forecast anything that's outside of the historic values but you'll see with profit that is possible because it uses this additive type of model everything I'm doing is in a kaggle notebook which I'll link down below you can copy and explore the data on your own and don't forget to like And subscribe that helps me out a lot alright let's go okay so here we are in the notebook where I'm going to be doing time series forecasting using profit but before we get too far I do want to talk about some types of Time series series data that you might be working with so of course we're assuming that our data has some sort of trend inherently in it that our model is going to pick up on and those types of Trends can be very different depending on your time series data we can have seasonal patterns we can have increasing Trends either linear or quadratic or any other type of function and then we can also have a combination of either of these and you're going to see exactly how the profit model breaks these things down when we train it on our data set speaking of which the data that I have loaded here in this notebook is hourly energy consumption which I've used in my previous tutorials and it has over 10 years of historic data that we can train and evaluate our model on okay first thing we need to do is do some imports we've imported numpy pandas Seaborn for visualization and matplotlib and then here's the import that we care most about for this tutorial which is Facebook's profit model we just import The Profit model object from FB profit if you want to run this locally then you'll first have to pip install Facebook profit before you can import this we're also going to pull in some error metrics that we can use to evaluate our model and I've actually defined a function down here for a different type of error metric called mean absolute percent error nice thing about this metric is it gives more of an easy to understand metric that we can use to evaluate because we're looking at the average percent off that our prediction is from the ground truth as I said before we have hourly power consumption from pjm this energy consumption has some unique characteristics and we're just going to read in using pandas read CSV we're going to give it the index column as 0 and we're also going to have it parse the date column of zero and now if I run this you can see that we now have an index of our date time column and we have this PGM East megawatts which is the value we're going to be trying to predict let's also plot this just so we can see what it looks like so I'm going to make a color palette first just pull this from Seaborn and then I'm going to take this data frame and I'm going to plot with the Fig size let's make these markers smaller so it's easier to see and go ahead and plot that so here we can see this data is going from 2002 all the way to 2018 and you can see already that it has some Trends in the data and as we Dig Down Deeper we're going to see it even has it at the hourly level some trends that we can pick up on so in order to visualize those before we get into modeling let's make some time series features now these time series features we don't actually need for the profit model but we're going to use them just to visualize what the data looks like so I've already written this create features function because it's not needed for the model I'm not going to go into too much detail about it but what it really does is it takes this data frame that has an index of a Time series and it pulls out different features about the date time index so the date the hour the day of the week all become their own variables or features about that row I've also added some things here like cutting into spring summer fall and winter let's go ahead and run this and we're going to concatenate the output as features in Target and that basically is the same data that we had before but instead of just the index of a date time and pjm East megawatts as our only output feature we also have all these other features about this date time including the weekday and the season here so now that we have that let's go ahead and plot some of this data so I'm going to actually make a subplot with matplotlib and using a box plot it will be a good way to visualize how some of these features relate to each other so let's make this data equal features in Target our x value is going to be weekday and our y value is going to be this pjm East megawatts we'll color it by the season value that we made and make sure I'm adding it to that and also I'm going to make the line with one so it's easier to read and let's add some other features to this plot before we plot it there we go so this is a nice plot to see that our day of the week feature does seem to play important into our predictions but also the season is very important so we have peaks in the winter and summer and the lower Seasons like fall and spring tend to have less power consumption okay but we want to do some forecasting so in a previous video I talked about very detailed way that we can do cross-validation for time series but here just because we're trying to explore this model I'm just going to do a train test split of our data so that we can train a profit model and see what the predictions look like so what I'm doing here is I'm going to split our pjm East data by January 2015 which cuts off those last three years that we'll use as our test data and then I'm also going to plot it so we can see exactly what the train and tests look like let's also make this marker style smaller now we can see how we've split our data that we'll be using to train our model on so everything here in red will be our training set and then our test set we'll use to evaluate our model it will be in blue all right the next thing we want to do is actually train our profit model but profit does expect the data to be in a certain format so the main thing is that it expects a column with the date time type which currently in our data frame is the index and it needs it to be named DS and then our Target variable which currently is that megawatt value it requires it to be the value y so that's pretty easy for us we can just take this PJ East training data and use Panda's rename to rename the columns into what we would like it to be now if I show you PGM East train profit this has DS and Y as our column names and once we have our data in that format it's pretty easy to actually train the model so remember we imported The Profit model before like this we're just going to create a profit object now I'm going to show you some of the parameters that you could use within this when you're training your own model we're going to keep most of this as the default just to see how it predicts but you can add change points you can change the growth type you can do a lot of things just using your knowledge and imparting that into your modeling to account for your data so we're going to make this our model and then we're going to take our model and we're going to run fit on this pjm East train profit data frame which we know is in the right format I'm also going to add the time command at the top of this which will show you how long it takes for this to run and we'll cut that part out so you don't have to sit here and watch the model train all right our model is done training and you can see it took about four minutes here to train on about 10 years of data so now that we have a trained model we can actually just run predict on it so we could take our model and run predict and predict on a new data frame now we do have to create that test data frame in the same format with the correct column names so I'm going to go ahead and do that by doing this rename and then we will or this needs to be pH and we'll save the result as a new data frame called pjm ease test forecast okay so that's done running and now we could take a look by running a head command on the data frame that our model predicted now you'll see here that this data frame has more than just the output prediction remember this is an additive model so it's actually showing us each of the components of the prediction in our output here so we have the trend additive terms yearly Trends and then all the way here to the right we have y hat which is the predicted value for this test set now that's a lot of features to get a handle of the nice thing is that profits model actually comes in with some built-in plotting functions that we can use to visualize this so let's make another subplot and we'll just take our model and run the plot we'll plot on the forecast value which is this data frame and we added a title to it so now we can see this is our forecasted value going into the future the dark values are the actual forecasted value but then we also have some confidence intervals and this will tell you the upper and lower bounds of what the model thinks is reasonable for a prediction into the future and you can see that always the confidence interval is going to become wider as you try to predict way further out into the future so the nice thing about having confidence intervals is you can say to yourself not only do I think this is the predictive value in the future but this is the predicted range in which values are expected to maybe end up but there's more than just visualizing the predictions that we can do with this model we can actually take the model and we can plot the components of it so if we run this on the forecast so when we're plotting this we're also going to have it show the uncertainty values but you can see that's broken it down here into just the trend value over time which for this data it sees as being mostly flat then you yearly which has some ups and downs that we remember we saw when we plotted using the exploratory data analysis that we have this these two peaks in the winter and in the summer then it's broken it down into the day of the week so weekends having slightly lower values than weekdays and then finally within the day this is the hourly component of the trend that the model picks up on so now you can see how these components combined together actually combine to create our forecast and that's a great thing about profit that makes it easy to understand and easy to explain okay but let's run some evaluation metrics to see how well this model actually did in predicting and we'll compare the forecast to actuals by plotting the actual values alongside our model predicted values so let's go ahead and plot that and we'll see here now in red we have the actual values and in blue we have the forecasted values you can see here that the model may have been picking up on a downward Trend in the historic data data over time and it's applied that into the future it may have been overfitting to that component because as we get further out the predictions are still within the confidence interval but they look like they've gone a little bit lower in value than what we would hope to see and next I'm going to do the same plot just zoomed in here at the first month because it's a little hard to see down to the daily value what our predictions look like so I'm going to take January to February 2015 which is the first month that we were predicting and we're going to look at the details of the predictions so you can see here with the red dots being the truth and the blue dots being our forecast the forecast does seem to overlap most of the times with the ground truth values there are some exceptions to that but early in our predicted values they seem to be on Trend with what the actual values look like and we can get more down into detail by even looking at the first week so I just have from January 1st to January 8th the predicted versus actual and you can see here that the components of within the day are even being picked up by this model and it's fairly closely following the trend line of the actual values but of course we want to evaluate this model with error metrics and I did import three different metric types that we're going to use mean squared error and we'll actually take the square root of the mean squared error mean absolute error and then that mean average percent error which will give us a percent value that's easier to understand when evaluating the model so first I'm going to take this mean squared error value we're going to take it and give it our true value which is the ground truth and then predicted value which is our y hat value the forecasted value and we'll take the square root of this to give us the root mean squared error which is a very common error metric to use and our error metric is 6600 this is actually worse performing than the XG boost model that I've showed in my other video and then just for consistency with that video I'll do mean absolute error mean absolute error which is about five thousand and then our mean absolute percent error which is the percent value that it is off on average and you can see it's about 16 and a half percent off from the actual value when it predicts which isn't that bad if you consider that it's forecasting way out years into the future there's still one thing I haven't covered yet about the profit model that can be really powerful and that's that has built-in ability to account for holidays and with things like power consumption or sales anything you might want to forecast holidays can have a big effect on the forecast so we're going to show how that could be used if you wanted to improve a model like this so I'm just going to add a section here called adding holidays so I'm just going to be lazy here and pull out all the U.S federal holidays from a calendar built into pandas and then we'll load that in as all the holidays in our model so the way we do that is from the pandas T-Series holiday Library we're going to import the U.S federal holiday calendar we're going to create our calendar and then we'll actually make a data frame from the start to the end Days of our pjm East time so you can see these are all the holidays I'm actually going to identify each holiday as being unique so if we go to Holiday names here and do a value counts we could see most holidays that appear 17 times or 16 times in this data set and these are all the main holidays on the U.S federal calendar now I'm going to create another profit model but this time we're actually going to provide it holidays and we have to do some renaming here that's right so it's expecting the data to be in the format of we can reset this index rename the columns index as DS I believe this is the way that the model expects it to be provided there we go so we just need to make sure that we have for the holidays we're provided a DS column with the date and then the holiday name in a holiday column so that was pretty easy and then we'll call this model with holiday and we'll go ahead here again and retrain this model on our training data and while that's training here I'm just going to save our prediction which will essentially be the same thing we did before we'll predict on the test set and then we'll also get ready here to plot the different components here and once it's done training we'll see that those components will also include a holiday feature to them so the model is done training it was a little quicker that time it took about three minutes and we will now run this predict and create our forecasts with holidays now that the prediction is done let's take this output data frame and run a head command on it again and we can see that each holiday now actually is its own component so it's broken down and I'll have different effects on the model so if I want to plot those components we could see our components look very similar to the previous model that we trained but now we have this holiday effect so you can see certain holidays actually pull down the prediction of the model and there are some that actually make the forecast go up just to visualize this let's go ahead and take one of these earlier plots that we made with our predictions versus the truth we'll predict out our forecast with holidays let's do the week of July 4th because we know that's a holiday and plot this out so we can see here the July 4th predictions are a little bit lower and the actuals were definitely lower on this day due to the holiday and finally let's do the same evaluation metrics that we used before to compare how the model's results are so running these on our holiday predictions we actually see that the model performs slightly worse with these and it could be for a number of reasons but the holiday effects really didn't impact the worst of these models now if we just checked on those holiday dates we may see an improvement of the predictions only on those dates but overall really didn't impact the model too much to add the holidays now finally what we're going to do is just predict into the future and the nice thing about profit is it actually has this part built in so we can take our model object and run this make future future data frame and provided the number of periods so let's say periods equals 365 it's actually going to make 300 65 hourly component so let's multiply this by 24. so this is including the history but we can turn history off and we'll also add in the frequency equals hourly for hourly data frame now if we had trained a model on data all the way up until today then we could make this future data frame going forward into the future but for this example we're just pretending like we have the data up until 2015. so we'll call that our future so let's go ahead and run this model with holidays prediction on the future and we'll call this our forecast and then we'll run a head command on that and you can see this forecast has what would be future dates and has all of our components and if we just subset down to Y hat we can see the actual predictions for those future dates thanks for watching the video make sure to subscribe and I'll see you next time
Original Description
In this video I show how you can use facebook's prophet model to easily do time series forecasting in python. This model is very powerful because it uses an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects.
Kaggle Notebook: https://www.kaggle.com/robikscube/time-series-forecasting-with-prophet-yt
Forecasting with Machine Learning Part 1: https://www.youtube.com/watch?v=vV12dGe_Fho
Forecasting with Machine Learning Part 2: https://www.youtube.com/watch?v=z3ZnOW-S550
Timeline:
00:00 Intro
00:50 Data and Imports
03:43 Features and EDA
05:54 Test Split
06:49 Train and Predict
10:01 Evaluate Forecast
15:01 Adding Holidays
19:19 Make Future Dataframe
Follow me on twitch for live coding streams: https://www.twitch.tv/medallionstallion_
My other videos:
Speed Up Your Pandas Code: https://www.youtube.com/watch?v=SAFmrTnEHLg
Speed up Pandas Code: https://www.youtube.com/watch?v=SAFmrTnEHLg
Intro to Pandas video: https://www.youtube.com/watch?v=_Eb0utIRdkw
Exploratory Data Analysis Video: https://www.youtube.com/watch?v=xi0vhXFPegw
Working with Audio data in Python: https://www.youtube.com/watch?v=ZqpSb5p1xQo
Efficient Pandas Dataframes: https://www.youtube.com/watch?v=u4_c2LDi4b8
* Youtube: https://www.youtube.com/channel/UCxladMszXan-jfgzyeIMyvw
* Twitch: https://www.twitch.tv/medallionstallion_
* Twitter: https://twitter.com/Rob_Mulla
* Kaggle: https://www.kaggle.com/robikscube
#fbprophet #python #machinelearning
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Rob Mulla · Rob Mulla · 60 of 60
← Previous
Next →
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
▶
A Gentle Introduction to Pandas Data Analysis (on Kaggle)
Rob Mulla
Exploratory Data Analysis with Pandas Python
Rob Mulla
7 Python Data Visualization Libraries in 15 minutes
Rob Mulla
Kaggle competition starter notebook walkthrough
Rob Mulla
Kaggle Competitions: A Beginner's Guide to Winning
Rob Mulla
Jupyter Notebook Complete Beginner Guide - From Jupyter to Jupyterlab, Google Colab and Kaggle!
Rob Mulla
Audio Data Processing in Python
Rob Mulla
Complete Data Science Project!
Rob Mulla
Make Your Pandas Code Lightning Fast
Rob Mulla
Image Processing with OpenCV and Python
Rob Mulla
Speed Up Your Pandas Dataframes
Rob Mulla
This INCREDIBLE trick will speed up your data processes.
Rob Mulla
Complete Guide to Cross Validation
Rob Mulla
Easy Python Progress Bars with tqdm
Rob Mulla
Economic Data Analysis Project with Python Pandas - Data scraping, cleaning and exploration!
Rob Mulla
Python Sentiment Analysis Project with NLTK and 🤗 Transformers. Classify Amazon Reviews!!
Rob Mulla
Get Started with Machine Learning and AI in 2023
Rob Mulla
The Trick to Get Unlimited Datasets
Rob Mulla
Video Data Processing with Python and OpenCV
Rob Mulla
Object Detection in 10 minutes with YOLOv5 & Python!
Rob Mulla
Pandas for Data Science #shorts
Rob Mulla
Object Detection in 60 Seconds using Python and YOLOv5 #shorts
Rob Mulla
Machine Learning for Facial Recognition in Python in 60 Seconds #shorts
Rob Mulla
Time Series Forecasting with XGBoost - Use python and machine learning to predict energy consumption
Rob Mulla
Detect Text in Images with Python - pytesseract vs. easyocr vs keras_ocr
Rob Mulla
Solving an Impossible Riddle with Code
Rob Mulla
Do these Pandas Alternatives actually work?
Rob Mulla
Time Series Forecasting with XGBoost - Advanced Methods
Rob Mulla
Data Science Uncut - Data Shootout Kaggle Competition (Aug 1 2022 Stream)
Rob Mulla
Kaggle Dataset Creation from Scratch- Data Science Uncut (Aug 10 2022)
Rob Mulla
Chess Board Computer Vision AI - Data Science Uncut (Sep 7, 2022)
Rob Mulla
25 Nooby Pandas Coding Mistakes You Should NEVER make.
Rob Mulla
DEFCON Hacking AI CTF Solution on Kaggle - Data Science Uncut Sep 11, 2022
Rob Mulla
More Chessboard Computer Vision AI - Data Science Uncut - Sep 13
Rob Mulla
Medallion Data Science Live Stream
Rob Mulla
Community Kaggle Competition Overview - Corn Classification (
Rob Mulla
Deep Learning Image Classification - Corn Kernels - Data Science Uncut
Rob Mulla
OpenAI Whisper Demo: Convert Speech to Text in Python
Rob Mulla
Yolov7 Custom Object Detection in Python Tutorial - Chess Piece Detection
Rob Mulla
Live Kaggle Coding - Enzyme Stability Prediction - Data Science Uncut Sep, 27 2022
Rob Mulla
Finding Chess Cheaters with Python! - Data Science Uncut Livestream
Rob Mulla
Data Science Uncut - Kaggle Community Competition & Chess Data Analysis - Oct 4, 2022
Rob Mulla
Flight Delay Dataset Creation (Data Science Uncut)
Rob Mulla
5 Reasons to Kaggle #shorts
Rob Mulla
♟️ Data Science - Chess Data Analysis
Rob Mulla
EXTREME PYTHON & DATA SCIENCE LIVE STREAM
Rob Mulla
What is Clustering in ML?
Rob Mulla
What is K-Nearest Neighbors?
Rob Mulla
LIVE CODING: Flight Data Exploration with Pandas & Python
Rob Mulla
Kaggle Survey vs. Twitter Sentiment
Rob Mulla
If Top Chess.com Players were STOCKS - Live Coding Data Anaylsis Stream
Rob Mulla
Data Visualization BATTLE!
Rob Mulla
LIVE CODING: Stocks & Sentiment Analysis
Rob Mulla
Progress Bar in Python with TQDM
Rob Mulla
Flight Cancellation Data Analysis
Rob Mulla
Synthetic Dataset Creation for Machine Learning - Blender and Python
Rob Mulla
The Ultimate Coding Setup for Data Science
Rob Mulla
Dataset Creation SPEED RUN - Live Coding With Python & Pandas
Rob Mulla
Data Wrangling with Python and Pandas LIVE
Rob Mulla
Forecasting with the FB Prophet Model
Rob Mulla
More on: ML for Analytics
View skill →Related Reads
📰
📰
📰
📰
Evolutionary Data Through Schemaboi: Achieving Forward, Backwards, and Sideways Compatibility
InfoQ AI/ML
How Morphohack Helped Me Recover €678,000 in Lost Crypto Assets
Medium · Data Science
10 awk and sed Techniques Every Data Analyst Should Know for Data Cleaning and Transformation
Medium · Data Science
From Data Ownership to an AI-Powered Second Brain
Medium · Data Science
Chapters (8)
Intro
0:50
Data and Imports
3:43
Features and EDA
5:54
Test Split
6:49
Train and Predict
10:01
Evaluate Forecast
15:01
Adding Holidays
19:19
Make Future Dataframe
🎓
Tutor Explanation
DeepCamp AI