Created
August 3, 2017 17:31
-
-
Save kamath/b51cbea869e27ca527dda2ac3e631152 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import numpy as np | |
| from numpy import newaxis | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| from pandas import datetime | |
| import math, time | |
| import itertools | |
| from sklearn import preprocessing | |
| import datetime | |
| from operator import itemgetter | |
| from sklearn.metrics import mean_squared_error | |
| from math import sqrt | |
| from keras.models import Sequential | |
| from keras.layers.core import Dense, Dropout, Activation | |
| from keras.layers.recurrent import LSTM | |
| import time | |
| def is_number(num): | |
| for a in str(num): | |
| if not a.isdigit() and a != '.': | |
| return 0 | |
| return float(num) | |
| start = time.time() | |
| stock_name = 'FB' | |
| url="http://www.google.com/finance/historical?q="+stock_name+"&startdate=Jul+12%2C+2013&enddate=Jul+31%2C+2017&num=30&ei=rCtlWZGSFN3KsQHwrqWQCw&output=csv" | |
| col_names = ['Date','Open','High','Low','Close','Volume'] | |
| stocks = pd.read_csv(url, header=0, names=col_names) | |
| df = pd.DataFrame(stocks) | |
| df.drop(df.columns[[0,3,5]], axis=1, inplace=True) | |
| df.tail() | |
| today = datetime.date.today() | |
| file_name = stock_name+'_stock_%s.csv' % today | |
| #df.to_csv(file_name) | |
| df['High'] = [is_number(a) for a in df['High']] | |
| df['Open'] = [is_number(a) for a in df['Open']] | |
| df['Close'] = [is_number(a) for a in df['Close']] | |
| print(df['Open'][0]) | |
| df['High'] = [a / 1000 for a in df['High']] | |
| for a in df['Open']: | |
| print(a) | |
| print(a/1000) | |
| df['Open'] = [a / 1000 for a in df['Open']] | |
| df['Close'] = [a / 1000 for a in df['Close']] | |
| df.head(5) | |
| stock = df[::-1] | |
| seq_len = 5 | |
| amount_of_features = len(stock.columns) | |
| data = stock.as_matrix() #pd.DataFrame(stock) | |
| print(data) | |
| sequence_length = seq_len + 1 | |
| result = [] | |
| for index in range(len(data) - sequence_length): | |
| result.append(data[index: index + sequence_length]) | |
| result = np.array(result) | |
| row = round(result.shape[0]) - 95 | |
| if result.shape[0] < 1000: | |
| row = round(result.shape[0]) - 7 | |
| train = result[:int(row), :] | |
| x_train = train[:, :-1] | |
| y_train = train[:, -1][:,-1] | |
| x_test = result[int(row):, :-1] | |
| y_test = result[int(row):, -1][:,-1] | |
| x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], amount_of_features)) | |
| x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], amount_of_features)) | |
| print("X_train", x_train.shape) | |
| print("y_train", y_train.shape) | |
| print("X_test", x_test.shape) | |
| print("y_test", y_test.shape) | |
| d = 0.2 | |
| model = Sequential() | |
| model.add(LSTM(128, input_shape=(x_train.shape[1], x_train.shape[2]), return_sequences=True)) | |
| model.add(Dropout(d)) | |
| model.add(LSTM(64, input_shape=(x_train.shape[1], x_train.shape[2]), return_sequences=True)) | |
| model.add(Dropout(d)) | |
| model.add(LSTM(32, input_shape=(x_train.shape[1], x_train.shape[2]), return_sequences=False)) | |
| model.add(Dropout(d)) | |
| model.add(Dense(16,init='uniform',activation='relu')) | |
| model.add(Dense(1,init='uniform',activation='relu')) | |
| model.compile(loss='mse',optimizer='adam') | |
| model.fit( | |
| x_train, | |
| y_train, | |
| batch_size=512, | |
| nb_epoch=300, | |
| validation_split=0.1, | |
| verbose=1) | |
| #trainScore = | |
| print(model.evaluate(x_train, y_train, verbose=0)) | |
| #print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0]))) | |
| #testScore = model.evaluate(x_test, y_test, verbose=0) | |
| #print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore[0], math.sqrt(testScore[0]))) | |
| p = model.predict(x_test) | |
| import matplotlib.pyplot as plt2 | |
| end = time.time() | |
| print(end - start) | |
| plt2.plot(p[5:],color='red', label='predicted') | |
| plt2.plot(y_test[:-5],color='blue', label='actual') | |
| plt2.legend(loc='upper left') | |
| plt.title(stock_name) | |
| plt2.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment