model = save_load.load_model(path_name) sc_output = MinMaxScaler(feature_range = (0,1)) sc_output.fit(input_set[:,output_col]) # prediction using train data pred_train_scaled = model.predict(X_train) # rescaling for predictions ( train data ) train_predict = sc_output.inverse_transform(pred_train_scaled) train_actual = sc_output.inverse_transform(y_train) print('R2 Score : ', r2_score(train_actual, train_predict)) print('MSE Score : ', mean_squared_error(train_actual, train_predict)) plot.time_series_plot(train_actual, train_predict, 'red', 'blue', 'actual_open', \ 'predicted_open', 'days', 'price', 'Neural Network (single attribute - train data)') pred_test_scaled = model.predict(X_test) # rescaling for predictions ( test data ) test_predict = sc_output.inverse_transform(pred_test_scaled) #test = sc_output.inverse_transform(testing_set_scaled) test_actual = sc_output.inverse_transform(y_test) print('R2 Score : ', r2_score(test_actual, test_predict)) print('MSE Score : ', mean_squared_error(test_actual, test_predict)) plot.time_series_plot(test_actual, test_predict, 'red', 'blue', 'actual_open', \ 'predicted_open', 'days', 'price', 'Neural Network (single attribute - test data)')
# loading the model path_name_open = "./model/trading_model/open" model_open = save_load.load_model(path_name_open) # prediction using train set output_open_scaled = model_open.predict(X_test_open) # rescaling for predictions ( train data ) output_open = sc_output.inverse_transform(output_open_scaled) actual_open = sc_output.inverse_transform(y_test_open) # analysis using opening price print('R2 Score : ', r2_score(actual_open, output_open)) print('MSE Score : ', mean_squared_error(actual_open, output_open)) plot.time_series_plot(actual_open, output_open, 'red', 'blue', 'actual', \ 'predicted', 'days', 'open price', 'Neural Network (trading)') # loading the model path_name_close = "./model/trading_model/close" model_close = save_load.load_model(path_name_close) # prediction using train set output_close_scaled = model_close.predict(X_test_close) # rescaling for predictions ( train data ) output_close = sc_output.inverse_transform(output_close_scaled) actual_close = sc_output.inverse_transform(y_test_close) # analysis using closing price print('R2 Score : ', r2_score(actual_close, output_close)) print('MSE Score : ', mean_squared_error(actual_close, output_close))
# actual output test_actual = sc_output.inverse_transform(y_test) # creating dataframe for storing result results = pd.DataFrame(columns=['feature_col', 'r2_score', 'mse_score']) count = 0 for i in range(no_of_feature): for j in range(len(combination[i])): feature = np.array(combination[i][j]) # loading the model model = save_load.load_model(path_name + "/" + str(count)) # prediction pred_test_scaled = model.predict(X_test[:, :, feature]) test_predict = sc_output.inverse_transform(pred_test_scaled) # evaluation model_accuracy_r2 = r2_score(test_actual, test_predict) model_accuracy_mse = mean_squared_error(test_actual, test_predict) print("feature: {}\n r2_score: {}\n mse_score: {}\n".format(feature, \ model_accuracy_r2, model_accuracy_mse)) plot.time_series_plot(test_actual, test_predict, 'red', 'blue', \ 'actual_close', 'predicted_close', 'days', 'price',\ 'Neural Network (multiple attributes - train data)') results.loc[count] = [feature, model_accuracy_r2, model_accuracy_mse] count = count + 1 results.to_excel("./result/feature_importance/result.xlsx") # r2_score: 0.9837941366548775 mse_score: 1830.8363851761196
testing_set_scaled = sc_input.transform(testing_set) # creating 3d tensor X_test, y_test = tensor.create_tensor(testing_set_scaled, neuron, input_col,\ output_col, no_of_feature) for optim in optimiser: for func in activation: # loading model model = save_load.load_model(path_name + "/" + str(count)) # prediction using test data pred_test_scaled = model.predict(X_test) # rescaling the predictions test_predict = sc_output.inverse_transform(pred_test_scaled) test_actual = sc_output.inverse_transform(y_test) # score model_accuracy_r2 = r2_score(test_actual, test_predict) model_accuracy_mse = mean_squared_error(test_actual, test_predict) print("r2 : ", model_accuracy_r2) print("mse : ", model_accuracy_mse) plot.time_series_plot(test_actual, test_predict, 'red', 'blue', 'actual_close',\ 'predicted_close', 'days', 'price', 'Neural Network') count = count + 1 results.loc[count] = [neuron, optim, func, model_accuracy_r2, \ model_accuracy_mse] results.to_excel("./model/hyperParaModels/hyperparameter_optim.xlsx") # r2 : 0.9772057086220985, mse : 2575.1554940470696
poly_model = LinearRegression() poly_model = poly_model.fit(X_poly, train_close) ############################################################################### # prediction using training data X_poly_train = poly_feat.fit_transform(train_dates) train_predict = poly_model.predict(X_poly_train) train_predict = np.array(train_predict).reshape(-1, 1) # printing scores print("R2 Score : ", r2_score(train_close, train_predict)) print("MSE Score : ", mean_squared_error(train_close, train_predict)) print("RMSE Score : ", sqrt(mean_squared_error(train_close, train_predict))) plot.time_series_plot(train_close, train_predict, 'red', 'blue', 'actual_close', \ 'predicted_close', 'days', 'price', 'Regression Model (traing data)') # prediction for testing data X_poly_test = poly_feat.fit_transform(test_dates) test_predict = poly_model.predict(X_poly_test) test_predict = np.array(test_predict).reshape(-1, 1) # printing scores print("R2 Score : ", r2_score(test_close, test_predict)) print("MSE Score : ", mean_squared_error(test_close, test_predict)) print("RMSE Score : ", sqrt(mean_squared_error(test_close, test_predict))) plot.time_series_plot(test_close, test_predict, 'red', 'blue', 'actual_close', \ 'predicted_close', 'days', 'price', 'Regression Model (test data)') # plotting error
for i in range(timestep, timestep + prediction_days): X_test = [] X_test.append(X_test_scaled[i - timestep:i]) X_test = np.array(X_test).reshape(1, -1) X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], no_of_feature)) output.append(model.predict(X_test)) X_test_scaled = np.append(X_test_scaled, output) output = sc.inverse_transform(np.array(output).reshape(-1, 1)) actual = np.array(y_test) plot.time_series_plot(y_test, output, 'red', 'blue', 'actual_close', \ 'predicted_close', 'days', 'price', 'Neural Network (multiple attributes - test data)') # plotting error error_list = [] for i in range(len(actual)): error = ((actual[i] - output[i]) / actual[i]) * 100 error_list.append(error) ############################################################################### # saving the results in excel format date = pd.DataFrame(df_test['Date'].iloc[:prediction_days]) actual_price_df = pd.DataFrame(actual).round(3) predict_price_df = pd.DataFrame(output).round(3) error_df = pd.DataFrame(error_list).round(3)