【问题标题】:Error when trying to implement Holt-Winters Exponential Smoothing in Pyspark尝试在 Pyspark 中实现 Holt-Winters 指数平滑时出错
【发布时间】:2023-04-11 12:08:01
【问题描述】:

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

【问题讨论】:

  • 试试model = ExponentialSmoothing(train.astype('<f8'), seasonal='mul', seasonal_periods=12).fit(),即代替int类型转换为float。

标签: python statsmodels holtwinters


【解决方案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 似乎没有。尽管如此,将输入值转换为浮点数以保持一致性是一个好习惯。

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 2016-06-27
    • 2019-11-07
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多