-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add working train and predict recipes
- Loading branch information
1 parent
6e62ff8
commit 4f6ebe9
Showing
5 changed files
with
255 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pathlib import Path | ||
from config_model import * | ||
from models import KNNModel | ||
from dataset import DataSplitter, TimeSeriesScaler, TimeSeriesFormatter | ||
from utils import setup_logging, predicitons_to_df | ||
|
||
inference_date = INFERENCE_INPUT_DATE | ||
inference_file = file_processed_input | ||
|
||
lb, ph = (MODEL_PARAMS["lb"], MODEL_PARAMS["ph"]) | ||
# Set up logging | ||
logging = setup_logging("predict.log") | ||
|
||
if __name__ == "__main__": | ||
|
||
data_object = DataSplitter(inference_file) | ||
X_formatted = data_object.df | ||
|
||
time_series_object = TimeSeriesScaler( | ||
continous_features, | ||
categorical_features, | ||
other_columns, | ||
target_as_autoregressive_feature, | ||
target_column, | ||
) | ||
scaler = time_series_object.load_scaler("artifacts/minmax_scaler.gz") | ||
scaled_test = time_series_object.scaler_transform(X_formatted) | ||
|
||
series_formatter_obj = TimeSeriesFormatter( | ||
lb, ph, static_features, dynamic_features, True, True | ||
) | ||
|
||
W_test, X_test, z_test = series_formatter_obj.format_data(scaled_test) | ||
X_test = TimeSeriesFormatter.reshape_x(X_test) | ||
|
||
traffic_model = KNNModel() | ||
traffic_model.load_model("artifacts/knn_model") | ||
|
||
y_test_hat = traffic_model.predict_model(X_test) | ||
|
||
df_test = predicitons_to_df(ph, z_test, y_test_hat) | ||
df_test.to_csv( | ||
Path("..") / "predictions" / f"knn_{INFERENCE_PREDICTION_DATE}.csv", index=False | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from config_model import * | ||
from utils import setup_logging | ||
from models import KNNModel, EvaluationMetrics | ||
from dataset import DataSplitter, TimeSeriesScaler, TimeSeriesFormatter | ||
|
||
# Set up logging | ||
logging = setup_logging("train.log") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
data_object = DataSplitter(data_path) | ||
X_formatted = data_object.df | ||
|
||
for lb, ph in [(MODEL_PARAMS["lb"], MODEL_PARAMS["ph"])]: | ||
det_ids = data_object.get_groups | ||
|
||
seed = CONFIG["seed"] | ||
validation_prop = CONFIG["validation_proportion"] | ||
test_prop = CONFIG["test_proportion"] | ||
|
||
data_object.split_groups(seed, validation_prop, test_prop) | ||
|
||
X_formatted_train, X_formatted_val, X_formatted_test = data_object.split_data() | ||
|
||
time_series_object = TimeSeriesScaler( | ||
continous_features, | ||
categorical_features, | ||
other_columns, | ||
target_as_autoregressive_feature, | ||
target_column, | ||
) | ||
|
||
(X_formatted_train, X_formatted_val, X_formatted_test) = [ | ||
time_series_object.copy_target_column(df) | ||
for df in (X_formatted_train, X_formatted_val, X_formatted_test) | ||
] | ||
|
||
scaler = time_series_object.scaler_fit("minmax", X_formatted_train) | ||
|
||
(scaled_train, scaled_val, scaled_test) = [ | ||
time_series_object.scaler_transform(df) | ||
for df in (X_formatted_train, X_formatted_val, X_formatted_test) | ||
] | ||
|
||
series_formatter_obj = TimeSeriesFormatter( | ||
lb, ph, static_features, dynamic_features, True, False | ||
) | ||
|
||
W_train, X_train, y_train, z_train = series_formatter_obj.format_data( | ||
scaled_train | ||
) | ||
W_val, X_val, y_val, z_val = series_formatter_obj.format_data(scaled_val) | ||
|
||
W_test, X_test, y_test, z_test = series_formatter_obj.format_data(scaled_test) | ||
|
||
logging.info(f"Column order: {scaled_train.columns}") | ||
|
||
lookback_timesteps = lb | ||
prediction_horizon = ph | ||
|
||
X_train = TimeSeriesFormatter.reshape_x(X_train) | ||
X_val = TimeSeriesFormatter.reshape_x(X_val) | ||
X_test = TimeSeriesFormatter.reshape_x(X_test) | ||
|
||
optimal_k = 8 | ||
|
||
traffic_model = KNNModel( | ||
n_neighbors=optimal_k, weights="uniform", algorithm="kd_tree", p=2 | ||
) | ||
traffic_model.train_model(X_train, y_train) | ||
|
||
y_train_hat = traffic_model.predict_model(X_train) | ||
train_rmse = EvaluationMetrics(y_train, y_train_hat).rmse() | ||
print("RMSE on Train Set:", train_rmse) | ||
|
||
y_val_hat = traffic_model.predict_model(X_val) | ||
val_rmse = EvaluationMetrics(y_val, y_val_hat).rmse() | ||
print("RMSE on Validation Set:", val_rmse) | ||
|
||
y_test_hat = traffic_model.predict_model(X_test) | ||
test_rmse = EvaluationMetrics(y_test, y_test_hat).rmse() | ||
print("RMSE on Test Set:", test_rmse) | ||
|
||
traffic_model.save_model("artifacts/knn_model") | ||
time_series_object.save_scaler("artifacts/minmax_scaler.gz") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters