基于SVR的时间序列股票价格预测测试集预测方法咨询
你只需要对测试集做和训练集完全一致的日期格式转换,之后调用训练好的SVR模型的predict方法即可,完整操作步骤如下:
1. 预处理测试集数据
和训练集的日期转换逻辑保持统一,处理为模型可接收的输入格式:
# 处理测试集日期 test_dates_df = test_df.copy() test_dates_df = test_dates_df.reset_index() # 保存测试集原始日期用于后续绘图 test_org_dates = test_dates_df['Date'] # 日期转数值格式,和训练集转换规则一致 test_dates_df['Date'] = test_dates_df['Date'].map(mdates.date2num) # 转为模型要求的二维数组输入格式 test_dates = test_dates_df['Date'].to_numpy() test_dates = np.reshape(test_dates, (len(test_dates), 1)) # 提取测试集真实调整后收盘价用于后续效果对比 test_real_prices = test_df['Adj Close'].to_numpy()
2. 执行测试集预测
直接调用已经拟合完成的svr_rbf模型生成预测结果:
test_pred_prices = svr_rbf.predict(test_dates)
3. (可选)完整效果可视化
可以把训练集、测试集的真实值和预测值放在同一张图里对比效果:
plt.figure(figsize = (15,7)) # 训练集部分 plt.plot(org_dates, prices, color= 'black', label= '训练集原始数据') plt.plot(org_dates, svr_rbf.predict(dates), color= 'red', label= '训练集RBF模型预测值') # 测试集部分 plt.plot(test_org_dates, test_real_prices, color= 'green', label= '测试集原始数据') plt.plot(test_org_dates, test_pred_prices, color= 'blue', label= '测试集RBF模型预测值') plt.xlabel('日期') plt.ylabel('调整后收盘价') plt.legend() plt.show()
注:仅用日期作为单特征的SVR时序预测泛化能力有限,如果测试集预测偏差较大属于正常情况,实际使用建议加入成交量、均线、大盘指数等更多关联特征,同时网格搜索优化C、gamma超参数提升效果。
内容的提问来源于stack exchange,提问作者Akhter
相关产品推荐
相关产品推荐

