import os
import time
import math
import numpy as np
import pandas as pd
import tushare as ts
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dropout,Dense,SimpleRNN
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error,mean_absolute_error
INTnpseed=19740425
def FUNCgetstockdata(STRcode,STRtype,STRbegindate,STRenddate):
FILEhandle=ts.get_k_data(STRcode,ktype=STRtype,start=STRbegindate,end=STRenddate)
FILEname="./"+STRcode+".csv"
FILEhandle.to_csv(FILEname)
def FUNCgetyyyymmdd():
STRtoday=time.strftime("%Y%m%d",time.gmtime())
STRyyyymmdd=STRtoday[0:4]+"-"+STRtoday[4:6]+"-"+STRtoday[6:]
return STRyyyymmdd
def FUNCrnnnet(STRcode):
FUNCgetstockdata(STRcode,"D","2000-01-01",FUNCgetyyyymmdd())
FILEname="./"+STRcode+".csv"
STOCKLIST=pd.read_csv(FILEname)
train_set=STOCKLIST.iloc[0:int(len(STOCKLIST)*0.7),2:3]
test_set =STOCKLIST.iloc[int(len(STOCKLIST)*0.7): ,2:3]
sc=MinMaxScaler(feature_range=(0,1))
train_set_scaled=sc.fit_transform(train_set)
test_set =sc.transform(test_set)
x_train=[]
y_train=[]
x_test =[]
y_test =[]
for i in range(60,len(train_set_scaled)):
x_train.append(train_set_scaled[i-60:i,0])
y_train.append(train_set_scaled[i,0])
np.random.seed(INTnpseed)
np.random.shuffle(x_train)
np.random.seed(INTnpseed)
np.random.shuffle(y_train)
np.random.seed(INTnpseed)
x_train,y_train=np.array(x_train),np.array(y_train)
x_train=np.reshape(x_train,(x_train.shape[0],60,1))
for i in range(60,len(test_set)):
x_test.append(test_set[i-60:i,0])
y_test.append(test_set[i,0])
x_test,y_test=np.array(x_test),np.array(y_test)
x_test=np.reshape(x_test,(x_test.shape[0],60,1))
model=tf.keras.Sequential([
SimpleRNN(80,return_sequences=True),
Dropout(0.2),
SimpleRNN(100),
Dropout(0.2),
Dense(1)])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.01),
loss="mean_squared_error",
metrics=["sparse_categorical_accuracy"])
checkpoint_save_path="./checkpoint.stock.rnn/stock.rnn.ckpt"
if os.path.exists(checkpoint_save_path+".index"):
print("--------------------加载模型--------------------")
model.load_weights(checkpoint_save_path)
cp_callback=tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True,
monitor="val_loss")
history=model.fit(
x_train,
y_train,
batch_size=128,
epochs=100,
validation_data=(x_test,y_test),
validation_freq=1,
callbacks=[cp_callback])
model.summary()
print(model.trainable_variables)
file=open('./weights.stock.rnn.txt', 'w')
for v in model.trainable_variables:
file.write(str(v.name) + '\n')
file.write(str(v.shape) + '\n')
file.write(str(v.numpy()) + '\n')
file.close()
acc =history.history['sparse_categorical_accuracy']
loss =history.history['loss']
val_loss=history.history['val_loss']
plt.rcParams['font.sans-serif'] = ['simhei']
plt.plot(acc, label=u'训练准确率')
plt.plot(loss, label=u'训练错误率')
plt.plot(val_loss,label=u'验证错误率')
plt.title(u'RNN网络训练和验证错误率')
plt.legend()
plt.show()
predicted_stock_price=model.predict(x_test)
predicted_stock_price=sc.inverse_transform(predicted_stock_price)
real_stock_price=sc.inverse_transform(test_set[60:])
plt.plot(real_stock_price,color="red",label=u"实际股价")
plt.plot(predicted_stock_price,color="blue",label=u"预测股价")
plt.title(u"RNN网络股价预测")
plt.xlabel(u"时间")
plt.ylabel(u"股价")
plt.legend()
plt.show()
mse =mean_squared_error(predicted_stock_price,real_stock_price)
rmse=math.sqrt(mean_squared_error(predicted_stock_price,real_stock_price))
mae =mean_absolute_error(predicted_stock_price,real_stock_price)
print("均方误差:{:>0.6f},均方根误差:{:>0.6f},平均绝对误差:{:>0.6f}".format(mse,rmse,mae))
def FUNCusernnnet(STRcode):
FUNCgetstockdata(STRcode,"D","2000-01-01",FUNCgetyyyymmdd())
FILEname="./"+STRcode+".csv"
STOCKLIST=pd.read_csv(FILEname)
train_set=STOCKLIST.iloc[len(STOCKLIST)-60:,2:3]
sc=MinMaxScaler(feature_range=(0,1))
train_set_scaled=sc.fit_transform(train_set)
x_train=[]
for i in range(60,len(train_set_scaled)+1):
x_train.append(train_set_scaled[i-60:i,0])
x_train=np.array(x_train)
x_train=np.reshape(x_train,(x_train.shape[0],60,1))
model=tf.keras.Sequential([
SimpleRNN(80,return_sequences=True),
Dropout(0.2),
SimpleRNN(100),
Dropout(0.2),
Dense(1)])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.01),
loss="mean_squared_error",
metrics=["sparse_categorical_accuracy"])
checkpoint_save_path="./checkpoint.stock.rnn/stock.rnn.ckpt"
if os.path.exists(checkpoint_save_path+".index"):
print("--------------------加载模型--------------------")
model.load_weights(checkpoint_save_path)
cp_callback=tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True,
monitor="val_loss")
predicted_stock_price=model.predict(x_train)
predicted_stock_price=sc.inverse_transform(predicted_stock_price)
print("RNN网络预测"+STRcode+"股价为:"+str(predicted_stock_price))
time.sleep(5)
while True:
os.system("clear")
print("股票预测程序RNN网络>>>>>>>>")
print(" L600888............训练600888新疆众和数据")
print(" P600888............预测600888新疆众和估价")
print(" L600289............训练600289亿阳信通数据")
print(" P600289............预测600289亿阳信通估价")
print(" L002208............训练002208合肥城建数据")
print(" P002208............预测002208合肥城建估价")
print(" Quit...............退出系统")
STRinput=input(" >>>>>>>>请输入选择项:")
STRinput=STRinput.upper()
if STRinput=="L600888":
FUNCrnnnet("600888")
elif STRinput=="P600888":
FUNCusernnnet("600888")
elif STRinput=="L600289":
FUNCrnnnet("600289")
elif STRinput=="P600289":
FUNCusernnnet("600289")
elif STRinput=="L002208":
FUNCrnnnet("002208")
elif STRinput=="P002208":
FUNCusernnnet("002208")
elif STRinput=="QUIT":
break
else:
continue