股票价格走势预测

基于线性回归

算法原理

回归是基于已有数据对新的数据进行预测,比如预测股票走势。这里我们主要讲简单线性回归。基于标准的线性回归,可以扩展出更多的线性回归算法。
线性回归就是能够用一个直线较为精确地描述数据之间的关系,这样当出现新的数据的时候,就能够预测出一个简单的值。

支持向量回归

算法原理
SVR作为SVM的分支从而被提出,都要通过训练样本找到一个函数g(x),不同之处在于支持向量机是分类问题,寻求的是一个最优超平面(函数g(x)
)将两类样本点分的最开,最大间隔准则(H1和H2之间间隔最大)是支持向量机最佳准则。

在这里插入图片描述

而支持向量回归机寻求的是一个线性回归方程(函数y=g(x))去拟合所有的样本点,它寻求的最优超平面不是将两类分得最开,而是使样本点离超平面总方差最小。

在这里插入图片描述

模型评价方法

在这里插入图片描述

代码实现

1.基于线性回归

#coding=utf-8
# 线性回归算法一般用于解决”使用已知样本对未知公式参数的估计“类问题
# 获取数据
# 股票数据特征:开盘价(Open)、最高价(High)、最低价(Low)、收盘价(Close)、交易额(Volume)
# 及调整后的开盘价(Adj. Open)、最高价(Adj. High)、最低价(Adj. Low)、收盘价(Adj. Close)、交易额(Adj. Volume)
# 数据预处理
# 除权后的数据更能反映数据特征,选择调整后的数据为主要使用的数据特征
# 两个数据特征:HL_PCT(股票最高价与最低价变化百分比)、PCT_change(股票收盘价与最低价的变化百分比)
# 自变量为:Adj.Close、HL_PCT、PCT_change、Adj.Volume
# 因变量为:Adj.Close

import quandl
from sklearn import preprocessing

df = quandl.get('WIKI/GOOGL')
# df = quandl.get('WIKI/AAPL')
# print(df)

import math
import numpy as np

# 定义预测列变量,存放研究对象的标签名
forecast_col = 'Adj. Close'
# 定义预测天数,这里设置为所有数据量长度的1%
forecast_out = int(math.ceil(0.01 * len(df)))
# 只用到df中的下面几个字段
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
# 构造两个新列
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Close']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
# 真正用到的特征
df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
# 处理空值,这里设置为-99999
df.fillna(-99999, inplace=True)
# label代表预测结果,通过让Adj. Close列的数据往前移动1%行来表示
df['label'] = df[forecast_col].shift(-forecast_out)
# 生成在模型中使用的数据X,y,以及预测时用到的数据X_lately
X = np.array(df.drop(['label'], 1))
X = preprocessing.scale(X)
# 上面生成的label列时留下的最后1%行的数据,这些行并没有label 数据,用作预测时用到的输入数据
X_lately = X[-forecast_out:]
X = X[:-forecast_out]
# 抛弃label列中为空的那些行
df.dropna(inplace=True)
y = np.array(df['label'])

from sklearn import model_selection, svm
from sklearn.linear_model import LinearRegression

# 先把X,y数据分成两部份,训练和测试,这里选取80%作为训练集,%20作为测试集
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)
# 生成线性回归对象
clf = LinearRegression(n_jobs=-1)
# 开始训练
clf.fit(X_train, y_train)
# 用测试数据评估准确性
accuracy = clf.score(X_test, y_test)
# 进行预测
foreca_set = clf.predict(X_lately)
print(foreca_set, accuracy)

import matplotlib.pyplot as plt
from matplotlib import style
import datetime

# 修改matplotlib样式
style.use('ggplot')
one_day = 86400
# 在df中新建Forecast列,用于存放预测结果的数据
df['Forecast'] = np.nan
# 取df最后一行的时间索引
last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
next_unix = last_unix + one_day
# 遍历预测结果,用它向df中追加行
for i in foreca_set:
    next_date = datetime.datetime.fromtimestamp(next_unix)
    next_unix += one_day
    # [np.nan  for _ in range(len(df.columns)-1)]生成不包含Forecast字段的列表
    # 而[i]是只包含Forecast字段的列表
    # 拼在一起组成新行,按日期追加到df下面
    df.loc[next_date] = [np.nan for _ in range(len(df.columns) - 1)] + [i]
# 绘图
df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

2.支持向量回归

#coding=utf-8
import quandl
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
import math
#Get the stock data
df = quandl.get("WIKI/GOOGL")
# Take a look at the data
# print(df.head())

 # 读取训练数据

# stock_code = '601318.XSHG'
# start_date = '2016-02-05'
# end_date = '2017-02-07'


# 只用到df中的下面几个字段
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
# 构造两个新列
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Close']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
# 真正用到的特征
df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
#df = get_price(stock_code, start_date, end_date, frequency='daily',skip_paused=False,fq='pre',fields=['open','high','low','close','money'])
print(df.head())

