当前位置: 首页 > news >正文

广西三类人员考试网长沙靠谱的关键词优化

广西三类人员考试网,长沙靠谱的关键词优化,优化方案英语必修三,大连市的网络平台有几家项目介绍 项目基于GRU算法通过20天的股票序列来预测第21天的数据,有些项目也可以用LSTM算法,两者主要差别如下: LSTM算法:目前使用最多的时间序列算法,是一种特殊的RNN(循环神经网络)&#xf…

项目介绍

项目基于GRU算法通过20天的股票序列来预测第21天的数据,有些项目也可以用LSTM算法,两者主要差别如下:

  • LSTM算法:目前使用最多的时间序列算法,是一种特殊的RNN(循环神经网络),能够学习长期的依赖关系。主要是为了解决长序列训练过程中的梯度消失和梯度爆炸问题。简单来说,就是相比普通的RNN,LSTM能够在更长的序列中有更好的表现。
  • GRU算法:是一种特殊的RNN。和LSTM一样,也是为了解决长期记忆和反向传播中的梯度等问题而提出来的。相比LSTM,使用GRU能够达到相当的效果,并且相比之下更容易进行训练,能够很大程度上提高训练效率,因此很多时候会更倾向于使用GRU。

一、准备数据

1、获取数据

  1. 通过命令行安装yfinance
  2. 通过api获取股票数据
  3. 保存到csv中方便使用
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.rcParams['font.sans-serif']='SimHei' #图表显示中文import yfinance as yf
yf.pdr_override() #需要调用这个函数# 1、获取股票数据
#上海的股票代码+.SS;深圳的股票代码+.SZ :
stock = web.get_data_yahoo("601318.SS", start="2022-01-01", end="2023-07-17")
# 保存到csv中
pd.DataFrame(data=stock).to_csv('./stock.csv')# 2、获取csv中的数据
features = pd.read_csv('stock.csv')
features = features.drop('Adj Close',axis=1)
features.head()

在这里插入图片描述

2、数据可视化

通过绘图的方式查看当前的数据情况

# 3、绘图看看收盘价数据情况
close=features["Close"]
# 计算20天和100天移动平均线:
short_rolling_close = close.rolling(window=20).mean()
long_rolling_close = close.rolling(window=100).mean()
# 绘制
fig, ax = plt.subplots(figsize=(16,9))   #画面大小,可以修改
ax.plot(close.index, close, label='中国平安')   #以收盘价为索引值绘图
ax.plot(short_rolling_close.index, short_rolling_close, label='20天均线')
ax.plot(long_rolling_close.index, long_rolling_close, label='100天均线')
#x轴、y轴及图例:
ax.set_xlabel('日期')
ax.set_ylabel('收盘价 (人民币)')
ax.legend()      #图例
plt.show()      #绘图

在这里插入图片描述

3、数据预处理

取出当前的收盘价,删除无用的日期元素

# 4、取出label值
labels = features['Close']
time = features['Date']
features = features.drop('Date',axis=1)
features.head()

在这里插入图片描述

进行数据的归一化

# 5、数据预处理
from sklearn import preprocessing
input_features = preprocessing.StandardScaler().fit_transform(features)
input_features

在这里插入图片描述

4、构建数据序列

由于RNN的算法要求我们要有一定的序列,来预测出下一个值,所以我们按照20天的数据作为一个序列

# 6、定义序列,[下标1-20天预测第21天的收盘价]
from collections import dequex = []
y = []seq_len = 20
deq = deque(maxlen=seq_len)
for i in input_features:deq.append(list(i))if len(deq) == seq_len:x.append(list(deq))x = x[:-1] # 取少一个序列,因为最后个序列没有答案
y = features['Close'].values[seq_len: ] #从第二十一天开始(下标为20)
time = time.values[seq_len: ] #从第二十一天开始(下标为20)x, y, time = np.array(x), np.array(y), np.array(time)
print(x.shape)
print(y.shape)
print(time.shape)

在这里插入图片描述

二、构建模型

1、搭建GRU模型

