使用Prophet模型拟合数据时报错:error: terminated by signal 3221225467、Optimization terminated abnormally. Falling back to Newton.
一开始无法调用Prophet模型,在安装cmdstan之后解决了,但是在拟合数据df的时候,再次报错:
编辑环境:anaconda python 3.9.19
编辑器:VS Code、Jupyter
--Name____Version____Build________ Channel
cmdstan____ 2.36.0 ____ h8f0442e_0____ unknown
cmdstanpy ____ 1.2.5 ____ pypi_0 _______ pypi
查了很多资料,找不到解决办法,请大家帮忙看看。
#输出是:
22:45:28 - cmdstanpy - INFO - Chain [1] start processing
22:45:28 - cmdstanpy - INFO - Chain [1] done processing
22:45:28 - cmdstanpy - ERROR - Chain [1] error: terminated by signal 3221225467
Optimization terminated abnormally. Falling back to Newton.
22:45:28 - cmdstanpy - INFO - Chain [1] start processing
22:45:28 - cmdstanpy - INFO - Chain [1] done processing
22:45:28 - cmdstanpy - ERROR - Chain [1] error: terminated by signal 3221225467
#报错部分是:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
d:\anaconda3\envs\DL\lib\site-packages\prophet\models.py in fit(self, stan_init, stan_data, **kwargs)
120 try:
--> 121 self.stan_fit = self.model.optimize(**args)
122 except RuntimeError as e:
d:\anaconda3\envs\DL\lib\site-packages\cmdstanpy\model.py in optimize(self, data, seed, inits, output_dir, sig_figs, save_profile, algorithm, init_alpha, tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param, history_size, iter, save_iterations, require_converged, show_console, refresh, time_fmt, timeout, jacobian)
658 else:
--> 659 raise RuntimeError(msg)
660 mle = CmdStanMLE(runset)
RuntimeError: Error during optimization! Command 'D:\anaconda3\envs\DL\Lib\site-packages\prophet\stan_model\prophet_model.bin random seed=21568 data file=C:\Users\22410\AppData\Local\Temp\tmpdf9wcwa_\sh41vlv4.json init=C:\Users\22410\AppData\Local\Temp\tmpdf9wcwa_\9makqtkf.json output file=C:\Users\22410\AppData\Local\Temp\tmpdf9wcwa_\prophet_model0o3vozaz\prophet_model-20250122224528.csv method=optimize algorithm=lbfgs iter=10000' failed:
During handling of the above exception, another exception occurred:
RuntimeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_25676\3138887884.py in <module>
1 model = Prophet()
----> 2 model.fit(df)
d:\anaconda3\envs\DL\lib\site-packages\prophet\forecaster.py in fit(self, df, **kwargs)
1230 self.params = self.stan_backend.sampling(stan_init, dat, self.mcmc_samples, **kwargs)
1231 else:
-> 1232 self.params = self.stan_backend.fit(stan_init, dat, **kwargs)
...
--> 659 raise RuntimeError(msg)
660 mle = CmdStanMLE(runset)
661 return mle
RuntimeError: Error during optimization! Command 'D:\anaconda3\envs\DL\Lib\site-packages\prophet\stan_model\prophet_model.bin random seed=21306 data file=C:\Users\22410\AppData\Local\Temp\tmpdf9wcwa_\rcbm2_y7.json init=C:\Users\22410\AppData\Local\Temp\tmpdf9wcwa_\gec1luoc.json output file=C:\Users\22410\AppData\Local\Temp\tmpdf9wcwa_\prophet_modeli1let55r\prophet_model-20250122224528.csv method=optimize algorithm=newton iter=10000' failed:
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from prophet import Prophet
df = pd.read_csv('data_ar.csv')
df = (df.iloc[0:359,:]).iloc[::-1]
# 将 'date' 列转换为日期格式
df['ds'] = pd.to_datetime(df['ds'])
df.head() # df有两列:‘ds’、‘y’,满足Prophet模型。
plt.plot(df['y'])
# 检查数据
print(df.describe())
print(df.isnull().sum()) # 数据无缺失
model = Prophet()
model.fit(df)