import numpy as np import tensorflow as tf from tensorflow.contrib import rnn import matplotlib.pyplot as plt from tensorflow.contrib.learn.python.learn.estimators.estimator import SKCompat from matplotlib import style import pandas as pd Data = pd.read_excel ( ' yen - RMB * .xls ' , header = 0, = sheetname ' Sheet1 ' ) data.head() time = data.iloc[:,0].tolist() data = data.iloc[:,4].tolist() style.use('ggplot') plt.figure(figsize=(16,9)) plt.rcParams [ ' font.sans serif- ' ] = ' SimHei ' # # set the font to display Chinese SimHei plt.rcParams [ ' axes.unicode_minus ' ] = False # # Set the normal display symbols plt.title ( " raw data " ) plt.plot(time,data) plt.show() def data_processing(raw_data,scale=True): if scale == True: return (raw_data-np.mean(raw_data))/np.std(raw_data)#标准化 else: return (raw_data-np.min(raw_data))/(np.max(raw_data)-np.min(raw_data))#极差规格化 '' ' Set the number of hidden neurons ' '' HIDDEN_SIZE = 32 '' ' Set number of hidden layers ' '' NUM_LAYERS = 1 '' ' Arranged in a time step of folding steps recursive ' '' TIMESTEPS = 12 '' ' Provided training rounds ' '' TRAINING_STEPS = 2000 '' ' Set training batch size ' '' BATCH_SIZE = 64 def generate_data(seq): X- = [] # initialization input sequence X- Y = [] # initialization sequence output Y '' ' To generate a time sequence type coherent set of samples, each corresponding to a row in the X specified step length input sequences, each row corresponding to a target value Y in the ratio a lag of X ' '' for i in range(len(seq) - TIMESTEPS - 1): X.append ([SEQ [I: I + timesteps]]) # from the input sequence of the first, and so the sampling step uninterrupted Y.append ([SEQ [I + timesteps]]) # corresponding to each sequence X It lags a sequence of values return np.array(X, dtype=np.float32), np.array(Y, dtype=np.float32) '' ' Defined LSTM cell assembly, which will be updated during the training parameters ' '' def LstmCell(): lstm_cell = rnn.BasicLSTMCell(HIDDEN_SIZE, state_is_tuple=True) # return lstm_cell '' ' Defined LSTM model ' '' DEF lstm_model (X-, Y): '' ' to LSTM cell as defined above definition is based multilayer stack LSTM, where only one layer ' '' cell = rnn.MultiRNNCell([LstmCell() for _ in range(NUM_LAYERS)]) '' ' Which has been played by the stacked unit LSTM converted to dynamically updatable during training unit LSTM ' '' output, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) '' ' To generate each hidden layer neuron means each according to the number of predefined ' '' output = tf.reshape(output, [-1, HIDDEN_SIZE]) '' ' Is calculated by total activation function without connection layer linear regression, and the data compressed into one-dimensional array ' '' predictions = tf.contrib.layers.fully_connected(output, 1, None) '' ' Shape unify the predicted value and the real value of ' '' labels = tf.reshape(y, [-1]) predictions = tf.reshape(predictions, [-1]) '' ' Defined loss function, where the normal mean square error ' '' loss = tf.losses.mean_squared_error(predictions, labels) '' ' Defined parameters optimizer ' '' train_op = tf.contrib.layers.optimize_loss(loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad', learning_rate=0.6) '' ' Returns the predicted value, and the loss function optimizer ' '' return predictions, loss, train_op '' ' Loading tf Imitation sklearn training mode module ' '' learn = tf.contrib.learn '' ' Initialization LSTM models, and saved to the working directory to facilitate incremental learning ' '' regressor = SKCompat(learn.Estimator(model_fn=lstm_model, model_dir='Models/model_1')) '' ' To the original scale data scaled ' '' data = data_processing(data) '' ' The 7000 data as training samples ' '' train_X, train_y = generate_data(data[0:7000]) '' ' And the remaining data as the test samples ' '' test_X, test_y = generate_data(data[6989:-1]) regressor.fit(train_X, train_y, batch_size=BATCH_SIZE, steps=TRAINING_STEPS) '' ' Using the model has been trained LSTM, generating a prediction value corresponding to all the test set ' '' predicted = np.array([pred for pred in regressor.predict(test_X)]) '' ' Drawn before denormalized true and predicted values comparison chart ' '' plt.plot (Predicted, label = ' predictive value ' ) plt.plot (test_y, label = ' true value ' ) plt.title ( ' previous denormalized ' ) plt.legend () plt.show() '' ' Custom function denormalized ' '' def scale_inv(raw_data,scale=True): '' ' Is read and converted into original data List ' '' Data = pd.read_excel ( ' yen - RMB * .xls ' , header = 0, = sheetname ' Sheet1 ' ) data = data.iloc[:, 4].tolist() if scale == True: return raw_data*np.std(data)+np.mean(data) else: return raw_data*(np.max(data)-np.min(data))+np.min(data) sp = scale_inv(predicted) sy = scale_inv (test_y) '' ' True and predicted values after comparison chart drawing denormalized ' '' plt.figure(figsize=(12,8)) plt.plot (SP, label = ' predictive value ' ) plt.plot (SY, label = ' true value ' ) plt.title ( ' after denormalized ' ) plt.legend () plt.show() p = plt.figure(figsize=(16, 9)) ax = p.add_subplot(1, 2, 1) plt.plot(time[7002:-1], sp) plt.plot(time[0:7000], scale_inv(data[0:7000])) plt.title ( ' predictive picture ' ) ax = p.add_subplot(1, 2, 2) plt.plot(time, scale_inv(data)) plt.title ( ' original ' ) plt.show() acc_num = 0 for i in range(len(sy)): if (abs(sp[i]-sy[i])) < 0.05: acc_num += 1 Print ( ' accuracy rate: ' , acc_num / len (SP)) day=20 l = len (data) for i in range(day): P=[] P.append([data[l-TIMESTEPS-1+i:l-1+i]]) P=np.array(P, dtype=np.float32) pre=regressor.predict(P) data=np.append(data,pre) to = data [len (data) -day: only the (data) +1 ] print(pre) # Anti normalized value print(scale_inv(pre)) # Forecast FIG P = plt.figure () plt.plot (scale_inv (on)) plt.show()
Yen exchange rate against large data analysis and forecasting [complete code]
Guess you like
Origin www.cnblogs.com/lingxingyitiao/p/12102096.html
Recommended
Ranking