Skip to content

Commit

Permalink
fix: black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kbiniek committed May 5, 2024
1 parent 824c7e6 commit 280a1fd
Show file tree
Hide file tree
Showing 7 changed files with 955 additions and 585 deletions.
16 changes: 4 additions & 12 deletions nmrcraft/analysis/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
plt.rcParams["text.usetex"] = False


def plot_predicted_vs_ground_truth(
y_test: np.array, y_pred: np.array, title: str
):
def plot_predicted_vs_ground_truth(y_test: np.array, y_pred: np.array, title: str):
"""
Plots the predicted values against the ground truth values.
Parameters:
Expand All @@ -33,9 +31,7 @@ def plot_predicted_vs_ground_truth(
# Creating the plot
plt.figure(figsize=(10, 8))
plt.scatter(y_test, y_pred, color=first_color, edgecolor="k", alpha=0.6)
plt.plot(
[y_test.min(), y_test.max()], [y_test.min(), y_test.max()], "k--", lw=2
)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], "k--", lw=2)
plt.xlabel("Actual")
plt.ylabel("Predicted")
plt.title(title)
Expand Down Expand Up @@ -68,12 +64,8 @@ def plot_predicted_vs_ground_truth_density(

# Creating the plot
plt.figure(figsize=(10, 8))
plt.scatter(
y_test, y_pred, c=scalar_map.to_rgba(kernel), edgecolor="k", alpha=0.9
)
plt.plot(
[y_test.min(), y_test.max()], [y_test.min(), y_test.max()], "k--", lw=2
)
plt.scatter(y_test, y_pred, c=scalar_map.to_rgba(kernel), edgecolor="k", alpha=0.9)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], "k--", lw=2)
plt.xlabel("Actual")
plt.ylabel("Predicted")
plt.title(title)
Expand Down
4 changes: 1 addition & 3 deletions nmrcraft/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def load_dataset_from_hf(
Returns:
pandas.DataFrame: The loaded dataset as a pandas DataFrame.
"""
dataset = load_dataset(dataset_name, data_files=data_files)[
"train"
].to_pandas()
dataset = load_dataset(dataset_name, data_files=data_files)["train"].to_pandas()
return dataset


Expand Down
8 changes: 2 additions & 6 deletions nmrcraft/models/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
"random_forest": {
"model_params": {"random_state": 42},
"hyperparameters": {
"n_estimators": hp.choice("n_estimators", range(10, 1000, 10)),
"n_estimators": hp.choice("n_estimators", range(10, 1000, 10)),
"criterion": hp.choice("criterion", ["gini", "entropy"]),
# "max_depth": hp.choice("max_depth", range(10, 1200, 10)),
"min_samples_split": hp.uniform("min_samples_split", 0.01, 1.0),
"min_samples_leaf": hp.uniform("min_samples_leaf", 0.01, 0.5),
"max_features": hp.choice("max_features", ["sqrt", "log2", None]),
},
},

"gradient_boosting": {
"model_params": {"random_state": 42},
"hyperparameters": {
"loss": hp.choice("loss", ["log_loss", "exponential"]),
"learning_rate": hp.uniform("learning_rate", 0.01, 0.5),
"n_estimators": hp.choice("n_estimators", range(10, 1000, 10)),
# "subsample": hp.uniform("subsample", 0.01, 1.0),
# "subsample": hp.uniform("subsample", 0.01, 1.0),
"criterion": hp.choice("criterion", ["friedman_mse", "squared_error"]),
# "max_depth": hp.choice("max_depth", range(10, 1200, 10)),
"min_samples_split": hp.uniform("min_samples_split", 0.01, 1.0),
"min_samples_leaf": hp.uniform("min_samples_leaf", 0.01, 0.5),
"max_features": hp.choice("max_features", ["sqrt", "log2", None]),
},
},

"logistic_regression": {
"model_params": {"random_state": 42},
"hyperparameters": {
Expand All @@ -44,7 +42,6 @@
"l1_ratio": hp.uniform("l1_ratio", 0.01, 1.0),
},
},

"svc": {
"model_params": {"random_state": 42},
"hyperparameters": {
Expand All @@ -56,7 +53,6 @@
"shrinking": hp.choice("shrinking", [True, False]),
"probability": hp.choice("probability", [True, False]),
# "max_iter": hp.choice("max_iter", range(100, 1000, 100)),

},
},
}
11 changes: 4 additions & 7 deletions nmrcraft/training/hyperparameter_tune.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from hyperopt import STATUS_OK, Trials, fmin, space_eval, tpe

# from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score

Expand Down Expand Up @@ -29,9 +30,7 @@ def __init__(
self.max_evals = max_evals
self.algo = algo

def _objective(
self, params: dict, X_train, y_train, X_test, y_test
) -> dict:
def _objective(self, params: dict, X_train, y_train, X_test, y_test) -> dict:
"""
Objective function for hyperparameter tuning.
Expand All @@ -51,7 +50,7 @@ def _objective(
model.fit(X_train, y_train)
# y_pred = model.predict(X_test)
# score = accuracy_score(y_test, y_pred)
score = cross_val_score(model, X_train, y_train, cv = 5).mean()
score = cross_val_score(model, X_train, y_train, cv=5).mean()
return {"loss": -score, "status": STATUS_OK}

def tune(self, X_train, y_train, X_test, y_test) -> tuple:
Expand All @@ -68,9 +67,7 @@ def tune(self, X_train, y_train, X_test, y_test) -> tuple:
tuple: The best parameters and the tuning trials.
"""
best = fmin(
fn=lambda params: self._objective(
params, X_train, y_train, X_test, y_test
),
fn=lambda params: self._objective(params, X_train, y_train, X_test, y_test),
space=self.model_config["hyperparameters"],
algo=self.algo,
max_evals=self.max_evals,
Expand Down
Loading

0 comments on commit 280a1fd

Please sign in to comment.