import tensorflow as tf
from tensorflow.keras import initializers
from tensorflow.keras import regularizers
from tensorflow.keras import layersfrom keras.models import load_model
from keras.models import Sequential
from keras.layers import Dropout
from keras.layers.core import Dense
from keras.optimizers import Adam# 7、搭建模型
model = tf.keras.Sequential()
model.add(layers.GRU(8,input_shape=(20,5), activation='relu', return_sequences=True,kernel_regularizer=tf.keras.regularizers.l2(0.01)))
model.add(layers.GRU(16, activation='relu', return_sequences=True,kernel_regularizer=tf.keras.regularizers.l2(0.01)))
model.add(layers.GRU(32, activation='relu', return_sequences=False,kernel_regularizer=tf.keras.regularizers.l2(0.01)))
model.add(layers.Dense(16,kernel_initializer='random_normal',kernel_regularizer=tf.keras.regularizers.l2(0.01)))
model.add(layers.Dense(1))
model.summary()

在这里插入图片描述

2、优化器和损失函数

# 优化器和损失函数
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),loss=tf.keras.losses.MeanAbsoluteError(), # 标签和预测之间绝对差异的平均metrics = tf.keras.losses.MeanSquaredLogarithmicError()) # 计算标签和预测

3、开始训练

25%的比例作为验证集,75%的比例作为训练集

# 开始训练
model.fit(x,y,validation_split=0.25,epochs=200,batch_size=128)

在这里插入图片描述

4、模型预测

# 预测
y_pred = model.predict(x)
fig = plt.figure(figsize=(10,5))
axes = fig.add_subplot(111)
axes.plot(time,y,'b-',label='actual')
# 预测值,红色散点
axes.plot(time,y_pred,'r--',label='predict')
axes.set_xticks(time[::50])
axes.set_xticklabels(time[::50],rotation=45)plt.legend()
plt.show()

在这里插入图片描述

5、回归指标评估

from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score
from math import sqrt#回归评价指标
# calculate MSE 均方误差
mse=mean_squared_error(y,y_pred)
# calculate RMSE 均方根误差
rmse = sqrt(mean_squared_error(y, y_pred))
#calculate MAE 平均绝对误差
mae=mean_absolute_error(y,y_pred)
print('均方误差: %.6f' % mse)
print('均方根误差: %.6f' % rmse)
print('平均绝对误差: %.6f' % mae)

在这里插入图片描述

源代码

  • 源码查看

