在 Tableau 中运行 Python(预测)时出错
2018-05-10
160
我对这个系统还很陌生,而且对 Python 也不是很熟悉。因此代码中可能有一些冗余行。
我正在尝试使用 x (Hybrid_MF) 预测 y (CARA_Flows)。虽然相同的代码在 Python 中运行良好,但我在 tableau 中遇到了错误。错误窗口本身显示了正确的预测(以及未来 12 个月的预测)。
此外,集成没有问题。 有人能帮我理解这里的问题吗?
SCRIPT_REAL(
"
import pandas as pd
import numpy as np
dateparse = lambda dates: pd.datetime.strptime(dates, '%Y%m')
data = pd.read_excel('S:\AIM India\Anup\Requests_2018\CTI_Forecasting_Tableau\Forecast_CTI_2.xlsx',parse_dates=['YYYYMM'], index_col='YYYYMM',date_parser=dateparse)
ts_exogenMF = data['Hybrid_MF']
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(ts_exogenMF,order=(2, 0, 2))
results_ARIMA1 = model.fit(disp=-1)
forecast1,std,conf=results_ARIMA1.forecast(steps=12,alpha=0.5)
forecastMF=forecast1
MF_Arr=[]
MF_Arr=forecastMF
ts = data['CARA_Flows']
from statsmodels.tsa.stattools import adfuller
ts_log = np.log(ts)
ts_log_diff = ts_log - ts_log.shift()
model = ARIMA(ts_log,exog=ts_exogenMF,order=(2, 0, 2))
results_ARIMA2 = model.fit(disp=1)
Final_Untransformed_Forecast=results_ARIMA2.predict(start=1, end=46, exog=MF_Arr, dynamic=False)
predictions_ARIMA_log = pd.Series(ts_log.ix[0], index=ts_log.index)
predictions_ARIMA_cumsum = predictions_ARIMA_log.add(Final_Untransformed_Forecast,fill_value=0)
predictions_12M = np.exp(Final_Untransformed_Forecast)
return predictions_12M
",SUM([Hybrid MF]), SUM([CARA Flows]))
2个回答
错误是由于代码和输出的日期格式不匹配造成的。因此,您应该将
pd.datetime.strptime(x, '%Y-%m-%d')
替换为
pd.datetime.strptime(dates, '%Y%m')
Harish Gopi
2018-05-10
当我将输出转换为列表时,这个问题得到了解决。以下是完整代码:
SCRIPT_REAL(
"
import pandas as pd
import numpy as np
dateparse = lambda dates: pd.datetime.strptime(dates, '%Y%m')
data = pd.read_excel('S:\AIM.....\...\ =dateparse)
ts_exogenMF = data['Hybrid_MF']
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(ts_exogenMF,order=(2, 0, 2))
results_ARIMA1 = model.fit(disp=-1)
forecast1,std,conf=results_ARIMA1.forecast(steps=12,alpha=0.5)
forecastMF=forecast1
MF_Arr=[]
MF_Arr=forecastMF
ts = data['CARA_Flows']
from statsmodels.tsa.stattools import adfuller
ts_log = np.log(ts)
ts_log_diff = ts_log - ts_log.shift()
model = ARIMA(ts_log,exog=ts_exogenMF,order=(2, 0, 2))
results_ARIMA2 = model.fit(disp=1)
Final_Untransformed_Forecast=results_ARIMA2.predict(start=0, end=46, exog=MF_Arr, dynamic=False)
predictions_ARIMA_log = pd.Series(ts_log.ix[0], index=ts_log.index)
predictions_ARIMA_cumsum = predictions_ARIMA_log.add(Final_Untransformed_Forecast,fill_value=0)
predictions_12M = np.exp(Final_Untransformed_Forecast)
predList=pd.Series.tolist(predictions_12M)
return predList
",SUM([Hybrid MF]), SUM([CARA Flows]))
Bulleride
2018-05-11