series_fbprophet_forecast_fl()
The function series_fbprophet_forecast_fl() is a user-defined function (UDF) that takes an expression containing a time series as input, and predicts the values of the last trailing points using the Prophet algorithm. The function returns both the forecasted points and their confidence intervals. This function is a Kusto Query Language (KQL) wrapper to Prophet() class, and exposes only the parameters that are mandatory for prediction. Feel free to modify your copy to support more parameters. such as holidays, change points, Fourier order, and so on.
- Install the
fbprophetpackage since it isn’t included in the Python image. To install the package, do the following:- Follow the guidelines for Installing packages for the Python plugin.
- To save time in the above guidelines, you can download the
prophetZIP file, containing the wheel files ofprophetand its dependencies, from https://artifactswestusnew.blob.core.windows.net/public/prophet-1.1.5.zip. Save this file to your allowlisted blob container.
- To save time in the above guidelines, you can download the
- Create a SAS token with read access to your ZIP file. To create a SAS token, see get the SAS for a blob container.
- In the Example, replace the URL reference in the
external_artifactsparameter with your file path and its SAS token.
- Follow the guidelines for Installing packages for the Python plugin.
Syntax
T | invoke series_fbprophet_forecast_fl(ts_series, y_series, y_pred_series, [ points ], [ y_pred_low_series ], [ y_pred_high_series ])
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| ts_series | string | ✔️ | The name of the input table column containing the time stamps of the series to predict. |
| y_series | string | ✔️ | The name of the input table column containing the values of the series to predict. |
| y_pred_series | string | ✔️ | The name of the column to store the predicted series. |
| points | int | ✔️ | The number of points at the end of the series to predict (forecast). These points are excluded from the learning (regression) process. The default is 0. |
| y_pred_low_series | string | The name of the column to store the series of the lowest values of the confidence interval. Omit if the confidence interval isn’t needed. | |
| y_pred_high_series | string | The name of the column to store the series of the highest values of the confidence interval. Omit if the confidence interval isn’t needed. |
Function definition
You can define the function by either embedding its code as a query-defined function, or creating it as a stored function in your database, as follows:
Query-defined
Define the function using the following let statement. No permissions are required.
let series_fbprophet_forecast_fl=(tbl:(*), ts_series:string, y_series:string, y_pred_series:string, points:int=0, y_pred_low_series:string='', y_pred_high_series:string='')
{
let kwargs = bag_pack('ts_series', ts_series, 'y_series', y_series, 'y_pred_series', y_pred_series, 'points', points, 'y_pred_low_series', y_pred_low_series, 'y_pred_high_series', y_pred_high_series);
let code = ```if 1:
from sandbox_utils import Zipackage
Zipackage.install("prophet.zip")
ts_series = kargs["ts_series"]
y_series = kargs["y_series"]
y_pred_series = kargs["y_pred_series"]
points = kargs["points"]
y_pred_low_series = kargs["y_pred_low_series"]
y_pred_high_series = kargs["y_pred_high_series"]
result = df
sr = pd.Series(df[y_pred_series])
if y_pred_low_series != '':
srl = pd.Series(df[y_pred_low_series])
if y_pred_high_series != '':
srh = pd.Series(df[y_pred_high_series])
from prophet import Prophet
df1 = pd.DataFrame(columns=["ds", "y"])
for i in range(df.shape[0]):
df1["ds"] = pd.to_datetime(df[ts_series][i])
df1["ds"] = df1["ds"].dt.tz_convert(None)
df1["y"] = df[y_series][i]
df2 = df1[:-points]
m = Prophet()
m.fit(df2)
future = df1[["ds"]]
forecast = m.predict(future)
sr[i] = list(forecast["yhat"])
if y_pred_low_series != '':
srl[i] = list(forecast["yhat_lower"])
if y_pred_high_series != '':
srh[i] = list(forecast["yhat_upper"])
result[y_pred_series] = sr
if y_pred_low_series != '':
result[y_pred_low_series] = srl
if y_pred_high_series != '':
result[y_pred_high_series] = srh
```;
tbl
| evaluate python(typeof(*), code, kwargs
, external_artifacts=bag_pack('prophet.zip', 'https://artifactswestusnew.blob.core.windows.net/public/prophet-1.1.5.zip?*** YOUR SAS TOKEN ***'))
};
// Write your query to use the function here.
Stored
Define the stored function once using the following .create function. Database User permissions are required.
.create-or-alter function with (folder = "Packages\\Series", docstring = "Time Series Forecast using Facebook fbprophet package")
series_fbprophet_forecast_fl(tbl:(*), ts_series:string, y_series:string, y_pred_series:string, points:int=0, y_pred_low_series:string='', y_pred_high_series:string='')
{
let kwargs = bag_pack('ts_series', ts_series, 'y_series', y_series, 'y_pred_series', y_pred_series, 'points', points, 'y_pred_low_series', y_pred_low_series, 'y_pred_high_series', y_pred_high_series);
let code = ```if 1:
from sandbox_utils import Zipackage
Zipackage.install("prophet.zip")
ts_series = kargs["ts_series"]
y_series = kargs["y_series"]
y_pred_series = kargs["y_pred_series"]
points = kargs["points"]
y_pred_low_series = kargs["y_pred_low_series"]
y_pred_high_series = kargs["y_pred_high_series"]
result = df
sr = pd.Series(df[y_pred_series])
if y_pred_low_series != '':
srl = pd.Series(df[y_pred_low_series])
if y_pred_high_series != '':
srh = pd.Series(df[y_pred_high_series])
from prophet import Prophet
df1 = pd.DataFrame(columns=["ds", "y"])
for i in range(df.shape[0]):
df1["ds"] = pd.to_datetime(df[ts_series][i])
df1["ds"] = df1["ds"].dt.tz_convert(None)
df1["y"] = df[y_series][i]
df2 = df1[:-points]
m = Prophet()
m.fit(df2)
future = df1[["ds"]]
forecast = m.predict(future)
sr[i] = list(forecast["yhat"])
if y_pred_low_series != '':
srl[i] = list(forecast["yhat_lower"])
if y_pred_high_series != '':
srh[i] = list(forecast["yhat_upper"])
result[y_pred_series] = sr
if y_pred_low_series != '':
result[y_pred_low_series] = srl
if y_pred_high_series != '':
result[y_pred_high_series] = srh
```;
tbl
| evaluate python(typeof(*), code, kwargs
, external_artifacts=bag_pack('prophet.zip', 'https://artifactswestusnew.blob.core.windows.net/public/prophet-1.1.5.zip?*** YOUR SAS TOKEN ***'))
}
Example
The following example uses the invoke operator to run the function.
Query-defined
To use a query-defined function, invoke it after the embedded function definition.
let series_fbprophet_forecast_fl=(tbl:(*), ts_series:string, y_series:string, y_pred_series:string, points:int=0, y_pred_low_series:string='', y_pred_high_series:string='')
{
let kwargs = bag_pack('ts_series', ts_series, 'y_series', y_series, 'y_pred_series', y_pred_series, 'points', points, 'y_pred_low_series', y_pred_low_series, 'y_pred_high_series', y_pred_high_series);
let code = ```if 1:
from sandbox_utils import Zipackage
Zipackage.install("prophet.zip")
ts_series = kargs["ts_series"]
y_series = kargs["y_series"]
y_pred_series = kargs["y_pred_series"]
points = kargs["points"]
y_pred_low_series = kargs["y_pred_low_series"]
y_pred_high_series = kargs["y_pred_high_series"]
result = df
sr = pd.Series(df[y_pred_series])
if y_pred_low_series != '':
srl = pd.Series(df[y_pred_low_series])
if y_pred_high_series != '':
srh = pd.Series(df[y_pred_high_series])
from prophet import Prophet
df1 = pd.DataFrame(columns=["ds", "y"])
for i in range(df.shape[0]):
df1["ds"] = pd.to_datetime(df[ts_series][i])
df1["ds"] = df1["ds"].dt.tz_convert(None)
df1["y"] = df[y_series][i]
df2 = df1[:-points]
m = Prophet()
m.fit(df2)
future = df1[["ds"]]
forecast = m.predict(future)
sr[i] = list(forecast["yhat"])
if y_pred_low_series != '':
srl[i] = list(forecast["yhat_lower"])
if y_pred_high_series != '':
srh[i] = list(forecast["yhat_upper"])
result[y_pred_series] = sr
if y_pred_low_series != '':
result[y_pred_low_series] = srl
if y_pred_high_series != '':
result[y_pred_high_series] = srh
```;
tbl
| evaluate python(typeof(*), code, kwargs
, external_artifacts=bag_pack('prophet.zip', 'https://artifactswestusnew.blob.core.windows.net/public/prophet-1.1.5.zip?*** YOUR SAS TOKEN ***'))
};
//
// Forecasting 3 time series using fbprophet, compare to forecasting using the native function series_decompose_forecast()
//
let min_t = datetime(2017-01-05);
let max_t = datetime(2017-02-03 22:00);
let dt = 2h;
let horizon=7d;
demo_make_series2
| make-series num=avg(num) on TimeStamp from min_t to max_t+horizon step dt by sid
| extend pred_num_native = series_decompose_forecast(num, toint(horizon/dt))
| extend pred_num=dynamic(null), pred_num_lower=dynamic(null), pred_num_upper=dynamic(null)
| invoke series_fbprophet_forecast_fl('TimeStamp', 'num', 'pred_num', toint(horizon/dt), 'pred_num_lower', 'pred_num_upper')
| render timechart
Stored
//
// Forecasting 3 time series using fbprophet, compare to forecasting using the native function series_decompose_forecast()
//
let min_t = datetime(2017-01-05);
let max_t = datetime(2017-02-03 22:00);
let dt = 2h;
let horizon=7d;
demo_make_series2
| make-series num=avg(num) on TimeStamp from min_t to max_t+horizon step dt by sid
| extend pred_num_native = series_decompose_forecast(num, toint(horizon/dt))
| extend pred_num=dynamic(null), pred_num_lower=dynamic(null), pred_num_upper=dynamic(null)
| invoke series_fbprophet_forecast_fl('TimeStamp', 'num', 'pred_num', toint(horizon/dt), 'pred_num_lower', 'pred_num_upper')
| render timechart
Output

Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.