LinearRegression量化简单实现股价趋势图

本文介绍了如何使用Python中的A股数据接口、Pandas库和scikit-learn库中的LinearRegression模型对股票价格进行时间序列预测。作者通过提取sz002594股票的历史数据,构建并展示了未来40天的股价预测趋势图。
摘要由CSDN通过智能技术生成
import akshare as ak
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

#sh600571、sh603019
stock_data = ak.stock_zh_a_daily("sz002594", "2016-01-01", "2024-02-29")

# 选择用于预热的特征数据
stock_data['date'] = pd.to_datetime(stock_data['date'])
stock_data['day_of_year'] = stock_data['date'].dt.dayofyear
x = stock_data['day_of_year'].values.reshape(-1, 1)
y = stock_data['close'].values.reshape(-1, 1)

# 训练特征回归模型
model = LinearRegression()
model.fit(x, y)

# 预测未来40天价格走势
future_days = pd.date_range("2024-02-29", periods=320)
future_day_of_year = future_days.dayofyear.values.reshape(-1, 1)
future_predictions = model.predict(future_day_of_year)

# 绘制预测趋势图
plt.figure(figsize=(12, 6))
plt.rcParams["font.sans-serif"] = ["SimHei"]  # 设置字体
plt.rcParams["axes.unicode_minus"] = False  # 正常显示负号
plt.plot(stock_data['date'], stock_data['close'], label="历史股价")
plt.plot(future_days, future_predictions, label="预测股价", linestyle="--")
plt.xlabel("日期")
plt.ylabel("收盘价")
plt.title("股价走势预测")
plt.legend()
plt.show()
  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tanrenzong1986

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值