Files
OpenBB/gamestonk_terminal/common/prediction_techniques/knn_view.py
jmaslek 7ba964d61e (Finally) add prediction abilities to crypto (#1004)
* Basic integration of crypto pred

* Add loading into crypto pred

* Change all pred _views to allow for other datetime than daily

* small mypy change

* Add pred menu description and catch if no coin is loaded.

Co-authored-by: James Maslek <james@jamess-mbp.myfiosgateway.com>
2021-12-01 23:20:24 +00:00

85 lines
2.1 KiB
Python

""" KNN Prediction View"""
__docformat__ = "numpy"
from typing import Union
import pandas as pd
from pandas.plotting import register_matplotlib_converters
from gamestonk_terminal.common.prediction_techniques.pred_helper import (
print_pretty_prediction,
plot_data_predictions,
)
from gamestonk_terminal.common.prediction_techniques import knn_model
register_matplotlib_converters()
# pylint:disable=too-many-arguments
def display_k_nearest_neighbors(
ticker: str,
data: Union[pd.DataFrame, pd.Series],
n_neighbors: int,
n_input_days: int,
n_predict_days: int,
test_size: float,
end_date: str = "",
no_shuffle: bool = True,
time_res: str = "",
):
"""Display predictions using knn
Parameters
----------
ticker : str
Stock data
data : Union[pd.DataFrame, pd.Series]
Data to use for ML
n_neighbors : int
Number of neighborns for knn
n_input_days : int
Length of input sequences
n_predict_days : int
Number of days to predict
test_size : float
Fraction of data for testing
end_date : str, optional
End date for backtesting, by default ""
no_shuffle : bool, optional
Flag to shuffle data randomly, by default True
time_res : str
Resolution for data, allowing for predicting outside of standard market days
"""
(
forecast_data_df,
preds,
y_valid,
y_dates_valid,
scaler,
) = knn_model.get_knn_model_data(
data, n_input_days, n_predict_days, n_neighbors, test_size, end_date, no_shuffle
)
if forecast_data_df.empty:
print("Issue performing data prep and prediction")
return
if time_res:
forecast_data_df.index = pd.date_range(
data.index[-1], periods=n_predict_days + 1, freq=time_res
)[1:]
print_pretty_prediction(forecast_data_df[0], data.values[-1])
plot_data_predictions(
data,
preds,
y_valid,
y_dates_valid,
scaler,
f"KNN Model with {n_neighbors} Neighbors on {ticker}",
forecast_data_df,
1,
time_res,
)
print("")