Skip to content

Commit

Permalink
REFACTOR: Fix gigantic error. Gridsearch returned the worst parameter…
Browse files Browse the repository at this point in the history
…s, not the best
  • Loading branch information
denBruneBarone committed Mar 27, 2024
1 parent 6a847d4 commit cb6420e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 3 additions & 3 deletions machine_learning/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class HPConfig:
max_leaf_nodes = 500 # værdien fra paperet om modeller er 10

class BestHPConfig:
criterion = None
max_depth = None
criterion = 'friedman_mse'
max_depth = 10
max_features = None
max_leaf_nodes = None
max_leaf_nodes = 10

class GridSearchConfig:
param_grid = {
Expand Down
2 changes: 1 addition & 1 deletion machine_learning/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def train():
model = pickle.load(model_file)
else:
print("Training...")
model = train_model(train_data)
model = train_model(train_data, grid_search_cv=False)

# Save the trained model
print("Saving trained model...")
Expand Down
16 changes: 5 additions & 11 deletions machine_learning/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sklearn.tree import DecisionTreeRegressor
from machine_learning.prepare_for_training import TrainingDataset
from sklearn.metrics import mean_squared_error
from machine_learning.config import HPConfig, GridSearchConfig
from machine_learning.config import HPConfig, GridSearchConfig, BestHPConfig
from sklearn.metrics import make_scorer


Expand All @@ -17,7 +17,7 @@ def rmse_cum_power(true_labels, predicted_labels):


def custom_scoring(true_labels, predicted_labels):
return rmse_cum_power(true_labels, predicted_labels)
return -rmse_cum_power(true_labels, predicted_labels)

custom_scoring = make_scorer(custom_scoring)

Expand Down Expand Up @@ -61,8 +61,8 @@ def train_model(train_data, grid_search_cv=True):
training_dataset = TrainingDataset(train_data)

# Instantiate the decision tree model with specified hyperparameters
model = DecisionTreeRegressor(criterion=HPConfig.criterion, max_depth=HPConfig.max_depth,
max_features=HPConfig.max_features, max_leaf_nodes=HPConfig.max_leaf_nodes,
model = DecisionTreeRegressor(criterion=BestHPConfig.criterion, max_depth=BestHPConfig.max_depth,
max_features=BestHPConfig.max_features, max_leaf_nodes=BestHPConfig.max_leaf_nodes,
random_state=42)

# Extract features and targets from the training dataset
Expand Down Expand Up @@ -124,13 +124,7 @@ def evaluate_model(model, test_data):
test_targets_cumulative_power = np.cumsum(test_targets_np[:, 0] * test_targets_np[:, 1] * time_diff)
original_test_rmse = np.sqrt(mean_squared_error(test_targets_cumulative_power, cumulative_power))

print(f"Original Test Root Mean Squared Error (RMSE) for Cumulative Power: {original_test_rmse}")

# Calculate RMSE on the adjusted cumulative power for the test set
test_rmse = np.sqrt(
mean_squared_error(test_targets_np[:, 0] * test_targets_np[:, 1], power_consumption_predictions))
print(f"Adjusted Test Root Mean Squared Error (RMSE) for Cumulative Power: {test_rmse}")

print(f"Root Mean Squared Error (RMSE) for Cumulative Power: {original_test_rmse}")
print("Evaluation finished!")

return model

0 comments on commit cb6420e

Please sign in to comment.