尝试在Pyspark中实现Holt-Winters指数平滑时出错

时间:2019-09-08 17:37:02

标签: python statsmodels holtwinters

我正在尝试对我的数据集FinalModel进行Holt-Winters指数平滑处理,该数据集以Date作为索引,另外还有Crimecount列。我只想预测CrimeCount列,但出现以下错误:

ValueError: Buffer dtype mismatch, expected 'double' but got 'long long'

我的代码:

df = FinalModel.copy()
train, test = FinalModel.iloc[:85, 18], df.iloc[85:, 18]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing

df.index.freq = 'MS'
model = ExponentialSmoothing(train.astype(np.int64), seasonal='mul', seasonal_periods=12).fit()
pred = model.predict(start=test.index[0], end=test.index[-1])
plt.plot(train.index, train, label='Train')
plt.plot(test.index, test, label='Test')
plt.plot(pred.index, pred, label='Holt-Winters')
plt.legend(loc='best')

1 个答案:

答案 0 :(得分:1)

该错误表明输入值应为doubles,但收到的是long类型。强制输入值为numpy floats而不是numpy ints将解决问题:

df = FinalModel.copy()
train, test = FinalModel.iloc[:85, 18], df.iloc[85:, 18]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing

df.index.freq = 'MS'
model = ExponentialSmoothing(train.astype('<f8'), seasonal='mul', seasonal_periods=12).fit()
pred = model.predict(start=test.index[0], end=test.index[-1])
plt.plot(train.index, train, label='Train')
plt.plot(test.index, test, label='Test')
plt.plot(pred.index, pred, label='Holt-Winters')
plt.legend(loc='best')

通常,statsmodelssklearn中的大多数统计模型都假定输入值为浮点型。这些方法大多数都可以自动为您进行转换,但ExponentialSmoothing似乎没有。尽管如此,将输入值强制转换为浮点数以保持一致性是个好习惯。