df = df[['Adj. Close']]
print(df.head())


# A variable for predicting 'n' days out into the future
forecast_out = 30 #'n=30' days
#Create another column (the target or dependent variable) shifted 'n' units up
df['Prediction'] = df[['Adj. Close']].shift(-forecast_out)
#print the new data set
print(df.tail())


### Create the independent data set (X)  #######
# Convert the dataframe to a numpy array
X = np.array(df.drop(['Prediction'],1))

#Remove the last 'n' rows
X = X[:-forecast_out]
# print(X)


### Create the dependent data set (y)  #####
# Convert the dataframe to a numpy array (All of the values including the NaN's)
y = np.array(df['Prediction'])
# Get all of the y values except the last 'n' rows
y = y[:-forecast_out]
# print(y)


# Split the data into 80% training and 20% testing
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Create and train the Support Vector Machine (Regressor)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_rbf.fit(x_train, y_train)

# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
svm_confidence = svr_rbf.score(x_test, y_test)
print("svm confidence: ", svm_confidence)


# Create and train the Linear Regression  Model
lr = LinearRegression()
# Train the model
lr.fit(x_train, y_train)

# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
lr_confidence = lr.score(x_test, y_test)
print("lr confidence: ", lr_confidence)


# Set x_forecast equal to the last 30 rows of the original data set from Adj. Close column
x_forecast = np.array(df.drop(['Prediction'],1))[-forecast_out:]
# print(x_forecast)

foreca_set = lr.predict(x_test)
# Print linear regression model predictions for the next 'n' days
lr_prediction = lr.predict(x_forecast)
print("linear regression model predictions for the next n days:\n",lr_prediction)
#将numpy数据存为csv
np.savetxt("lr_prediction.csv", lr_prediction, delimiter=',')

# Print support vector regressor model predictions for the next 'n' days
svm_prediction = svr_rbf.predict(x_forecast)
print("support vector regressor model predictions for the next n days:\n",svm_prediction)
#将numpy数据存为csv
np.savetxt("svm_prediction.csv", svm_prediction, delimiter=',')

#Original stock price
z = np.array(df['Adj. Close'])
print("Original stock price for the next n days:\n",z[-forecast_out:])
np.savetxt("z.csv", z[-forecast_out:], delimiter=',')

import matplotlib.pyplot as plt
from matplotlib import style
import datetime
style.use('ggplot')
one_day = 1
# 在df中新建Forecast列,用于存放预测结果的数据
df['Forecast'] = np.nan
# 取df最后一行的时间索引
last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
next_unix = last_unix + one_day
# 遍历预测结果,用它向df中追加行
for i in foreca_set:
    next_date = datetime.datetime.fromtimestamp(next_unix)
    next_unix += one_day
    # [np.nan  for _ in range(len(df.columns)-1)]生成不包含Forecast字段的列表
    # 而[i]是只包含Forecast字段的列表
    # 拼在一起组成新行,按日期追加到df下面
    df.loc[next_date] = [np.nan for _ in range(len(df.columns) - 1)] + [i]
# 绘图
df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

运行结果

1.基于线性回归
走势预测

预测值

准确性

2.SVR
走势预测
原始股价

在这里插入图片描述
准确性

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
股票预测是一个复杂的问题,需要考虑很多因素,比如历史数据、市场环境等,因此需要使用一些机器学习算法来进行预测。下面是一个简单的用 Python 实现的股票预测代码,该代码使用支持向量机(SVM)算法来进行预测: ```python # 导入必要的库 import pandas as pd import numpy as np from sklearn.svm import SVR from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error # 读取股票数据 df = pd.read_csv('stock_data.csv') # 数据预处理 df = df[['Open', 'High', 'Low', 'Close', 'Volume']] df['HL_PCT'] = (df['High'] - df['Low']) / df['Close'] * 100.0 df['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0 df = df[['Close', 'HL_PCT', 'PCT_change', 'Volume']] df.fillna(-99999, inplace=True) # 分离标签和特征 X = np.array(df.drop(['Close'], 1)) y = np.array(df['Close']) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 训练模型 clf = SVR(kernel='linear') clf.fit(X_train, y_train) # 预测并计算误差 y_pred = clf.predict(X_test) mse = mean_squared_error(y_test, y_pred) print("MSE:", mse) ``` 这段代码中,我们首先读取了股票数据,然后进行了一些预处理,包括计算了每日最高价、最低价、涨跌幅等指标。接着,我们使用 SVM 算法对数据进行训练,并在测试集上进行预测和误差计算。最后,输出了预测的均方误差(MSE)。 需要注意的是,这只是一个简单的示例,实际中还需要对数据进行更加细致的处理和特征工程,以提高预测的准确性。同时,股票预测是一个高风险的行为,建议在投资前仔细评估风险和收益。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值