How to get Exponential Smoothing model forecast values to POWER BI/POWER Query dataset?

Viewed 196

I am stuck with getting the forecast values in the POWER BI /query dataset. Below is the code I have where I tried to export the y_hat values using pd.DataFrame. The code does not give error but only original dataset values get returned not the future date forecasted values. I want to have a separate dataset which would contains full forecasted values for next 6 months. What can be done to achieve the same?

# 'dataset' holds the input data for this script
dataset = dataset.drop_duplicates()

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
from matplotlib.dates import DateFormatter
import numpy as np
import pandas as pd
import datetime
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import os
from datetime import datetime, timedelta

dataset['Month']= pd.to_datetime(dataset['Month'])
dataset.set_index('Month', inplace=True)

def get_prediction(dataset):

 list_TPID = dataset.TPID.unique()
 for TPID in list_TPID:

  TPID_df = dataset.loc[dataset['TPID'] == TPID]

  train, test = dataset.iloc[:4,0] , dataset.iloc[3:,0]
  model= ExponentialSmoothing(train,trend='add',damped=False).fit() 
  y_hat = model.forecast(6)

  dfoutput= pd.DataFrame(y_hat)
1 Answers

What you have in your snippet are a bunch of imports and a function definition. What you seem to be missing is a return statement in the function, like return(dfoutput). Your indentation seems a bit off though. But if everything else is correct you are missing a call to your function such as output=get_prediction(dataset=dataset).

As long as output does indeed end up as a dataframe, then that will be made available to you in PowerBI after the code is run.

Related