Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to use CNN for stock price prediction but my code does not seem to work, what do I need to change or

I am trying to use CNN for stock price prediction but my code does not seem to work, what do I need to change or add? 

This above is my code. I tried to edit it but does not seem to be working. I am suspecting that I did not format my dataset well but I am new to this field so I do not know what should I do to my codes such that it will fit in. I hope you guys can enlighten me on this, Thank you!

I encountered errors like : ''IndexError: index 2264 is out of bounds for axis 0 with size 2264'' and '' ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 800 but received input with shape [None, 480]''

 import math import numpy as np import pandas as pd import pandas_datareader as pdd from sklearn.preprocessing import MinMaxScaler from keras.layers import Dense, Dropout, Activation, LSTM, Convolution1D, MaxPooling1D, Flatten from keras.models import Sequential import matplotlib.pyplot as plt df = pdd.DataReader('AAPL', data_source='yahoo', start='2012-01-01', end='2020-12-31') data = df.filter(['Close']) dataset = data.values len(dataset) training_data_size = math.ceil(len(dataset)*0.7) training_data_size scaler = MinMaxScaler(feature_range=(0,1)) scaled_data = scaler.fit_transform(dataset) scaled_data train_data = scaled_data[0:training_data_size,:] x_train = [] y_train = [] for i in range(60, len(train_data)): x_train.append(train_data[i-60:i, 0]) y_train.append(train_data[i,0]) if i<=60: print(x_train) print(y_train) x_train, y_train = np.array(x_train), np.array(y_train) x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) x_train.shape model = Sequential() model.add(Convolution1D(64, 3, input_shape= (100,4), padding='same')) model.add(MaxPooling1D(pool_size=2)) model.add(Convolution1D(32, 3, padding='same')) model.add(MaxPooling1D(pool_size=2)) model.add(Flatten()) model.add(Dense(1)) model.add(Activation('linear')) model.summary() model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=50, epochs=50, validation_data = (X_test, y_test), verbose=2) test_data = scaled_data[training_data_size-60: , :] x_test = [] y_test = dataset[training_data_size: , :] for i in range(60, len(test_data)): x_test.append(test_data[i-60:i, 0]) x_test = np.array(x_test) x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1)) predictions = model.predict(x_test) predictions = scaler.inverse_transform(predictions) rsme = np.sqrt(np.mean((predictions - y_test)**2)) rsme train = data[:training_data_size] valid = data[training_data_size:] valid['predictions'] = predictions plt.figure(figsize=(16,8)) plt.title('PFE') plt.xlabel('Date', fontsize=18) plt.ylabel('Close Price in $', fontsize=18) plt.plot(train['Close']) plt.plot(valid[['Close', 'predictions']]) plt.legend(['Train', 'Val', 'predictions'], loc='lower right') plt.show import numpy as np y_test, predictions = np.array(y_test), np.array(predictions) mape = (np.mean(np.abs((predictions - y_test) / y_test))) * 100 accuracy = 100 - mape print(accuracy)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions