循环 FBProphet 对每个区域进行建模会出现异常错误
Looping over FBProphet to model each region gives an exception error
我有一个多变量时间序列数据,我正在使用 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.
将模型实例移到 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)
我有一个多变量时间序列数据,我正在使用 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.
将模型实例移到 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)