{"id":3804,"date":"2020-08-25T19:00:44","date_gmt":"2020-08-25T19:00:44","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2020\/08\/25\/time-series-forecasting-with-prophet-in-python\/"},"modified":"2020-08-25T19:00:44","modified_gmt":"2020-08-25T19:00:44","slug":"time-series-forecasting-with-prophet-in-python","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2020\/08\/25\/time-series-forecasting-with-prophet-in-python\/","title":{"rendered":"Time Series Forecasting With Prophet in Python"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>Time series forecasting can be challenging as there are many different methods you could use and many different hyperparameters for each method.<\/p>\n<p>The Prophet library is an open-source library designed for making forecasts for univariate time series datasets. It is easy to use and designed to automatically find a good set of hyperparameters for the model in an effort to make skillful forecasts for data with trends and seasonal structure by default.<\/p>\n<p>In this tutorial, you will discover how to use the Facebook Prophet library for time series forecasting.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>Prophet is an open-source library developed by Facebook and designed for automatic forecasting of univariate time series data.<\/li>\n<li>How to fit Prophet models and use them to make in-sample and out-of-sample forecasts.<\/li>\n<li>How to evaluate a Prophet model on a hold-out dataset.<\/li>\n<\/ul>\n<p>Let&rsquo;s get started.<\/p>\n<div id=\"attachment_10429\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10429\" loading=\"lazy\" class=\"size-full wp-image-10429\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/06\/Time-Series-Forecasting-With-Prophet-in-Python.jpg\" alt=\"Time Series Forecasting With Prophet in Python\" width=\"800\" height=\"531\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/06\/Time-Series-Forecasting-With-Prophet-in-Python.jpg 800w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/06\/Time-Series-Forecasting-With-Prophet-in-Python-300x199.jpg 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/06\/Time-Series-Forecasting-With-Prophet-in-Python-768x510.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\"><\/p>\n<p id=\"caption-attachment-10429\" class=\"wp-caption-text\">Time Series Forecasting With Prophet in Python<br \/>Photo by <a href=\"https:\/\/flickr.com\/photos\/wurglitsch\/9466317145\/\">Rinaldo Wurglitsch<\/a>, some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is divided into three parts; they are:<\/p>\n<ol>\n<li>Prophet Forecasting Library<\/li>\n<li>Car Sales Dataset\n<ol>\n<li>Load and Summarize Dataset<\/li>\n<li>Load and Plot Dataset<\/li>\n<\/ol>\n<\/li>\n<li>Forecast Car Sales With Prophet\n<ol>\n<li>Fit Prophet Model<\/li>\n<li>Make an In-Sample Forecast<\/li>\n<li>Make an Out-of-Sample Forecast<\/li>\n<li>Manually Evaluate Forecast Model<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2>Prophet Forecasting Library<\/h2>\n<p><a href=\"https:\/\/github.com\/facebook\/prophet\">Prophet<\/a>, or &ldquo;<em>Facebook Prophet<\/em>,&rdquo; is an open-source library for univariate (one variable) time series forecasting developed by Facebook.<\/p>\n<p>Prophet implements what they refer to as an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Additive_model\">additive time series forecasting model<\/a>, and the implementation supports trends, seasonality, and holidays.<\/p>\n<blockquote>\n<p>Implements a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects<\/p>\n<\/blockquote>\n<p>&mdash; <a href=\"https:\/\/cran.r-project.org\/web\/packages\/prophet\/prophet.pdf\">Package &lsquo;prophet&rsquo;<\/a>, 2019.<\/p>\n<p>It is designed to be easy and completely automatic, e.g. point it at a time series and get a forecast. As such, it is intended for internal company use, such as forecasting sales, capacity, etc.<\/p>\n<p>For a great overview of Prophet and its capabilities, see the post:<\/p>\n<ul>\n<li><a href=\"https:\/\/research.fb.com\/blog\/2017\/02\/prophet-forecasting-at-scale\/\">Prophet: forecasting at scale<\/a>, 2017.<\/li>\n<\/ul>\n<p>The library provides two interfaces, including R and Python. We will focus on the Python interface in this tutorial.<\/p>\n<p>The first step is to install the Prophet library using Pip, as follows:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">sudo pip install fbprophet<\/pre>\n<p>Next, we can confirm that the library was installed correctly.<\/p>\n<p>To do this, we can import the library and print the version number in Python. The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># check prophet version\r\nimport fbprophet\r\n# print version number\r\nprint('Prophet %s' % fbprophet.__version__)<\/pre>\n<p>Running the example prints the installed version of Prophet.<\/p>\n<p>You should have the same version or higher.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">Prophet 0.5<\/pre>\n<p>Now that we have Prophet installed, let&rsquo;s select a dataset we can use to explore using the library.<\/p>\n<h2>Car Sales Dataset<\/h2>\n<p>We will use the monthly car sales dataset.<\/p>\n<p>It is a standard univariate time series dataset that contains both a trend and seasonality. The dataset has 108 months of data and a naive persistence forecast can achieve a mean absolute error of about 3,235 sales, providing a lower error limit.<\/p>\n<p>No need to download the dataset as we will download it automatically as part of each example.<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv\">Monthly Car Sales Dataset (csv)<\/a><\/li>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.names\">Monthly Car Sales Dataset Description<\/a><\/li>\n<\/ul>\n<h3>Load and Summarize Dataset<\/h3>\n<p>First, let&rsquo;s load and summarize the dataset.<\/p>\n<p>Prophet requires data to be in Pandas DataFrames. Therefore, we will load and summarize the data using Pandas.<\/p>\n<p>We can load the data directly from the URL by calling the <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.read_csv.html\">read_csv() Pandas function<\/a>, then summarize the shape (number of rows and columns) of the data and view the first few rows of data.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># load the car sales dataset\r\nfrom pandas import read_csv\r\n# load data\r\npath = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv'\r\ndf = read_csv(path, header=0)\r\n# summarize shape\r\nprint(df.shape)\r\n# show first few rows\r\nprint(df.head())<\/pre>\n<p>Running the example first reports the number of rows and columns, then lists the first five rows of data.<\/p>\n<p>We can see that as we expected, there are 108 months worth of data and two columns. The first column is the date and the second is the number of sales.<\/p>\n<p>Note that the first column in the output is a row index and is not a part of the dataset, just a helpful tool that Pandas uses to order rows.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(108, 2)\r\n     Month  Sales\r\n0  1960-01   6550\r\n1  1960-02   8728\r\n2  1960-03  12026\r\n3  1960-04  14395\r\n4  1960-05  14587<\/pre>\n<\/p>\n<h3>Load and Plot Dataset<\/h3>\n<p>A time-series dataset does not make sense to us until we plot it.<\/p>\n<p>Plotting a time series helps us actually see if there is a trend, a seasonal cycle, outliers, and more. It gives us a feel for the data.<\/p>\n<p>We can plot the data easily in Pandas by calling the <em>plot()<\/em> function on the DataFrame.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># load and plot the car sales dataset\r\nfrom pandas import read_csv\r\nfrom matplotlib import pyplot\r\n# load data\r\npath = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv'\r\ndf = read_csv(path, header=0)\r\n# plot the time series\r\ndf.plot()\r\npyplot.show()<\/pre>\n<p>Running the example creates a plot of the time series.<\/p>\n<p>We can clearly see the trend in sales over time and a monthly seasonal pattern to the sales. These are patterns we expect the forecast model to take into account.<\/p>\n<div id=\"attachment_10425\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10425\" loading=\"lazy\" class=\"size-full wp-image-10425\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/02\/Line-Plot-of-Car-Sales-Dataset.png\" alt=\"Line Plot of Car Sales Dataset\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Line-Plot-of-Car-Sales-Dataset.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Line-Plot-of-Car-Sales-Dataset-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Line-Plot-of-Car-Sales-Dataset-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Line-Plot-of-Car-Sales-Dataset-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10425\" class=\"wp-caption-text\">Line Plot of Car Sales Dataset<\/p>\n<\/div>\n<p>Now that we are familiar with the dataset, let&rsquo;s explore how we can use the Prophet library to make forecasts.<\/p>\n<h2>Forecast Car Sales With Prophet<\/h2>\n<p>In this section, we will explore using the Prophet to forecast the car sales dataset.<\/p>\n<p>Let&rsquo;s start by fitting a model on the dataset<\/p>\n<h3>Fit Prophet Model<\/h3>\n<p>To use Prophet for forecasting, first, a <em>Prophet()<\/em> object is defined and configured, then it is fit on the dataset by calling the <em>fit()<\/em> function and passing the data.<\/p>\n<p>The <em>Prophet()<\/em> object takes arguments to configure the type of model you want, such as the type of growth, the type of seasonality, and more. By default, the model will work hard to figure out almost everything automatically.<\/p>\n<p>The <em>fit()<\/em> function takes a <em>DataFrame<\/em> of time series data. The <em>DataFrame<\/em> must have a specific format. The first column must have the name &lsquo;<em>ds<\/em>&lsquo; and contain the date-times. The second column must have the name &lsquo;<em>y<\/em>&lsquo; and contain the observations.<\/p>\n<p>This means we change the column names in the dataset. It also requires that the first column be converted to date-time objects, if they are not already (e.g. this can be down as part of loading the dataset with the right arguments to <em>read_csv<\/em>).<\/p>\n<p>For example, we can modify our loaded car sales dataset to have this expected structure, as follows:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# prepare expected column names\r\ndf.columns = ['ds', 'y']\r\ndf['ds']= to_datetime(df['ds'])<\/pre>\n<p>The complete example of fitting a Prophet model on the car sales dataset is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># fit prophet model on the car sales dataset\r\nfrom pandas import read_csv\r\nfrom pandas import to_datetime\r\nfrom fbprophet import Prophet\r\n# load data\r\npath = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv'\r\ndf = read_csv(path, header=0)\r\n# prepare expected column names\r\ndf.columns = ['ds', 'y']\r\ndf['ds']= to_datetime(df['ds'])\r\n# define the model\r\nmodel = Prophet()\r\n# fit the model\r\nmodel.fit(df)<\/pre>\n<p>Running the example loads the dataset, prepares the DataFrame in the expected format, and fits a Prophet model.<\/p>\n<p>By default, the library provides a lot of verbose output during the fit process. I think it&rsquo;s a bad idea in general as it trains developers to ignore output.<\/p>\n<p>Nevertheless, the output summarizes what happened during the model fitting process, specifically the optimization processes that ran.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.\r\nINFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.\r\nInitial log joint probability = -4.39613\r\n    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes\r\n      99       270.121    0.00413718       75.7289           1           1      120\r\n    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes\r\n     179       270.265    0.00019681       84.1622   2.169e-06       0.001      273  LS failed, Hessian reset\r\n     199       270.283   1.38947e-05       87.8642      0.3402           1      299\r\n    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes\r\n     240       270.296    1.6343e-05       89.9117   1.953e-07       0.001      381  LS failed, Hessian reset\r\n     299         270.3   4.73573e-08       74.9719      0.3914           1      455\r\n    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes\r\n     300         270.3   8.25604e-09       74.4478      0.3522      0.3522      456\r\nOptimization terminated normally:\r\n  Convergence detected: absolute parameter change was below tolerance<\/pre>\n<p>I will not reproduce this output in subsequent sections when we fit the model.<\/p>\n<p>Next, let&rsquo;s make a forecast.<\/p>\n<h3>Make an In-Sample Forecast<\/h3>\n<p>It can be useful to make a forecast on historical data.<\/p>\n<p>That is, we can make a forecast on data used as input to train the model. Ideally, the model has seen the data before and would make a perfect prediction.<\/p>\n<p>Nevertheless, this is not the case as the model tries to generalize across all cases in the data.<\/p>\n<p>This is called making an in-sample (in training set sample) forecast and reviewing the results can give insight into how good the model is. That is, how well it learned the training data.<\/p>\n<p>A forecast is made by calling the <em>predict()<\/em> function and passing a <em>DataFrame<\/em> that contains one column named &lsquo;<em>ds<\/em>&lsquo; and rows with date-times for all the intervals to be predicted.<\/p>\n<p>There are many ways to create this &ldquo;<em>forecast<\/em>&rdquo; <em>DataFrame<\/em>. In this case, we will loop over one year of dates, e.g. the last 12 months in the dataset, and create a string for each month. We will then convert the list of dates into a <em>DataFrame<\/em> and convert the string values into date-time objects.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the period for which we want a prediction\r\nfuture = list()\r\nfor i in range(1, 13):\r\n\tdate = '1968-%02d' % i\r\n\tfuture.append([date])\r\nfuture = DataFrame(future)\r\nfuture.columns = ['ds']\r\nfuture['ds']= to_datetime(future['ds'])<\/pre>\n<p>This <em>DataFrame<\/em> can then be provided to the <em>predict()<\/em> function to calculate a forecast.<\/p>\n<p>The result of the predict() function is a <em>DataFrame<\/em> that contains many columns. Perhaps the most important columns are the forecast date time (&lsquo;<em>ds<\/em>&lsquo;), the forecasted value (&lsquo;<em>yhat<\/em>&lsquo;), and the lower and upper bounds on the predicted value (&lsquo;<em>yhat_lower<\/em>&lsquo; and &lsquo;<em>yhat_upper<\/em>&lsquo;) that provide uncertainty of the forecast.<\/p>\n<p>For example, we can print the first few predictions as follows:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# summarize the forecast\r\nprint(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())<\/pre>\n<p>Prophet also provides a built-in tool for visualizing the prediction in the context of the training dataset.<\/p>\n<p>This can be achieved by calling the <em>plot()<\/em> function on the model and passing it a result DataFrame. It will create a plot of the training dataset and overlay the prediction with the upper and lower bounds for the forecast dates.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\nprint(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())\r\n# plot forecast\r\nmodel.plot(forecast)\r\npyplot.show()<\/pre>\n<p>Tying this all together, a complete example of making an in-sample forecast is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># make an in-sample forecast\r\nfrom pandas import read_csv\r\nfrom pandas import to_datetime\r\nfrom pandas import DataFrame\r\nfrom fbprophet import Prophet\r\nfrom matplotlib import pyplot\r\n# load data\r\npath = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv'\r\ndf = read_csv(path, header=0)\r\n# prepare expected column names\r\ndf.columns = ['ds', 'y']\r\ndf['ds']= to_datetime(df['ds'])\r\n# define the model\r\nmodel = Prophet()\r\n# fit the model\r\nmodel.fit(df)\r\n# define the period for which we want a prediction\r\nfuture = list()\r\nfor i in range(1, 13):\r\n\tdate = '1968-%02d' % i\r\n\tfuture.append([date])\r\nfuture = DataFrame(future)\r\nfuture.columns = ['ds']\r\nfuture['ds']= to_datetime(future['ds'])\r\n# use the model to make a forecast\r\nforecast = model.predict(future)\r\n# summarize the forecast\r\nprint(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())\r\n# plot forecast\r\nmodel.plot(forecast)\r\npyplot.show()<\/pre>\n<p>Running the example forecasts the last 12 months of the dataset.<\/p>\n<p>The first five months of the prediction are reported and we can see that values are not too different from the actual sales values in the dataset.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">ds          yhat    yhat_lower    yhat_upper\r\n0 1968-01-01  14364.866157  12816.266184  15956.555409\r\n1 1968-02-01  14940.687225  13299.473640  16463.811658\r\n2 1968-03-01  20858.282598  19439.403787  22345.747821\r\n3 1968-04-01  22893.610396  21417.399440  24454.642588\r\n4 1968-05-01  24212.079727  22667.146433  25816.191457<\/pre>\n<p>Next, a plot is created. We can see the training data are represented as black dots and the forecast is a blue line with upper and lower bounds in a blue shaded area.<\/p>\n<p>We can see that the forecasted 12 months is a good match for the real observations, especially when the bounds are taken into account.<\/p>\n<div id=\"attachment_10426\" style=\"width: 2010px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10426\" loading=\"lazy\" class=\"size-full wp-image-10426\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-In-Sample-Forecast-With-Prophet.png\" alt=\"Plot of Time Series and In-Sample Forecast With Prophet\" width=\"2000\" height=\"1200\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-In-Sample-Forecast-With-Prophet.png 2000w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-In-Sample-Forecast-With-Prophet-300x180.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-In-Sample-Forecast-With-Prophet-1024x614.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-In-Sample-Forecast-With-Prophet-768x461.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-In-Sample-Forecast-With-Prophet-1536x922.png 1536w\" sizes=\"(max-width: 2000px) 100vw, 2000px\"><\/p>\n<p id=\"caption-attachment-10426\" class=\"wp-caption-text\">Plot of Time Series and In-Sample Forecast With Prophet<\/p>\n<\/div>\n<h3>Make an Out-of-Sample Forecast<\/h3>\n<p>In practice, we really want a forecast model to make a prediction beyond the training data.<\/p>\n<p>This is called an out-of-sample forecast.<\/p>\n<p>We can achieve this in the same way as an in-sample forecast and simply specify a different forecast period.<\/p>\n<p>In this case, a period beyond the end of the training dataset, starting 1969-01.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the period for which we want a prediction\r\nfuture = list()\r\nfor i in range(1, 13):\r\n\tdate = '1969-%02d' % i\r\n\tfuture.append([date])\r\nfuture = DataFrame(future)\r\nfuture.columns = ['ds']\r\nfuture['ds']= to_datetime(future['ds'])<\/pre>\n<p>Tying this together, the complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># make an out-of-sample forecast\r\nfrom pandas import read_csv\r\nfrom pandas import to_datetime\r\nfrom pandas import DataFrame\r\nfrom fbprophet import Prophet\r\nfrom matplotlib import pyplot\r\n# load data\r\npath = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv'\r\ndf = read_csv(path, header=0)\r\n# prepare expected column names\r\ndf.columns = ['ds', 'y']\r\ndf['ds']= to_datetime(df['ds'])\r\n# define the model\r\nmodel = Prophet()\r\n# fit the model\r\nmodel.fit(df)\r\n# define the period for which we want a prediction\r\nfuture = list()\r\nfor i in range(1, 13):\r\n\tdate = '1969-%02d' % i\r\n\tfuture.append([date])\r\nfuture = DataFrame(future)\r\nfuture.columns = ['ds']\r\nfuture['ds']= to_datetime(future['ds'])\r\n# use the model to make a forecast\r\nforecast = model.predict(future)\r\n# summarize the forecast\r\nprint(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())\r\n# plot forecast\r\nmodel.plot(forecast)\r\npyplot.show()<\/pre>\n<p>Running the example makes an out-of-sample forecast for the car sales data.<\/p>\n<p>The first five rows of the forecast are printed, although it is hard to get an idea of whether they are sensible or not.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">ds          yhat    yhat_lower    yhat_upper\r\n0 1969-01-01  15406.401318  13751.534121  16789.969780\r\n1 1969-02-01  16165.737458  14486.887740  17634.953132\r\n2 1969-03-01  21384.120631  19738.950363  22926.857539\r\n3 1969-04-01  23512.464086  21939.204670  25105.341478\r\n4 1969-05-01  25026.039276  23544.081762  26718.820580<\/pre>\n<p>A plot is created to help us evaluate the prediction in the context of the training data.<\/p>\n<p>The new one-year forecast does look sensible, at least by eye.<\/p>\n<div id=\"attachment_10427\" style=\"width: 2010px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10427\" loading=\"lazy\" class=\"size-full wp-image-10427\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-Out-Of-Sample-Forecast-With-Prophet.png\" alt=\"Plot of Time Series and Out-of-Sample Forecast With Prophet\" width=\"2000\" height=\"1200\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-Out-Of-Sample-Forecast-With-Prophet.png 2000w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-Out-Of-Sample-Forecast-With-Prophet-300x180.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-Out-Of-Sample-Forecast-With-Prophet-1024x614.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-Out-Of-Sample-Forecast-With-Prophet-768x461.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Time-Series-and-Out-Of-Sample-Forecast-With-Prophet-1536x922.png 1536w\" sizes=\"(max-width: 2000px) 100vw, 2000px\"><\/p>\n<p id=\"caption-attachment-10427\" class=\"wp-caption-text\">Plot of Time Series and Out-of-Sample Forecast With Prophet<\/p>\n<\/div>\n<h3>Manually Evaluate Forecast Model<\/h3>\n<p>It is critical to develop an objective estimate of a forecast model&rsquo;s performance.<\/p>\n<p>This can be achieved by holding some data back from the model, such as the last 12 months. Then, fitting the model on the first portion of the data, using it to make predictions on the held-pack portion, and calculating an error measure, such as the mean absolute error across the forecasts. E.g. a simulated out-of-sample forecast.<\/p>\n<p>The score gives an estimate of how well we might expect the model to perform on average when making an out-of-sample forecast.<\/p>\n<p>We can do this with the samples data by creating a new <em>DataFrame<\/em> for training with the last 12 months removed.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# create test dataset, remove last 12 months\r\ntrain = df.drop(df.index[-12:])\r\nprint(train.tail())<\/pre>\n<p>A forecast can then be made on the last 12 months of date-times.<\/p>\n<p>We can then retrieve the forecast values and the expected values from the original dataset and calculate a mean absolute error metric using the scikit-learn library.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# calculate MAE between expected and predicted values for december\r\ny_true = df['y'][-12:].values\r\ny_pred = forecast['yhat'].values\r\nmae = mean_absolute_error(y_true, y_pred)\r\nprint('MAE: %.3f' % mae)<\/pre>\n<p>It can also be helpful to plot the expected vs. predicted values to see how well the out-of-sample prediction matches the known values.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# plot expected vs actual\r\npyplot.plot(y_true, label='Actual')\r\npyplot.plot(y_pred, label='Predicted')\r\npyplot.legend()\r\npyplot.show()<\/pre>\n<p>Tying this together, the example below demonstrates how to evaluate a Prophet model on a hold-out dataset.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># evaluate prophet time series forecasting model on hold out dataset\r\nfrom pandas import read_csv\r\nfrom pandas import to_datetime\r\nfrom pandas import DataFrame\r\nfrom fbprophet import Prophet\r\nfrom sklearn.metrics import mean_absolute_error\r\nfrom matplotlib import pyplot\r\n# load data\r\npath = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv'\r\ndf = read_csv(path, header=0)\r\n# prepare expected column names\r\ndf.columns = ['ds', 'y']\r\ndf['ds']= to_datetime(df['ds'])\r\n# create test dataset, remove last 12 months\r\ntrain = df.drop(df.index[-12:])\r\nprint(train.tail())\r\n# define the model\r\nmodel = Prophet()\r\n# fit the model\r\nmodel.fit(train)\r\n# define the period for which we want a prediction\r\nfuture = list()\r\nfor i in range(1, 13):\r\n\tdate = '1968-%02d' % i\r\n\tfuture.append([date])\r\nfuture = DataFrame(future)\r\nfuture.columns = ['ds']\r\nfuture['ds'] = to_datetime(future['ds'])\r\n# use the model to make a forecast\r\nforecast = model.predict(future)\r\n# calculate MAE between expected and predicted values for december\r\ny_true = df['y'][-12:].values\r\ny_pred = forecast['yhat'].values\r\nmae = mean_absolute_error(y_true, y_pred)\r\nprint('MAE: %.3f' % mae)\r\n# plot expected vs actual\r\npyplot.plot(y_true, label='Actual')\r\npyplot.plot(y_pred, label='Predicted')\r\npyplot.legend()\r\npyplot.show()<\/pre>\n<p>Running the example first reports the last few rows of the training dataset.<\/p>\n<p>It confirms the training ends in the last month of 1967 and 1968 will be used as the hold-out dataset.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">ds      y\r\n91 1967-08-01  13434\r\n92 1967-09-01  13598\r\n93 1967-10-01  17187\r\n94 1967-11-01  16119\r\n95 1967-12-01  13713<\/pre>\n<p>Next, a mean absolute error is calculated for the forecast period.<\/p>\n<p>In this case we can see that the error is approximately 1,336 sales, which is much lower (better) than a naive persistence model that achieves an error of 3,235 sales over the same period.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">MAE: 1336.814<\/pre>\n<p>Finally, a plot is created comparing the actual vs. predicted values. In this case, we can see that the forecast is a good fit. The model has skill and forecast that looks sensible.<\/p>\n<div id=\"attachment_10428\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10428\" loading=\"lazy\" class=\"size-full wp-image-10428\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/02\/Plot-of-Actual-vs.-Predicted-Values-for-Last-12-Months-of-Car-Sales.png\" alt=\"Plot of Actual vs. Predicted Values for Last 12 Months of Car Sales\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Actual-vs.-Predicted-Values-for-Last-12-Months-of-Car-Sales.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Actual-vs.-Predicted-Values-for-Last-12-Months-of-Car-Sales-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Actual-vs.-Predicted-Values-for-Last-12-Months-of-Car-Sales-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/02\/Plot-of-Actual-vs.-Predicted-Values-for-Last-12-Months-of-Car-Sales-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10428\" class=\"wp-caption-text\">Plot of Actual vs. Predicted Values for Last 12 Months of Car Sales<\/p>\n<\/div>\n<p>The Prophet library also provides tools to automatically evaluate models and plot results, although those tools don&rsquo;t appear to work well with data above one day in resolution.<\/p>\n<h2>Further Reading<\/h2>\n<p>This section provides more resources on the topic if you are looking to go deeper.<\/p>\n<ul>\n<li><a href=\"https:\/\/facebook.github.io\/prophet\/\">Prophet Homepage<\/a>.<\/li>\n<li><a href=\"https:\/\/github.com\/facebook\/prophet\">Prophet GitHub Project<\/a>.<\/li>\n<li><a href=\"https:\/\/facebook.github.io\/prophet\/docs\/\">Prophet API Documentation<\/a>.<\/li>\n<li><a href=\"https:\/\/research.fb.com\/blog\/2017\/02\/prophet-forecasting-at-scale\/\">Prophet: forecasting at scale<\/a>, 2017.<\/li>\n<li><a href=\"https:\/\/peerj.com\/preprints\/3190\/\">Forecasting at scale<\/a>, 2017.<\/li>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/monthly-car-sales.csv\">Car Sales Dataset<\/a>.<\/li>\n<li><a href=\"https:\/\/cran.r-project.org\/web\/packages\/prophet\/prophet.pdf\">Package &lsquo;prophet&rsquo;, R Documentation<\/a>.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to use the Facebook Prophet library for time series forecasting.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>Prophet is an open-source library developed by Facebook and designed for automatic forecasting of univariate time series data.<\/li>\n<li>How to fit Prophet models and use them to make in-sample and out-of-sample forecasts.<\/li>\n<li>How to evaluate a Prophet model on a hold-out dataset.<\/li>\n<\/ul>\n<p><strong>Do you have any questions?<\/strong><br \/>\nAsk your questions in the comments below and I will do my best to answer.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/time-series-forecasting-with-prophet-in-python\/\">Time Series Forecasting With Prophet in Python<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/time-series-forecasting-with-prophet-in-python\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee Time series forecasting can be challenging as there are many different methods you could use and many different hyperparameters for each method. [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2020\/08\/25\/time-series-forecasting-with-prophet-in-python\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":3805,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3804"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=3804"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3804\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/3805"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=3804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=3804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=3804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}