0

我正在尝试对我的数据集 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')
4

1 回答 1

1

该错误表明输入值应该是doubles,但long接收到了类型。强制输入值是 numpy 浮点数而不是 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 似乎没有。尽管如此,将输入值转换为浮点数以保持一致性是一个好习惯。

于 2019-09-09T17:41:18.287 回答