循环 FBProphet 对每个区域进行建模会产生异常错误
Posted
技术标签:
【中文标题】循环 FBProphet 对每个区域进行建模会产生异常错误【英文标题】:Looping over FBProphet to model each region gives an exception error 【发布时间】:2021-02-12 05:44:45 【问题描述】:我有一个多元时间序列数据,我正在使用 FBProhpet。为了可复制性,这里是一个模拟:
df1 = pd.DataFrame(
pd.date_range(start="2020-01-01", end="2020-01-30", freq="D", name="ds")
)
df1["y"] = range(1, 31)
df1["region"] = "A"
df1["val1"] = range(1, 31)
df1["val2"] = np.sqrt(df["val1"]) * 2
df1["val3"] = (np.sqrt(df["val1"]) ** 3) + df["val2"]
df2 = pd.DataFrame(
pd.date_range(start="2020-01-01", end="2020-01-30", freq="D", name="ds")
)
df2["y"] = range(1, 31)
df2["y"] = df2["y"] ** 2
df2["region"] = "B"
df2["val1"] = range(1, 31)
df2["val1"] = df2["val1"] ** 3
df2["val2"] = np.sqrt(df["val1"])
df2["val3"] = (np.sqrt(df["val1"]) ** 2) + df["val2"]
df3 = pd.DataFrame(
pd.date_range(start="2020-01-01", end="2020-01-30", freq="D", name="ds")
)
df3["y"] = range(1, 31)
df3["y"] = np.sqrt(df2["y"])
df3["region"] = "C"
df3["val1"] = range(1, 31)
df3["val1"] = df3["y"] ** 3
df3["val2"] = np.sqrt(df3["y"])
df3["val3"] = (np.sqrt(df3["val1"]) ** 2) + df3["val2"]
df4 = df.append(df2)
df5 = df4.append(df3)
这里的目标是对每个区域的多元时间序列数据进行建模,并将模型保存在列表(或字典)中,稍后将调用该列表来预测隐藏的验证数据集。
我的代码如下:
p = Prophet()
p.add_regressor("val1")
p.add_regressor("val2")
p.add_regressor("val3")
models = []
for region in df5.region.unique():
print(region)
model = p.fit(train)
models.append(model)
但是,我收到此错误:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-265-48d06ef12449> in <module>
6 for region in df5.region.unique():
7 print(region)
----> 8 model = p.fit(train)
9 models.append(model)
Exception: Prophet object can only be fit once. Instantiate a new object.
【问题讨论】:
【参考方案1】:将模型实例化移动到 for
循环中:
models = []
for region in df5.region.unique():
p = Prophet()
p.add_regressor("val1")
p.add_regressor("val2")
p.add_regressor("val3")
print(region)
model = p.fit(train)
models.append(model)
【讨论】:
以上是关于循环 FBProphet 对每个区域进行建模会产生异常错误的主要内容,如果未能解决你的问题,请参考以下文章