我正在尝试对我的数据集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')
答案 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')
通常,statsmodels
和sklearn
中的大多数统计模型都假定输入值为浮点型。这些方法大多数都可以自动为您进行转换,但ExponentialSmoothing似乎没有。尽管如此,将输入值强制转换为浮点数以保持一致性是个好习惯。