日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

基于keras 的神经网络股价预测模型

發(fā)布時(shí)間:2025/4/5 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于keras 的神经网络股价预测模型 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
SELECT * FROM `heidao-market.mafia1_ods.game_log_status_snapshot` where timestamp >"2020-02-18 09:30:22" and timestamp < "2020-02-18 10:40:22" and operation= 'buy_giftbag'

? ? ? ?? ? ? ?這些年從網(wǎng)上的各位大牛那學(xué)到很多,本著開(kāi)源開(kāi)放的精神,今天我決定開(kāi)源我量化交易代碼。輸入股票代碼,和訓(xùn)練的數(shù)據(jù)時(shí)間,自動(dòng)預(yù)測(cè)股票未來(lái)的走勢(shì)。。。。。。。。。。。。。。。。。。

#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 7 17:55:28 2019@author: lg """from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY #from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc #import matplotlib import tushare as ts import pandas as pd import matplotlib.pyplot as plt from matplotlib.pylab import date2num import datetime import numpy as np from pandas import DataFrame from numpy import row_stack,column_stack from mpl_finance import candlestick_ochl df=ts.get_hist_data('601857',start='2019-01-15',end='2019-05-07') dd=df[['open','high','low','close']] from mpl_finance import candlestick_ochl,candlestick_ohlc #print(dd.values.shape[0])dd1=dd .sort_index()dd2=dd1.values.flatten()g1=dd2[::-1]g2=g1[0:120]g3=g2[::-1]gg=DataFrame(g3)gg.T.to_excel('gg.xls') #dd3=pd.DataFrame(dd2) #dd3.T.to_excel('d8.xls') g=dd2[0:140] for i in range(dd.values.shape[0]-34):s=dd2[i*4:i*4+140]g=row_stack((g,s))fg=DataFrame(g)print(fg) fg.to_excel('fg.xls') #-*- coding: utf-8 -*- #建立、訓(xùn)練多層神經(jīng)網(wǎng)絡(luò),并完成模型的檢驗(yàn) #from __future__ import print_function import pandas as pdinputfile1='fg.xls' #訓(xùn)練數(shù)據(jù) testoutputfile = 'test_output_data.xls' #測(cè)試數(shù)據(jù)模型輸出文件 data_train = pd.read_excel(inputfile1) #讀入訓(xùn)練數(shù)據(jù)(由日志標(biāo)記事件是否為洗浴) data_mean = data_train.mean() data_std = data_train.std() data_train1 = (data_train-data_mean)/5 #數(shù)據(jù)標(biāo)準(zhǔn)化y_train = data_train1.iloc[:,120:140].as_matrix() #訓(xùn)練樣本標(biāo)簽列 x_train = data_train1.iloc[:,0:120].as_matrix() #訓(xùn)練樣本特征 #y_test = data_test.iloc[:,4].as_matrix() #測(cè)試樣本標(biāo)簽列from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activationmodel = Sequential() #建立模型 model.add(Dense(input_dim = 120, output_dim = 240)) #添加輸入層、隱藏層的連接 model.add(Activation('relu')) #以Relu函數(shù)為激活函數(shù) model.add(Dense(input_dim = 240, output_dim = 120)) #添加隱藏層、隱藏層的連接 model.add(Activation('relu')) #以Relu函數(shù)為激活函數(shù) model.add(Dense(input_dim = 120, output_dim = 120)) #添加隱藏層、隱藏層的連接 model.add(Activation('relu')) #以Relu函數(shù)為激活函數(shù) model.add(Dense(input_dim = 120, output_dim = 20)) #添加隱藏層、輸出層的連接 model.add(Activation('sigmoid')) #以sigmoid函數(shù)為激活函數(shù) #編譯模型,損失函數(shù)為binary_crossentropy,用adam法求解 model.compile(loss='mean_squared_error', optimizer='adam')model.fit(x_train, y_train, nb_epoch = 100, batch_size = 8) #訓(xùn)練模型 model.save_weights('net.model') #保存模型參數(shù)inputfile2='gg.xls' #預(yù)測(cè)數(shù)據(jù) pre = pd.read_excel(inputfile2) pre_mean = data_mean[0:120] pre_std = pre.std() pre1 = (pre-pre_mean)/10 #數(shù)據(jù)標(biāo)準(zhǔn)化 #pre1 = (pre-pre_mean)/pre.std() #數(shù)據(jù)標(biāo)準(zhǔn)化 pre2 = pre1.iloc[:,0:120].as_matrix() #預(yù)測(cè)樣本特征 r = pd.DataFrame(model.predict(pre2)) rt=r*10+data_mean[120:140].as_matrix() print(rt.round(2))rt.to_excel('rt.xls') #print(r.values@data_train.iloc[:,116:120].std().values+data_mean[116:120].as_matrix())a=list(df.index[0:-1])b=a[0]c= datetime.datetime.strptime(b,'%Y-%m-%d')d = date2num(c)c1=[d+i+1 for i in range(5)] c2=np.array([c1])r1=rt.values.flatten() r2=r1[0:4] for i in range(4):r3=r1[i*4+4:i*4+8]r2=row_stack((r2,r3))c3=column_stack((c2.T,r2)) r5=DataFrame(c3)if len(c3) == 0:raise SystemExitfig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2)#ax.xaxis.set_major_locator(mondays) #ax.xaxis.set_minor_locator(alldays) #ax.xaxis.set_major_formatter(mondayFormatter) #ax.xaxis.set_minor_formatter(dayFormatter)#plot_day_summary(ax, quotes, ticksize=3) #candlestick_ochl(ax, c3, width=0.6, colorup='r', colordown='g') candlestick_ohlc(ax, c3, width=0.5, colorup='r', colordown='g')ax.xaxis_date() ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')ax.grid(True) #plt.title('000002') plt.show()

github 代碼

總結(jié)

以上是生活随笔為你收集整理的基于keras 的神经网络股价预测模型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。