def main(): #model = load_model('test_models/stock_multb_nosmooth_step4_1525242340/seq_len_50/model-1.h5') model = load_model( 'D:\Source\Repos\StockPrediction\stock_single_nosmooth_1525405252\seq_len_50/model-1.h5' ) seq_len = 50 predict_len = 7 #date_ranges = [(datetime.date(2016,11,1),datetime.date(2017,4,3)),(datetime.date(2002,3,18),datetime.date(2002,8,14)),(datetime.date(2015,3,8),datetime.date(2015,8,5))] date_ranges = [(datetime.date(2015, 1, 1), datetime.date(2015, 6, 1)), (datetime.date(2016, 1, 1), datetime.date(2016, 6, 1)), (datetime.date(2017, 1, 1), datetime.date(2017, 6, 1))] test_data = [ dataload.load_data('daily_spx.csv', seq_len, normalise_window=True, smoothing=False, date_range=date_range, train=False) for date_range in date_ranges ] predictions = [ dataload.predict_sequences_multiple(model, test[0], seq_len, predict_len) for test in test_data ] scores = [ model.evaluate(test[0], test[1], verbose=0) for test in test_data ] for prediction_index in range(len(predictions)): for sequence_index in range(len(predictions[prediction_index])): predictions[prediction_index][ sequence_index] = dataload.denormalize_sequence( test_data[prediction_index][2][sequence_index * 7], predictions[prediction_index][sequence_index]) for test_data_index in range(len(test_data)): for y_index in range(len(test_data[test_data_index][1])): test_data[test_data_index][1][ y_index] = dataload.denormalize_point( test_data[test_data_index][2][y_index], test_data[test_data_index][1][y_index]) model_plot = [(predictions[0], '2015'), (predictions[1], '2016'), (predictions[2], '2017')] #model_plot = [(predictions[0], 'Bullish'), (predictions[1], 'Bearish'), (predictions[2], 'Neutral')] results_fname = 'test_singleb_nosmooth_byyear_{}'.format( int(datetime.datetime.now().timestamp())) os.makedirs(results_fname) plot_results_multiple(model_plot, [t[1] for t in test_data], predict_len, fig_path=results_fname + '/plots.pdf') with open(results_fname + "/score.txt", "w") as fout: for score in scores: pprint.pprint(score, fout)
def main(): svm_model_path = 'svm_smoothed_1530436270/model.sav' results_fname = 'test_svm_smooth_byyear_{}'.format( int(datetime.datetime.now().timestamp())) seq_len = 50 predict_len = 7 os.makedirs(results_fname) # Define date ranges date_ranges = [(datetime.date(2015, 1, 1), datetime.date(2015, 6, 1)), (datetime.date(2016, 1, 1), datetime.date(2016, 6, 1)), (datetime.date(2017, 1, 1), datetime.date(2017, 6, 1))] # Load data model = pickle.load(open(svm_model_path, 'rb')) test_data = [ dataload.load_data('daily_spx.csv', seq_len, normalise_window=True, smoothing=False, date_range=date_range, train=False) for date_range in date_ranges ] # Generate predictions #[[print(seq.shape) for seq in test_date_range[0]] for test_date_range in test_data] #predictions = [[np.asscalar(model.predict(seq.transpose())) for seq in test_date_range[0]] for test_date_range in test_data] predictions = [ dataload.predict_sequences_multiple(model, test[0], seq_len, predict_len) for test in test_data ] for prediction_index in range(len(predictions)): for sequence_index in range(len(predictions[prediction_index])): predictions[prediction_index][ sequence_index] = dataload.denormalize_sequence( test_data[prediction_index][2][sequence_index * 7], predictions[prediction_index][sequence_index]) for test_data_index in range(len(test_data)): for y_index in range(len(test_data[test_data_index][1])): test_data[test_data_index][1][ y_index] = dataload.denormalize_point( test_data[test_data_index][2][y_index], test_data[test_data_index][1][y_index]) # Save plot model_plot = [(predictions[0], '2015'), (predictions[1], '2016'), (predictions[2], '2017')] plot_results_multiple(model_plot, [t[1] for t in test_data], predict_len, fig_path=results_fname + '/plots.pdf')
model = top_models[i] callbacks = [] if (i == 0): callbacks = [early_stopping] else: callbacks = [early_stopping] model.fit(X_train, y_train, batch_size=model.batch_size, nb_epoch=epochs, validation_split=0.10, callbacks=[early_stopping, tensorboard]) fit_models.append(model) predictions = [ dataload.predict_sequences_multiple(model, X_test, seq_len, predict_len) for model in top_models ] scores = [ model.evaluate(X_test, y_test, verbose=0) for model in top_models ] # Save results os.makedirs(results_fname) folder_name = 'seq_len_{}'.format(seq_len) os.makedirs('{}/{}'.format(results_fname, folder_name)) results.to_csv('{0}/{1}/results.csv'.format(results_fname, folder_name)) top_model_plots = [(predictions[i], 'Model {}'.format(i + 1)) for i in range(len(predictions))] plot_results_multiple(top_model_plots,
def main(): # Original 4 pipelines model = load_model('single_cnn_gru_1542783564/seq_len_50/model-3.h5') # 2 New pipelines for Bidir GRU #model = load_model('../final_sem_code/single_cnn_bidir_gru_1548982892/seq_len_50/model-3.h5') seq_len = 50 predict_len = 7 date_ranges = [(datetime.date(2016, 1, 1), datetime.date(2016, 6, 1)), (datetime.date(2017, 1, 1), datetime.date(2017, 6, 1)), (datetime.date(2018, 1, 1), datetime.date(2018, 6, 1))] ''' date_ranges = [(datetime.date(2015,1,1),datetime.date(2015,12,31)), (datetime.date(2016,1,1),datetime.date(2016,12,31)), (datetime.date(2017,1,1),datetime.date(2017,12,31))] ''' #test_data = [dataload.load_data('daily_spx.csv', seq_len, normalise_window=True, smoothing=False, date_range=date_range, train=False) for date_range in date_ranges] test_data = [ dataload.load_data('../2018_data/Yahoo_2000_to_2018.csv', seq_len, normalise_window=True, smoothing=False, date_range=date_range, train=False) for date_range in date_ranges ] predictions = [ dataload.predict_sequences_multiple(model, test[0], seq_len, predict_len) for test in test_data ] scores = [ model.evaluate(test[0], test[1], verbose=0) for test in test_data ] for prediction_index in range(len(predictions)): for sequence_index in range(len(predictions[prediction_index])): predictions[prediction_index][ sequence_index] = dataload.denormalize_sequence( test_data[prediction_index][2][sequence_index * 7], predictions[prediction_index][sequence_index]) for test_data_index in range(len(test_data)): for y_index in range(len(test_data[test_data_index][1])): test_data[test_data_index][1][ y_index] = dataload.denormalize_point( test_data[test_data_index][2][y_index], test_data[test_data_index][1][y_index]) model_plot = [(predictions[0], '2016'), (predictions[1], '2017'), (predictions[2], '2018')] #model_plot = [(predictions[0], 'Mar 2002 to Aug 2002'), (predictions[1], 'Mar 2015 to Aug 2015'),(predictions[2], 'Jan 2016 to Apr 2017')] #model_plot = [(predictions[0], 'Bullish'), (predictions[1], 'Bearish'), (predictions[2], 'Neutral')] results_fname = 'test_single_cnn_bidir_gru_{}'.format( int(datetime.datetime.now().timestamp())) #os.makedirs(results_fname) plot_results_multiple(model_plot, [t[1] for t in test_data], predict_len, fig_path='plots_test/plots.pdf') with open('plots_test' + "/score.txt", "w") as fout: for score in scores: pprint.pprint(score, fout)
#lstm_model = lstm.build_model([1, seq_len, 100, 1]) model_layers = [1, 2, 3, 4] cnn_models = [ cnn_batchnorm_lstm.build_model([1, seq_len, 100, 1], x) for x in model_layers ] [model.fit( X_train, y_train, batch_size=512, nb_epoch=epochs, validation_split=0.05) for model in cnn_models ] #lstm_predictions = dataload.predict_sequences_multiple(lstm_model, X_test, seq_len, seq_len) cnn_predictions = [dataload.predict_sequences_multiple(model, X_test, seq_len, seq_len) for model in cnn_models] #predictions = dataload.predict_sequence_full(model, X_test, seq_len) #predictions = dataload.predict_point_by_point(model, X_test) #lstm_score = lstm_model.evaluate(X_test, y_test, verbose=0) cnn_scores = [model.evaluate(X_test, y_test, verbose=0) for model in cnn_models] #print('LSTM') #print(lstm_score) #print('Test loss: ', lstm_score[0]) #print('Test accuracy: ', lstm_score[1]) for l in model_layers: print('\nCNN ', l) print(cnn_scores[l-1])