Original link:tecdat.cn/?p=7692

Original source:Tuo End number according to the tribe public number

 

A forecast is usually considered a development of the report. Reports can help us answer the question, what happened? Predictions help answer the next logical question, what will happen?

Prophet aims to “make it easier for experts and non-experts alike to make high-quality predictions that meet their needs.

You’ll learn how to use Prophet (in Python) to solve a common problem: predicting the company’s daily orders for the next year.

Data preparation and exploration

Prophet is best for daily periodic data and at least a year of historical data. We will use SQL to process the data to be predicted on a daily basis:

select
  date,
  value
from modeanalytics.daily_orders
order by date
Copy the code

支那

We can pipe SQL query result sets into R data box objects. First, rename your SQL query to Daily Orders. Then, in R, we can pass the query result set to data frame DF using the following statement:

df = datasets["Daily Orders"]
Copy the code

To quickly understand how many observations your data box contains, run the following statement:

df.shape
Copy the code

There are two columns in the Prophet input DataFrame: a date and a value, respectively.

df.dtypes
Copy the code

Verify that the columns in the data box are of the correct data type, and ds creates a new column in the data box that is an exact copy of that column:

df['ds'] = df['date']
df['y'] = df['value']
Copy the code

You can then repurpose the date column to be used as an index to the data box:

df.set_index('date')
Copy the code

Now that you have the data ready to use with Prophet, plot it and examine the data before entering it into Prophet.

 

 

The Box – Cox transformation

Often in forecasting, you explicitly choose a particular type of power transformation to apply to the data to eliminate noise, and then feed the data into the prediction model (for example, logarithmic transformation or square root transformation, etc.). However, it can sometimes be difficult to determine which transformation is right for your data.

Box-cox transformation is a data transformation used to evaluate a set of Lambda coefficients (λ) and select values that achieve the best approximation of normality. For our example, we’ll let the Boxcox method determine the best λ for the transformation and return that value to the variable named LAM:

Y df['y'], lam = boxcox(df['value'])Copy the code

If we plot the newly converted data with the unconverted data, we can see that the Box-Cox transformation eliminates the observed increase in variance over time:

 

To predict

The first step in creating a prediction with Prophet is to import the FBProphet library into our Python:

import fbprophet
Copy the code

After importing the Prophet library into the notebook, we can start with Prophet:

m = fbprophet.Prophet()
Copy the code

Once the Prophet object is instantiated, you can fit the model into historical data. You can do this by calling a method on a Prophet object through FIT and passing in a data box:

After using Prophet to fit the model through the box-Cox transformed data set, you can now start making predictions about future dates.Copy the code
We can now use the Predict method to predict each row in future data frames.Copy the code
At this point, Prophet creates a new data box assigned to the variable that contains the predicted value yhat for the future date under that column along with the confidence interval and prediction section. We can visualize the prediction using Prophet's built-in plot: in our example, our prediction looks like this:Copy the code

 

If you want to visualize individual prediction components, you can use Prophet’s built-in plot_Components method: Plot_Components running on our sample data will return the following component visualization:

 

Predictions and component visualizations show that Prophet is able to accurately model underlying trends in the data, as well as accurately model weekly and yearly seasonality (for example, low order volumes on weekends and holidays).

Inverse Box – Cox transformation

Because Prophet is used for box-Cox converted data, you need to convert the predicted values back to their original units. To convert the new predicted value back to its original units, you will need to perform a Box-Cox reversal.

The INV_boxcox method has two required inputs. The array of data to be converted and the value λ for the conversion. We will invert specific columns in the prediction data frame and provide the λ value previously obtained from the first Box-Cox transform stored in the LAM variable:

Now that you have converted the predicted value back to its original units, you can visualize the predicted value with the historical value:Copy the code