python用线性回归预测股票价格的实现代码
作者:qq_19600291 时间:2023-01-24 02:14:31
线性回归在整个财务中广泛应用于众多应用程序中。在之前的教程中,我们使用普通最小二乘法(OLS)计算了公司的beta与相对索引的比较。现在,我们将使用线性回归来估计股票价格。
线性回归是一种用于模拟因变量(y)和自变量(x)之间关系的方法。通过简单的线性回归,只有一个自变量x。可能有许多独立变量属于多元线性回归的范畴。在这种情况下,我们只有一个自变量即日期。对于第一个日期上升到日期向量长度的整数,该日期将由1开始的整数表示,该日期可以根据时间序列数据而变化。当然,我们的因变量将是股票的价格。为了理解线性回归,您必须了解您可能在学校早期学到的相当基本的等式。
y = a + bx
Y =预测值或因变量
b =线的斜率
x =系数或自变量
a = y截距
从本质上讲,这将构成我们对数据的最佳拟合。在OLS过程中通过数据集绘制了大量线条。该过程的目标是找到最佳拟合线,最小化平方误差和(SSE)与股票价格(y)的实际值以及我们在数据集中所有点的预测股票价格。这由下图表示。对于绘制的每条线,数据集中的每个点与模型输出的相应预测值之间存在差异。将这些差异中的每一个加起来并平方以产生平方和。从列表中,我们采用最小值导致我们的最佳匹配线。考虑下图:
第一部分:获取数据:
from matplotlib import style
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import quandl
import datetime
style.use('ggplot')
#Dates
start_date = datetime.date(2017,1,3)
t_date=start_date, end_date=end_date, collapse="daily")
df = df.reset_index()
prices = np.reshape(prices, (len(prices), 1))
第二部分:创建一个回归对象:
', linewidth=3, label = 'Predicted Price') #plotting the line made by linear regression
plt.title('Linear Regression | Time vs. Price')
plt.legend()
predicted_price =regressor.predict(date)
输出:
预测日期输入价格:
创建训练/测试集
et
xtrain, x , ytrain)
#Train
plt.title('Linear Regression | Time vs. Price')
#Test Set Graph
plt.scatter(xtest, ytest, color='yellow', label= 'Actual Price') #plotting the initial datapoints
plt.plot(xtest, regressor.predict(xtest), color='blue', linewidth=3, label = 'Predicted Price') #plotting
plt.show()
输出:
测试集:
来源:https://blog.csdn.net/qq_19600291/article/details/82627886
标签:python,线性回归,股票
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用moment.js获取本周、前n周、后n周开始结束日期及动态计算周数
2024-05-28 15:40:15
分享一些非常实用的Python小技巧
2022-08-09 09:27:17
关于pyinstaller生成.exe程序报错:缺少.ini文件的分析
2023-12-25 13:43:22
![](https://img.aspxhome.com/file/2023/5/78835_0s.png)
使用PyQtGraph绘制精美的股票行情K线图的示例代码
2023-12-05 11:58:21
![](https://img.aspxhome.com/file/2023/6/126956_0s.png)
sklearn+python:线性回归案例
2023-10-19 20:07:01
sqlserver、Mysql、Oracle三种数据库的优缺点总结
2024-01-22 10:33:56
pandas groupby 分组取每组的前几行记录方法
2021-06-19 05:52:20
php浅析反序列化结构
2023-11-17 17:34:37
Python 异步等待任务集合
2022-08-14 17:23:22
python pygame实现滚动横版射击游戏城市之战
2021-07-11 00:32:51
![](https://img.aspxhome.com/file/2023/2/83992_0s.jpg)
使用vue与jquery实时监听用户输入状态的操作代码
2023-07-02 17:08:02
python将天气预报可视化
2021-01-19 01:37:46
![](https://img.aspxhome.com/file/2023/5/110495_0s.jpg)
详解Python+OpenCV实现图像二值化
2022-01-02 13:08:07
![](https://img.aspxhome.com/file/2023/9/67389_0s.jpg)
利用numpy+matplotlib绘图的基本操作教程
2021-01-26 11:14:31
![](https://img.aspxhome.com/file/2023/8/133778_0s.png)
支付宝lab logo设计创意发想
2009-11-12 12:44:00
![](https://img.aspxhome.com/file/UploadPic/200911/12/blog1-38s.jpg)
Django+Celery实现定时任务的示例
2023-07-27 19:48:18
![](https://img.aspxhome.com/file/2023/4/61704_0s.png)
基于bootstrap实现收缩导航条
2024-04-28 09:53:16
![](https://img.aspxhome.com/file/2023/1/132721_0s.jpg)
perl的格式化(Format)报表输出实现代码
2022-02-10 09:43:47
用 Javascript 验证表单(form)中的单选(radio)值
2024-04-10 10:39:21
基于Python制作B站视频下载小工具
2023-11-18 11:58:50
![](https://img.aspxhome.com/file/2023/7/80417_0s.jpg)