首页 > 解决方案 > 类型错误:+ 不支持的操作数类型:statsmodels SimpleExpoentialSmoothing 的“时间戳”和“NoneType”

问题描述

我正在尝试按照本教程创建一个简单的指数平滑模型:https ://towardsdatascience.com/time-series-in-python-exponential-smoothing-and-arima-processes-2c67f2a52788

但遇到了我不明白的问题。我有一个非常简单的 pandas DataFrame ,它time_series使用每日日期时间索引和 20 到 100 之间的值来表示当天就诊的人数。它看起来像这样:

            patients
Date                
2015-01-04        49
2015-01-05        51
2015-01-06        48
2015-01-07        30
2015-01-08        27

fit1但是,在我创建以构建到 SES 模型的以下代码中运行行时,我收到一个错误。构建代码如下:

train, test = train_test_split(time_series, test_size=0.30, shuffle=False)

model = SimpleExpSmoothing(np.asarray(train['patients']))
model._index = pd.to_datetime(train.index)

fit1 = model.fit()
pred1 = fit1.forecast(9)
fit2 = model.fit(smoothing_level=.2)
pred2 = fit2.forecast(9)
fit3 = model.fit(smoothing_level=.5)
pred3 = fit3.forecast(9)

错误如下,我发现它很奇怪,因为我已经检查过并且 train 和 test 都不包含空值:

TypeError: unsupported operand type(s) for +: 'Timestamp' and 'NoneType'

有谁明白为什么会这样?

非常感谢。

标签: pythonpandastime-seriesstatsmodelsholtwinters

解决方案


我有完整的源代码,但它不起作用。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from statsmodels.tsa.holtwinters import SimpleExpSmoothing, Holt
import numpy as np


df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/monthly-milk-production-pounds.csv",
                 names = ["time", "data"],skiprows=1)



df.time = pd.to_datetime(df.time, format = "%Y-%m")
df = df.set_index('time')
  
from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size=0.30, shuffle=False)

model = SimpleExpSmoothing(np.asarray(train['data']),
                           initialization_method='estimated')


model._index = pd.to_datetime(train.index)
    
fit1 = model.fit()

pred1 = fit1.forecast(steps = 4)

请问有什么建议吗?


推荐阅读