使用非线性模型拟合CO₂观测数据时预测曲线呈直线而非预期正弦曲线的问题排查与咨询
非线性模型拟合CO₂观测数据时预测曲线呈直线而非预期正弦曲线的问题排查与咨询
我已经自己梳理出两个可能的问题根源,但还是想详细说明情况,看看是不是还有其他疏漏:
我在处理1959到2024年的CO₂观测数据(包含year和co2等字段),目标是用一个包含线性、二次和正弦分量的模型拟合数据,再用拟合出的参数预测训练集(1959-2004)和验证集(2005-2024)的CO₂值。但绘图时发现预测结果是直线,完全没出现预期的正弦波动,而观测数据是带有年度波动的曲线。
先说说我已经发现的两个问题:
- 之前做时间缩放时,错误地用了
year字段,而不是包含小数年份的time字段 - 计算
x_val时,应该减去训练集的起始年份,而不是验证集自己的起始年份
下面是我的完整代码,以及问题细节:
我的模型函数包含线性、二次和正弦项,本应生成带波动的曲线:
# Build model based on equation def co2_model(x, a, b, A, phi, c): # Model containing linear, quadratic and sinusoidal component return (a*x) + (b*(x**2)) + (A*np.sin((2*np.pi*x)+ phi)) + c x = (co2['time']).values - co2['time'].values[0] y = co2['co2'].values # Split data into training and validation periods co2_train = co2[co2['year'] <= 2004] co2_val = co2[co2['year'] > 2004] # Rescaling time periods and specifying dependent variable x_train = co2_train['year'].values - co2_train['year'].values[0] y_train = co2_train['co2'].values x_val = co2_val['year'].values - co2_val['year'].values[0] y_val = co2_val['co2'].values # Fit the co2 model to the training data params_train, covar_train = curve_fit(co2_model, x_train, y_train) a, b, A, phi, c = params_train # Extract fitted parameters #Calculate predicted values for the training and validation data y_train_predict = co2_model(x_train, a, b, A, phi, c) y_val_predict = co2_model(x_val, a, b, A, phi, c) # Plot the original data with the predictions plt.figure(figsize=(15, 6)) plt.scatter(x, y, color='grey', label='Observed temperature', alpha=0.5) # Plot the training period predictions plt.plot(x_train, y_train_predict, label='Training Period', color='blue', linestyle='--') # Plot the validation period predictions plt.plot(x_val, y_val_predict, label='Validation Period', color='red', linestyle='--') plt.xlabel('Year (CE)') plt.ylabel('CO$_2$ (ppm)') # Show the plot plt.show()
实际与预期效果:
- 实际输出:预测的训练集和验证集曲线都是直线,完全没有正弦波动
- 预期效果:预测曲线应该和观测数据类似,带有年度正弦波动,呈现蓝红两段的带波动曲线(和观测数据的形态一致)
想请教大家,除了我已经发现的两个问题,是不是还有其他我没注意到的细节导致预测结果异常?
备注:内容来源于stack exchange,提问作者carpet
相关产品推荐
相关产品推荐

