Based on the stock's closing price lstm forecast - python

References: based on the stock's closing price keras lstm forecast

Will get an error when importing MinMaxScaler "from import _arpack ImportError:. DLL load failed: Can not find the specified program."

#import datetime
import pandas as pd
import numpy as np
#from numpy import row_stack,column_stack
import tushare as ts
#import matplotlib
import matplotlib.pyplot as plt
#from matplotlib.pylab import date2num
#from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
#from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
from sklearn.preprocessing import MinMaxScaler
#https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#sphx-glr-auto-examples-preprocessing-plot-all-scaling-py
from keras.models import Sequential
from keras.layers import LSTM, Dense, Activation




df=ts.get_hist_data('601857',start='2016-06-15',end='2018-01-12')
dd=df[['open','high','low','close']]

#print(dd.values.shape[0])

dd1=dd .sort_index()

dd2=dd1.values.flatten()

dd3=pd.DataFrame(dd1['close'])

def load_data(df, sequence_length=10, split=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)
    scaler = MinMaxScaler()
    data_all = scaler.fit_transform(data_all)
    data = []
    for i in range(len(data_all) - sequence_length - 1):
        data.append(data_all[i: i + sequence_length + 1])
    = np.array reshaped_data (Data) .astype ( 'float64') 
    Model the Sequential = () 
    model.add (LSTM (input_dim =. 1, output_dim =. 6, return_sequences = True )) 
    # model.add (LSTM (. 6, input_dim =. 1, return_sequences = True))
    #np.random.shuffle(reshaped_data)
    # Of unified normalized x and y are not normalized 
    x = reshaped_data [:,: -1] 
    y = reshaped_data [:, -1] 
    split_boundary = int (reshaped_data.shape [0] * Split) 
    train_x = X [: split_boundary] 
    test_x = X [split_boundary:] 

    train_y = Y [: split_boundary] 
    test_y = Y [split_boundary:] 

    return train_x, train_y, test_x, test_y, Scaler 


DEF build_model (): 
    # input_dim is train_x entered last dimension, train_x the dimension (N_SAMPLES, time_steps, input_dim) 
    # 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'))

    model.compile(loss='mse', optimizer='rmsprop')
    return model


def train_model(train_x, train_y, test_x, test_y):
    model = build_model()

    try:
        model.fit(train_x, train_y, batch_size=512, nb_epoch=300, validation_split=0.1)
        predict = model.predict(test_x)
        predict = np.reshape(predict, (predict.size, ))
    except KeyboardInterrupt:
        print(predict)
        print(test_y)
    print(predict)
    print(test_y)
    try:
        fig = plt.figure(1)
        plt.plot(predict, 'r:')
        plt.plot(test_y, 'g-')
        plt.legend(['predict', 'true'])
    except Exception as e:
        print(e)
    return predict, test_y


if __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)
    train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
    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)
    predict_y = scaler.inverse_transform([[i] for i in predict_y])
    test_y = scaler.inverse_transform(test_y)
    fig2 = plt.figure(2)
    plt.plot(predict_y, 'g:')
    plt.plot(test_y, 'r-')
    plt.show()

  

  

Guess you like

Origin www.cnblogs.com/iupoint/p/10948315.html