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()
LinearRegression量化简单实现股价趋势图
最新推荐文章于 2024-08-15 20:27:56 发布
本文介绍了如何使用Python中的A股数据接口、Pandas库和scikit-learn库中的LinearRegression模型对股票价格进行时间序列预测。作者通过提取sz002594股票的历史数据,构建并展示了未来40天的股价预测趋势图。
摘要由CSDN通过智能技术生成