如何在 Python 中修复 statsmodels 的 Holt 和 Holt-Winters 函数中的 "TypeError"
How to fix "TypeError" in Holt and Holt-Winters function of statsmodels in Python
我使用这样的数据
data = [253993,275396.2,315229.5,356949.6,400158.2,442431.7,495102.9,570164.8,\
640993.1,704250.4,767455.4,781807.8,776332.3,794161.7,834177.7,931651.5,\
1028390,1114914]
然后,我导入 statsmodels 并使用 Holt 的方法
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
# Holt’s Method
fit1 = Holt(data).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o')
fit2 = Holt(data, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o')
fit3 = Holt(data, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2)
l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='o')
l4, = plt.plot(data, marker='o')
plt.legend(handles = [l1, l2, l3, l4], labels = ["Holt's linear trend", "Exponential trend", "Additive damped trend", 'data'], loc = 'best', prop={'size': 7})
plt.show()
fit2 抛出异常
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-9ce7957db4db> in <module>()
3 l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o')
4
----> 5 fit2 = Holt(data, exponential=True)
6 fit2.fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
7 l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o')
g:\competition\venv\lib\site-packages\statsmodels\tsa\holtwinters.py in __init__(self, endog, exponential, damped)
851 def __init__(self, endog, exponential=False, damped=False):
852 trend = 'mul' if exponential else 'add'
--> 853 super(Holt, self).__init__(endog, trend=trend, damped=damped)
854
855 def fit(self, smoothing_level=None, smoothing_slope=None, damping_slope=None, optimized=True):
g:\competition\venv\lib\site-packages\statsmodels\tsa\holtwinters.py in __init__(self, endog, trend, damped, seasonal, seasonal_periods, dates, freq, missing)
389 self.trending = trend in ['mul', 'add']
390 self.seasoning = seasonal in ['mul', 'add']
--> 391 if (self.trend == 'mul' or self.seasonal == 'mul') and (endog <= 0.0).any():
392 raise NotImplementedError(
393 'Unable to correct for negative or zero values')
TypeError: '<=' not supported between instances of 'list' and 'float'
不知道为什么,其他都是正常的(Holt-Winters的方法也是这样)
我认为是指数参数导致 problem.So 我应该怎么做才能使用指数模型?
我对这个库不是很有经验,但它似乎需要一个系列而不是数据列表。引入 pandas.pd 并将您的数据转换为 pd.Series:
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
import pandas as pd
data = [253993,275396.2,315229.5,356949.6,400158.2,442431.7,495102.9,570164.8,\
640993.1,704250.4,767455.4,781807.8,776332.3,794161.7,834177.7,931651.5,\
1028390,1114914]
series = pd.Series(data)
# Holt's Method
fit1 = Holt(series).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o')
fit2 = Holt(series, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o')
fit3 = Holt(series, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2)
l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='o')
l4, = plt.plot(series, marker='o')
plt.legend(handles = [l1, l2, l3, l4], labels = ["Holt's linear trend", "Exponential trend", "Additive damped trend", 'data'], loc = 'best', prop={'size': 7})
plt.show()
我使用这样的数据
data = [253993,275396.2,315229.5,356949.6,400158.2,442431.7,495102.9,570164.8,\
640993.1,704250.4,767455.4,781807.8,776332.3,794161.7,834177.7,931651.5,\
1028390,1114914]
然后,我导入 statsmodels 并使用 Holt 的方法
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
# Holt’s Method
fit1 = Holt(data).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o')
fit2 = Holt(data, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o')
fit3 = Holt(data, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2)
l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='o')
l4, = plt.plot(data, marker='o')
plt.legend(handles = [l1, l2, l3, l4], labels = ["Holt's linear trend", "Exponential trend", "Additive damped trend", 'data'], loc = 'best', prop={'size': 7})
plt.show()
fit2 抛出异常
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-9ce7957db4db> in <module>()
3 l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o')
4
----> 5 fit2 = Holt(data, exponential=True)
6 fit2.fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
7 l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o')
g:\competition\venv\lib\site-packages\statsmodels\tsa\holtwinters.py in __init__(self, endog, exponential, damped)
851 def __init__(self, endog, exponential=False, damped=False):
852 trend = 'mul' if exponential else 'add'
--> 853 super(Holt, self).__init__(endog, trend=trend, damped=damped)
854
855 def fit(self, smoothing_level=None, smoothing_slope=None, damping_slope=None, optimized=True):
g:\competition\venv\lib\site-packages\statsmodels\tsa\holtwinters.py in __init__(self, endog, trend, damped, seasonal, seasonal_periods, dates, freq, missing)
389 self.trending = trend in ['mul', 'add']
390 self.seasoning = seasonal in ['mul', 'add']
--> 391 if (self.trend == 'mul' or self.seasonal == 'mul') and (endog <= 0.0).any():
392 raise NotImplementedError(
393 'Unable to correct for negative or zero values')
TypeError: '<=' not supported between instances of 'list' and 'float'
不知道为什么,其他都是正常的(Holt-Winters的方法也是这样)
我认为是指数参数导致 problem.So 我应该怎么做才能使用指数模型?
我对这个库不是很有经验,但它似乎需要一个系列而不是数据列表。引入 pandas.pd 并将您的数据转换为 pd.Series:
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
import pandas as pd
data = [253993,275396.2,315229.5,356949.6,400158.2,442431.7,495102.9,570164.8,\
640993.1,704250.4,767455.4,781807.8,776332.3,794161.7,834177.7,931651.5,\
1028390,1114914]
series = pd.Series(data)
# Holt's Method
fit1 = Holt(series).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o')
fit2 = Holt(series, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False)
l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o')
fit3 = Holt(series, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2)
l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='o')
l4, = plt.plot(series, marker='o')
plt.legend(handles = [l1, l2, l3, l4], labels = ["Holt's linear trend", "Exponential trend", "Additive damped trend", 'data'], loc = 'best', prop={'size': 7})
plt.show()