python用keras库做个股票分析小程序
生活随笔
收集整理的這篇文章主要介紹了
python用keras库做个股票分析小程序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
分析的是中國癌股上證指數(shù)月線,分析半天的結(jié)果感覺無法達(dá)到股票預(yù)測,還不如直接用股票指標(biāo)實(shí)在.
也許本人能力有限,需要更高的數(shù)學(xué),編程和分析技術(shù).
不過我剛學(xué)python一星期,這個(gè)小程序就當(dāng)學(xué)習(xí)練手了.
# -*- coding: utf-8 -*- """ Created on Thu May 2 15:34:37 2019@author: Administrator """import numpy as np #科學(xué)計(jì)算數(shù)據(jù)分析庫,另起名為np import matplotlib.pyplot as plt #擅長畫二維圖曲線,股票均線,另起名為plt from sklearn.preprocessing import MinMaxScaler #機(jī)器學(xué)習(xí)庫,數(shù)據(jù)預(yù)處理,把數(shù)據(jù)定為0,1之間 from keras.models import Sequential #深度學(xué)習(xí)庫,建立模型,多網(wǎng)絡(luò)層線性堆疊順序模型 from keras.layers import LSTM, Dense, Activation #準(zhǔn)備用三種神經(jīng)網(wǎng)絡(luò)層,長短期記憶網(wǎng)絡(luò),全連接網(wǎng)絡(luò)層,激活層#from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY #import matplotlib import tushare as ts #接收中國股市數(shù)據(jù)另起名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 ''' import pandas #數(shù)據(jù)分析庫plt.rcParams['font.family'] = 'SimHei' ## 設(shè)置字體df=ts.get_k_data('sh000001',start='1990-12-29',ktype='m') #獲取上證指數(shù)上市以來的全部月線數(shù)據(jù) dd=df[['open','high','low','close']] #把DataFrame表的部分?jǐn)?shù)據(jù)列給新的對象#print(dd.values.shape[0])dd1=dd .sort_index() #把表按索引升序排序dd2=dd1.values.flatten() #列表返回表對象里的所有值,并把它們降成一維,變成等號左邊對象的一個(gè)列dd3=pandas.DataFrame(dd1['close']) #把A股收盤價(jià)一列賦值給新對象#下邊的函數(shù)把股票收盤價(jià)做預(yù)處理,把數(shù)據(jù)奕成0到1之間,擬合數(shù)據(jù)得到均值與方差,把收盤價(jià)轉(zhuǎn)換成神經(jīng)網(wǎng)絡(luò)能處理的數(shù)據(jù) def load_data(df, sequence_length=10, split=0.8): #自定義函數(shù),必須參數(shù)DataFrame表,默認(rèn)參數(shù)序列長度為10,數(shù)據(jù)拆分值0.8#df = pd.read_csv(file_name, sep=',', usecols=[1])#data_all = np.array(df).astype(float)data_all = np.array(df).astype(float) # 把df數(shù)據(jù)轉(zhuǎn)換為數(shù)組# 將數(shù)據(jù)縮放至給定的最小值與最大值之間,這里是0與1之間,數(shù)據(jù)預(yù)處理scaler = MinMaxScaler()data_all = scaler.fit_transform(data_all) #擬合數(shù)據(jù)得到均值和方差data = []#新數(shù)組for i in range(len(data_all) - sequence_length - 1):#數(shù)組長度減去序列長度減1得到循環(huán)長度data.append(data_all[i: i + sequence_length + 1])#把數(shù)據(jù)一條一條寫到data數(shù)組變量里reshaped_data = np.array(data).astype('float64') #把data數(shù)據(jù)轉(zhuǎn)換成float64數(shù)組#np.random.shuffle(reshaped_data)# 對x進(jìn)行統(tǒng)一歸一化,而y則不歸一化'''逗號前對行操作,逗號后對列操作,讀reshaped_data,冒號看作列,冒號左邊是指定從列左邊第幾位開始,冒號右邊是到多少列結(jié)束-1就是到列最后邊減1結(jié)束原來一列有11個(gè)數(shù)據(jù),現(xiàn)在變成一列有10個(gè)數(shù)據(jù),多的那個(gè)數(shù)據(jù)排到下一列'''x = reshaped_data[:, :-1]y = reshaped_data[:, -1]#還是對列操作,寫負(fù)1直接去掉10個(gè)一步,只有一個(gè)列了split_boundary = int(reshaped_data.shape[0] * split)#浮點(diǎn)數(shù)組有326個(gè),乘以0.8等于260train_x = x[: split_boundary]#冒號左邊為0,讀X從0行開始到260,讀取數(shù)據(jù)的前0.8test_x = x[split_boundary:]#從X的260行開始到最后一行,讀數(shù)據(jù)的后0.2train_y = y[: split_boundary]test_y = y[split_boundary:]#因?yàn)閄,Y數(shù)值是一樣的,所以上邊的train_X也等于train_yreturn train_x, train_y, test_x, test_y, scaler #最終函數(shù)返回5項(xiàng)處理結(jié)果def build_model():#建立神經(jīng)網(wǎng)絡(luò)模型# input_dim是輸入的train_x的最后一個(gè)維度,train_x的維度為(n_samples, time_steps, input_dim)model = Sequential()#建立順序模型,神經(jīng)網(wǎng)絡(luò)'''把LSTM長短期記憶網(wǎng)絡(luò)加到神經(jīng)網(wǎng)絡(luò),輸入維度1,輸出維度6,返回整個(gè)序列輸入維度1相當(dāng)于一行,就是一組股票收盤價(jià),輸出維度6,相當(dāng)于輸出6組數(shù)據(jù)'''model.add(LSTM(input_dim=1, output_dim=6, return_sequences=True))#添加LSTM網(wǎng)絡(luò)層#model.add(LSTM(6, input_dim=1, return_sequences=True))#model.add(LSTM(6, input_shape=(None, 1),return_sequences=True))"""#model.add(LSTM(input_dim=1, output_dim=6,input_length=10, return_sequences=True))#model.add(LSTM(6, input_dim=1, input_length=10, return_sequences=True))model.add(LSTM(6, input_shape=(10, 1),return_sequences=True))"""print(model.layers)#model.add(LSTM(100, return_sequences=True))#model.add(LSTM(100, return_sequences=True))model.add(LSTM(100, return_sequences=False))model.add(Dense(output_dim=1))model.add(Activation('linear'))#激活網(wǎng)絡(luò)層model.compile(loss='mse', optimizer='rmsprop')#編譯網(wǎng)絡(luò)層return modeldef train_model(train_x, train_y, test_x, test_y):#訓(xùn)練神經(jīng)網(wǎng)絡(luò),返回訓(xùn)練結(jié)果model = build_model()#用前邊自定義的神經(jīng)網(wǎng)絡(luò)模型建立對象#怕訓(xùn)練時(shí)出錯(cuò),提前用try做異常處理try:#用fit訓(xùn)練,前兩個(gè)參數(shù)內(nèi)容一樣,第三參數(shù)經(jīng)過多少樣本更新權(quán)重,第四參數(shù)訓(xùn)練次數(shù),第五參數(shù)指定訓(xùn)練集0.1的數(shù)據(jù)當(dāng)驗(yàn)證集model.fit(train_x, train_y, batch_size=512, nb_epoch=300, validation_split=0.1)predict = model.predict(test_x)#獲得輸入數(shù)據(jù)的對應(yīng)輸出,預(yù)測結(jié)果,test和predict都是后0.2的數(shù)據(jù),用于當(dāng)自我驗(yàn)證predict = np.reshape(predict, (predict.size, ))#重新排列數(shù)組predictexcept KeyboardInterrupt:print('predict出錯(cuò)了',predict)print('test_y出錯(cuò)了',test_y)print('predict結(jié)果',predict)print('test_y結(jié)果',test_y)try:fig = plt.figure(figsize=(14,6))#創(chuàng)建并設(shè)置畫布尺寸plt.plot(predict, 'r:')#畫均線,設(shè)置顏色plt.plot(test_y, 'g-')plt.legend(['分析完數(shù)據(jù)', '原始數(shù)據(jù)'])#在畫布寫上哪個(gè)是原始數(shù)據(jù)哪個(gè)是神經(jīng)網(wǎng)絡(luò)分析完的數(shù)據(jù)except Exception as e:print(e)return predict, test_yif __name__ == '__main__':#train_x, train_y, test_x, test_y, scaler = load_data('international-airline-passengers.csv')train_x, train_y, test_x, test_y, scaler =load_data(dd3, sequence_length=10, split=0.8)#股票收盤價(jià)做預(yù)處理,分成4種數(shù)據(jù)train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))#重新排列數(shù)組test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1))predict_y, test_y = train_model(train_x, train_y, test_x, test_y)#收盤價(jià)被分成4種數(shù)據(jù)輸入神經(jīng)網(wǎng)絡(luò)訓(xùn)練,得到兩條分析數(shù)據(jù)predict_y = scaler.inverse_transform([[i] for i in predict_y])#將標(biāo)準(zhǔn)化后的數(shù)據(jù)轉(zhuǎn)換為原始數(shù)據(jù)。test_y = scaler.inverse_transform(test_y)fig2 = plt.figure(figsize=(14,6)) #設(shè)置畫布大小plt.plot(predict_y, 'g:')#把神經(jīng)網(wǎng)絡(luò)分析結(jié)果畫出來plt.plot(test_y, 'r-')#test_y只做了預(yù)處理,沒用神經(jīng)網(wǎng)絡(luò)分析,相當(dāng)于收盤價(jià)原始數(shù)據(jù),圖片畫出來以后看到,神經(jīng)網(wǎng)絡(luò)分析的數(shù)據(jù)慢于原始數(shù)據(jù)plt.legend(['分析完數(shù)據(jù)', '原始數(shù)據(jù)'])#在畫布寫上哪個(gè)是原始數(shù)據(jù)哪個(gè)是神經(jīng)網(wǎng)絡(luò)分析完的數(shù)據(jù)#plt.plot(df.close)plt.show()總結(jié)
以上是生活随笔為你收集整理的python用keras库做个股票分析小程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 寒江独钓学习笔记 -- 第四章 Hook
- 下一篇: 互联网-3互联网思维的应用