0
CONST_TRAINTING_SEQUENCE_LENGTH = 12

CONST_TESTING_CASES = 5


def dataNormalization(data):
    return [(datum - data[0]) / data[0] for datum in data]


def dataDeNormalization(data, base):
    return [(datum + 1) * base for datum in data]


def getDeepLearningData(ticker):
    # Step 1. Load data
    data = pandas.read_csv('/Users/yindeyong/Desktop/Django_Projects/pythonstock/data/Intraday/' + ticker + '.csv')[
        'close'].tolist()

    # Step 2. Building Training data
    dataTraining = []
    for i in range(len(data) - CONST_TESTING_CASES * CONST_TRAINTING_SEQUENCE_LENGTH):
        dataSegment = data[i:i + CONST_TRAINTING_SEQUENCE_LENGTH + 1]
        dataTraining.append(dataNormalization(dataSegment))

    dataTraining = numpy.array(dataTraining)
    numpy.random.shuffle(dataTraining)
    X_Training = dataTraining[:, :-1]
    Y_Training = dataTraining[:, -1]

    # Step 3. Building Testing data
    X_Testing = []
    Y_Testing_Base = []
    for i in range(CONST_TESTING_CASES, 0, -1):
        dataSegment = data[-(i + 1) * CONST_TRAINTING_SEQUENCE_LENGTH:-i * CONST_TRAINTING_SEQUENCE_LENGTH]
        Y_Testing_Base.append(dataSegment[0])
        X_Testing.append(dataNormalization(dataSegment))

    Y_Testing = data[-CONST_TESTING_CASES * CONST_TRAINTING_SEQUENCE_LENGTH:]

    X_Testing = numpy.array(X_Testing)
    Y_Testing = numpy.array(Y_Testing)

    # Step 4. Reshape for deep learning
    X_Training = numpy.reshape(X_Training, (X_Training.shape[0], X_Training.shape[1], 1))
    X_Testing = numpy.reshape(X_Testing, (X_Testing.shape[0], X_Testing.shape[1], 1))

    return X_Training, Y_Training, X_Testing, Y_Testing, Y_Testing_Base


def predictLSTM(ticker):
    # Step 1. Load data

    X_Training, Y_Training, X_Testing, Y_Testing, Y_Testing_Base = getDeepLearningData(ticker)

    # Step 2. Build model
    model = Sequential()

    model.add(LSTM(
        input_shape=1,
        output_dim=50,
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        200,
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(output_dim=1))
    model.add(Activation('linear'))

    model.compile(lose='mse', optimizer='rmsprop')

    # Step 3. Train model
    model.fit(X_Training, Y_Training,
              batch_size=512,
              nb_epoch=5,
              validation_split=0.05)


predictLSTM(ticker='MRIN')

I got an error:

File "/Users/yindeyong/Desktop/Django_Projects/envs/stockenv/lib/python3.6/site-packages/keras/engine/base_layer.py", line 147, in init batch_size,) + tuple(kwargs['input_shape']) TypeError: 'int' object is not iterable

I have tried to change input_shape=1, to input_shape=(1,),and then got another error:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2

2 Answers 2

1

LSTMs are recurrent netowrks to work with sequences

Sequences must have length and features, your input shape must contain these two: input_shape=(length, features).

Your data must also be shaped accordingly, with (sequences, length, features).

For variable lengths, you may use input_shape=(None,features).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi friend,can you help me with this question stackoverflow.com/questions/53971563/…
Hi Daniel, could you please go to this page and vote for making "rnn" a synonym of "recurren-neural-network" (of course, if you are agreed with this synonym proposal)? Thanks.
1

You cannot pass input_shape an integer, it needs to be an iterable, eg (1,). It looks like your X_training is of the wrong shape then. You must reshape it such that it fits the input_shape.

3 Comments

Thank you Jonathan,I have update my code and question,please have a look.Thank you so much!
Its hard to say without actually knowing your csv. Try to load the data that you want into a numpy array and look at its shape. You should see a different shape there. Then look at the np.reshape function how you can change it.
Hi friend,can you help me with this question stackoverflow.com/questions/53971563/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.