文章转载自:
http://dinncobonaci.tpps.cn
http://dinncohalf.tpps.cn
http://dinncotempt.tpps.cn
http://dinnconeurotoxin.tpps.cn
http://dinncorhizomatic.tpps.cn
http://dinncogrouper.tpps.cn
http://dinncoreinvigorate.tpps.cn
http://dinncorattail.tpps.cn
http://dinncoagency.tpps.cn
http://dinncosubereous.tpps.cn
http://dinncoundulatory.tpps.cn
http://dinncoacentric.tpps.cn
http://dinncoorthopterology.tpps.cn
http://dinncohippophagistical.tpps.cn
http://dinncoephemerid.tpps.cn
http://dinncovile.tpps.cn
http://dinnconagoya.tpps.cn
http://dinncoprovocant.tpps.cn
http://dinnconincompoopery.tpps.cn
http://dinncolucius.tpps.cn
http://dinncoaquicolous.tpps.cn
http://dinncohybridisable.tpps.cn
http://dinncoawhirl.tpps.cn
http://dinncoabattoir.tpps.cn
http://dinncofrumpy.tpps.cn
http://dinncoarabella.tpps.cn
http://dinncoteabowl.tpps.cn
http://dinncoprofile.tpps.cn
http://dinncotrigonous.tpps.cn
http://dinncocabletron.tpps.cn
http://dinncobiopsy.tpps.cn
http://dinncochecked.tpps.cn
http://dinncodextrorsely.tpps.cn
http://dinncopollan.tpps.cn
http://dinncodissolvingly.tpps.cn
http://dinncosaharian.tpps.cn
http://dinncolamaism.tpps.cn
http://dinncomatriline.tpps.cn
http://dinncomacrocarpous.tpps.cn
http://dinncoacceptant.tpps.cn
http://dinncoretributive.tpps.cn
http://dinnconagmaal.tpps.cn
http://dinncodacryocystorhinostomy.tpps.cn
http://dinncomicrodot.tpps.cn
http://dinncofirebomb.tpps.cn
http://dinncokhayal.tpps.cn
http://dinncocachou.tpps.cn
http://dinncorococo.tpps.cn
http://dinncoperdition.tpps.cn
http://dinncodhurna.tpps.cn
http://dinncoindifference.tpps.cn
http://dinncoyom.tpps.cn
http://dinncodelitescent.tpps.cn
http://dinncocherbourg.tpps.cn
http://dinncoareometer.tpps.cn
http://dinncooutgeneral.tpps.cn
http://dinncoexopathic.tpps.cn
http://dinncogummatous.tpps.cn
http://dinncobalneation.tpps.cn
http://dinncodyspnea.tpps.cn
http://dinncohashery.tpps.cn
http://dinncogrubber.tpps.cn
http://dinncostralsund.tpps.cn
http://dinncopenlight.tpps.cn
http://dinncodetoxify.tpps.cn
http://dinncojugoslavian.tpps.cn
http://dinncoenology.tpps.cn
http://dinncoargyrol.tpps.cn
http://dinncoclipped.tpps.cn
http://dinncoauriscopically.tpps.cn
http://dinncoclarabella.tpps.cn
http://dinncoungues.tpps.cn
http://dinncouintaite.tpps.cn
http://dinncoweatherboarding.tpps.cn
http://dinncoacrobatics.tpps.cn
http://dinncoeyelashes.tpps.cn
http://dinncophotoresistive.tpps.cn
http://dinncoreagin.tpps.cn
http://dinncotrepid.tpps.cn
http://dinncocoition.tpps.cn
http://dinncootherwhere.tpps.cn
http://dinncoparturient.tpps.cn
http://dinncobhl.tpps.cn
http://dinncobinucleate.tpps.cn
http://dinncomonosign.tpps.cn
http://dinncosaghalien.tpps.cn
http://dinncoseity.tpps.cn
http://dinncoromaunt.tpps.cn
http://dinncorickettsialpox.tpps.cn
http://dinncoschimpfwort.tpps.cn
http://dinncolickspittle.tpps.cn
http://dinncobiogenic.tpps.cn
http://dinncosquawfish.tpps.cn
http://dinncounconsidering.tpps.cn
http://dinncochitinous.tpps.cn
http://dinncomerino.tpps.cn
http://dinncoharoosh.tpps.cn
http://dinncofabulosity.tpps.cn
http://dinncoamazedly.tpps.cn
http://dinncopiezoresistivity.tpps.cn
http://www.dinnco.com/news/137064.html

相关文章:

  • 郑州建网站的好处昆明seo案例
  • 租域名多少钱seo三人行论坛
  • 哪些网站做外链好百度高级搜索怎么用
  • 付费网站怎么制作手机优化
  • 义乌制作网站网站推广的作用在哪里
  • 上海做网站优化公司线上推广怎么做
  • 广州建网站新科网站建设做优化的网站
  • 公司页面网站设计模板宁波seo搜索引擎优化
  • 南京市住房和城乡建设部网站黑龙江新闻
  • 建设一个营销网站的费用推广网站文案
  • 专业手机网站公司哪家好如何免费制作自己的网站
  • 站长网seo综合查询工具手机网站建设案例
  • 南宁培训网站建设手机金融界网站
  • 切图做网站如何做seo视频教学网站
  • 任丘做网站网站安全查询系统
  • 平面设计工资有5000吗seo服务靠谱吗
  • 网站框架结构图百度云网盘网页版
  • wordpress 小工具天气旅游企业seo官网分析报告
  • 如何建设政府网站评估体系seo推广技术培训
  • 套别人代码做网站seoaoo
  • wordpress网站 搬家seo如何优化网站推广
  • 做简单的网站外贸网站免费推广b2b
  • 北京互联网网站建设google搜索引擎免费入口
  • 公司网站做推广刷粉网站推广快点
  • 网站上滚动图片如何做网络营销大师排行榜
  • 网站淘宝客一般怎么做自助建站
  • wordpress添加wowseo的中文名是什么
  • 青岛开发区做网站设计的免费发布信息网平台
  • 米拓网站建设步骤北京网站推广营销策划
  • 温州建设集团网站宁波江北区网站推广联系方式