def get_accuracy(model, data): y_test = data["y_test"] X_test = data["X_test"] y_pred = model.predict(X_test) y_test = np.squeeze(data["column_scaler"]["adjclose"].inverse_transform(np.expand_dims(y_test, axis=0))) y_pred = np.squeeze(data["column_scaler"]["adjclose"].inverse_transform(y_pred)) y_pred = list(map(lambda current, future: int(float(future) > float(current)), y_test[:-LOOKUP_STEP], y_pred[LOOKUP_STEP:])) y_test = list(map(lambda current, future: int(float(future) > float(current)), y_test[:-LOOKUP_STEP], y_test[LOOKUP_STEP:])) return accuracy_score(y_test, y_pred)
def plot_graph(model, data): y_test = data["y_test"] X_test = data["X_test"] y_pred = model.predict(X_test) y_test = np.squeeze(data["column_scaler"]["adjclose"].inverse_transform(np.expand_dims(y_test, axis=0))) y_pred = np.squeeze(data["column_scaler"]["adjclose"].inverse_transform(y_pred)) plt.plot(y_test[-200:], c='b') plt.plot(y_pred[-200:], c='r') plt.xlabel("Days") plt.ylabel("Price") plt.legend(["Actual Price", "Predicted Price"]) plt.show()
def plot_graph(model, data, name): y_test = data["y_test"] X_test = data["X_test"] y_pred = model.predict(X_test) y_test = np.squeeze(data["column_scaler"]["adjclose"].inverse_transform(np.expand_dims(y_test, axis=0))) y_pred = np.squeeze(data["column_scaler"]["adjclose"].inverse_transform(y_pred)) plt.close() plt.plot(y_test[-200:], c='b') plt.plot(y_pred[-200:], c='r') plt.title(name) plt.xlabel("Days") plt.ylabel("Price") plt.legend(["Actual Price", "Predicted Price"]) plt.savefig('/root/PycharmProjects/stock-advisor/images/results.png') plt.show()
def get_final_df(model, data): """ This function takes the `model` and `data` dict to construct a final dataframe that includes the features along with true and predicted prices of the testing dataset """ # if predicted future price is higher than the current, # then calculate the true future price minus the current price, to get the buy profit buy_profit = lambda current, true_future, pred_future: true_future - current if pred_future > current else 0 # if the predicted future price is lower than the current price, # then subtract the true future price from the current price sell_profit = lambda current, true_future, pred_future: current - true_future if pred_future < current else 0 X_test = data["X_test"] y_test = data["y_test"] # perform prediction and get prices y_pred = model.predict(X_test) if SCALE: y_test = np.squeeze( data["column_scaler"]["adjclose"].inverse_transform( np.expand_dims(y_test, axis=0))) y_pred = np.squeeze( data["column_scaler"]["adjclose"].inverse_transform(y_pred)) test_df = data["test_df"] # add predicted future prices to the dataframe test_df[f"adjclose_{LOOKUP_STEP}"] = y_pred # add true future prices to the dataframe test_df[f"true_adjclose_{LOOKUP_STEP}"] = y_test # sort the dataframe by date test_df.sort_index(inplace=True) final_df = test_df # add the buy profit column final_df["buy_profit"] = list( map(buy_profit, final_df["adjclose"], final_df[f"adjclose_{LOOKUP_STEP}"], final_df[f"true_adjclose_{LOOKUP_STEP}"]) # since we don't have profit for last sequence, add 0's ) # add the sell profit column final_df["sell_profit"] = list( map(sell_profit, final_df["adjclose"], final_df[f"adjclose_{LOOKUP_STEP}"], final_df[f"true_adjclose_{LOOKUP_STEP}"]) # since we don't have profit for last sequence, add 0's ) return final_df