From f0572dc5bcc93c0d203efe71c024973acc070e34 Mon Sep 17 00:00:00 2001 From: didier Date: Sat, 13 Feb 2021 19:15:59 +0000 Subject: [PATCH] Fix KNN using adjusted close data --- prediction_techniques/knn.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/prediction_techniques/knn.py b/prediction_techniques/knn.py index b0838209e02..2e42303b31c 100644 --- a/prediction_techniques/knn.py +++ b/prediction_techniques/knn.py @@ -28,19 +28,19 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock): return # Split training data - stock_x, stock_y = splitTrain.split_train(df_stock['4. close'].values, ns_parser.n_inputs, ns_parser.n_days, ns_parser.n_jumps) + stock_x, stock_y = splitTrain.split_train(df_stock['5. adjusted close'].values, ns_parser.n_inputs, ns_parser.n_days, ns_parser.n_jumps) # Machine Learning model knn = neighbors.KNeighborsRegressor(n_neighbors=ns_parser.n_neighbors) knn.fit(stock_x, stock_y) # Prediction data - l_predictions = knn.predict(df_stock['4. close'].values[-ns_parser.n_inputs:].reshape(1, -1))[0] - l_pred_days = get_next_stock_market_days(last_stock_day=df_stock['4. close'].index[-1], n_next_days=ns_parser.n_days) + l_predictions = knn.predict(df_stock['5. adjusted close'].values[-ns_parser.n_inputs:].reshape(1, -1))[0] + l_pred_days = get_next_stock_market_days(last_stock_day=df_stock['5. adjusted close'].index[-1], n_next_days=ns_parser.n_days) df_pred = pd.Series(l_predictions, index=l_pred_days, name='Price') # Plotting - plt.plot(df_stock.index, df_stock['4. close'], lw=2) + plt.plot(df_stock.index, df_stock['5. adjusted close'], lw=2) plt.title(f"{ns_parser.n_neighbors}-Nearest Neighbors on {s_ticker} - {ns_parser.n_days} days prediction") plt.xlim(df_stock.index[0], get_next_stock_market_days(df_pred.index[-1], 1)[-1]) plt.xlabel('Time') @@ -48,7 +48,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock): plt.grid(b=True, which='major', color='#666666', linestyle='-') plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) - plt.plot([df_stock.index[-1], df_pred.index[0]], [df_stock['4. close'].values[-1], df_pred.values[0]], lw=1, c='tab:green', linestyle='--') + plt.plot([df_stock.index[-1], df_pred.index[0]], [df_stock['5. adjusted close'].values[-1], df_pred.values[0]], lw=1, c='tab:green', linestyle='--') plt.plot(df_pred.index, df_pred, lw=2, c='tab:green') plt.axvspan(df_stock.index[-1], df_pred.index[-1], facecolor='tab:orange', alpha=0.2) xmin, xmax, ymin, ymax = plt.axis()