This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Show the valid and predicted prices | |
valid | |
#This compares between the "Close" and "Predictions" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Plot the data | |
train = data[:training_data_len] | |
valid = data[training_data_len:] | |
valid["Predictions"] = predictions | |
#Visualize the data | |
plt.figure(figsize=(16,8)) | |
plt.title("Model") | |
plt.xlabel("Data", fontsize=18) | |
plt.ylabel("Close Price USD ($)", fontsize=18) | |
plt.plot(train["Close"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Evaluate the model: Getting the root square error (RMSE) | |
rmse = np.sqrt( np.mean( predictions - y_test )**2 ) | |
rmse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Get the model predicted price values | |
predictions = model.predict(x_test) | |
predictions = scaler.inverse_transform(predictions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Convert the data to a numpy array | |
x_test = np.array(x_test)#Reshape the data | |
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1 )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Create the testing data set | |
#Create a new array containing scaled values from index 1543 to 2003 | |
test_data = scaled_data[training_data_len - 60: , :] | |
#Create the data sets x_test and y_test | |
x_test = [] | |
y_test = dataset[training_data_len:, :] | |
for i in range(60, len(test_data)): | |
x_test.append(test_data[i-60:i, 0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Train the model | |
model.fit(x_train, y_train, batch_size=1, epochs=5)#Batch size the number of Batch per training, while epochs is the number of Iteration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Compile the model | |
model.compile(optimizer="adam", loss="mean_squared_error") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Build the LSTM model | |
model = Sequential() | |
model.add(LSTM(50, return_sequences=True, input_shape= (x_train.shape[1], 1)))#50 means the no of input neurons | |
model.add(LSTM(50, return_sequences= False)) | |
model.add(Dense(25)) | |
model.add(Dense(1))# Final output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reshape the data to a 3 dimensional shape | |
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) | |
x_train.shape | |
#Now you'll notice it a 3 dimensional shape |