diff --git a/src/training/decision_tree/main.py b/src/training/decision_tree/main.py index 1d134eb..37c82d2 100644 --- a/src/training/decision_tree/main.py +++ b/src/training/decision_tree/main.py @@ -38,33 +38,58 @@ def create_split(x, y, game_ids): if __name__ == "__main__": - model_path = "C:/Users/Laurent/Desktop/chess-engine/src/training/decision_tree/cache/model.joblib" + model_path = "./src/training/decision_tree/cache/model.joblib" if os.path.exists(model_path): model = Model.load(model_path) else: - x, y, game_ids = preprocessor.run("C:/Users/Laurent/Desktop/chess-engine/dataset/v1/output.csv") + x, y, game_ids = preprocessor.run("./dataset/output.csv") split = create_split(x, y, game_ids) + + trained_model = [] + + is_hyperparameter_tuning = True + for depth in range(1, 10): + for leafs in range(1, int(depth * 5)): + print(f"Training model with depth={depth} and leafs={leafs}") - depth = 35 - leafs = int(depth * 3.5) + model = Model( + tree=RandomForestRegressor( + n_estimators=25, + max_depth=depth, + max_features=0.35, + min_samples_leaf=leafs, + random_state=42 + ), + feature_names=preprocessor.get_feature_names(), + train_test_split=split + ) - model = Model( - tree=RandomForestRegressor( - n_estimators=25, - max_depth=depth, - max_features=0.35, - min_samples_leaf=leafs, - random_state=42 - ), - feature_names=preprocessor.get_feature_names(), - train_test_split=split - ) - - model.train() - model.evaluate() + model.train() + [train_mae, train_mse, test_mae, test_mse] = model.evaluate() + + # Verify it's not overfitting + if test_mae > 1.5 * train_mae: + print("Model is overfitting, skipping saving...") + else: + trained_model.append({ + "depth": depth, + "leafs": leafs, + "train_mae": train_mae, + "train_mse": train_mse, + "test_mae": test_mae, + "test_mse": test_mse + }) + + best_model = min(trained_model, key=lambda x: x["test_mae"]) + + print("Best model so far:") + print("MAE:", best_model["test_mae"]) + print("MSE:", best_model["test_mse"]) + + #model.save_model(model_path) visualizer = Visualizer(model=model) diff --git a/src/training/decision_tree/model.py b/src/training/decision_tree/model.py index 504612a..3cfc24e 100644 --- a/src/training/decision_tree/model.py +++ b/src/training/decision_tree/model.py @@ -64,6 +64,8 @@ class Model: print("Test:") print("MAE:", test_mae) print("MSE:", test_mse) + + return [train_mae, train_mse, test_mae, test_mse] def save_model(self, path):