Skip to content

Commit

Permalink
define RMSE
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymoudiki committed Oct 23, 2024
1 parent d8e0565 commit 20df6f6
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 111 deletions.
2 changes: 1 addition & 1 deletion mlsauce-docs/search.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions mlsauce/booster/_booster_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,9 @@ def update(self, X, y, eta=0.9):
y: float = [n_samples=1]
Target value.
eta: float
Inverse power applied to number of observations
Inverse power applied to number of observations
(defines a learning rate).
Returns:
Expand All @@ -540,10 +540,10 @@ def update(self, X, y, eta=0.9):
seed=self.seed,
),
)
)
)

self.obj = boosterc.update_booster(
self.obj, np.asarray(X, order="C"), np.asarray(y, order="C"), eta
self.obj, np.asarray(X, order="C"), np.asarray(y, order="C"), eta
)

return self
Expand Down
15 changes: 7 additions & 8 deletions mlsauce/booster/_booster_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ def predict(self, X, level=95, method=None, **kwargs):
self.y_ = None
preds = self.pi.predict(X, return_pi=True)
return preds
#print(f"\n in predict self: {self} \n")
#print(f"\n in predict self.obj: {self.obj} \n")
#try:
# print(f"\n in predict self: {self} \n")
# print(f"\n in predict self.obj: {self.obj} \n")
# try:
return boosterc.predict_booster_regressor(
self.obj, np.asarray(X, order="C")
)
#except ValueError:
# except ValueError:
# pass

def update(self, X, y, eta=0.9):
Expand All @@ -393,9 +393,9 @@ def update(self, X, y, eta=0.9):
y: float = [n_samples=1]
Target value.
eta: float
Inverse power applied to number of observations
Inverse power applied to number of observations
(defines a learning rate).
Returns:
Expand All @@ -422,9 +422,8 @@ def update(self, X, y, eta=0.9):
seed=self.seed,
),
)
)
)


self.obj = boosterc.update_booster(
self.obj, np.asarray(X, order="C"), np.asarray(y, order="C"), eta
)
Expand Down
10 changes: 5 additions & 5 deletions mlsauce/lazybooster/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"IsotonicRegression",
"MLPRegressor",
"KernelRidge",
#"MultiOutputRegressor",
#"MultiTaskElasticNet",
# "MultiOutputRegressor",
# "MultiTaskElasticNet",
"MultiTaskElasticNetCV",
#"MultiTaskLasso",
# "MultiTaskLasso",
"MultiTaskLassoCV",
"NuSVR",
"NuSVR",
"OrthogonalMatchingPursuit",
"OrthogonalMatchingPursuitCV",
"PLSCanonical",
Expand All @@ -49,7 +49,7 @@

MTASKREGRESSORS = [
(
"GenericBooster(MultiTask(" + est[0] + "))",
"GenericBooster(MultiTask(" + est[0] + "))",
partial(MultiTaskRegressor, regr=est[1]()),
)
for est in all_estimators()
Expand Down
127 changes: 91 additions & 36 deletions mlsauce/lazybooster/lazyboosterclassif.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
and (est[0] in self.estimators)
)
] + [
("GBoostClassifier(MultiTask(" + est[0] + "))", partial(MultiTaskRegressor, regr=est[1]()))
(
"GBoostClassifier(MultiTask(" + est[0] + "))",
partial(MultiTaskRegressor, regr=est[1]()),
)
for est in all_estimators()
if (
issubclass(est[1], RegressorMixin)
Expand All @@ -352,7 +355,7 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
]

if self.preprocess is True:

if self.n_jobs is None:

for name, model in tqdm(self.classifiers): # do parallel exec
Expand All @@ -376,7 +379,9 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
fitted_clf = GenericBoostingClassifier(
{**other_args, **kwargs},
verbose=self.verbose,
base_model=model(random_state=self.random_state),
base_model=model(
random_state=self.random_state
),
)

else:
Expand All @@ -401,15 +406,19 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
pipe.fit(X_train, y_train)
self.models_[name] = pipe
y_pred = pipe.predict(X_test)
accuracy = accuracy_score(y_test, y_pred, normalize=True)
accuracy = accuracy_score(
y_test, y_pred, normalize=True
)
b_accuracy = balanced_accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred, average="weighted")
try:
roc_auc = roc_auc_score(y_test, y_pred)
except Exception as exception:
roc_auc = None
if self.ignore_warnings is False:
print("ROC AUC couldn't be calculated for " + name)
print(
"ROC AUC couldn't be calculated for " + name
)
print(exception)
names.append(name)
Accuracy.append(accuracy)
Expand Down Expand Up @@ -452,15 +461,24 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
print(exception)

else:
# train_model(self, name, model, X_train, y_train, X_test, y_test,
#use_preprocessing=False, preprocessor=None,

# train_model(self, name, model, X_train, y_train, X_test, y_test,
# use_preprocessing=False, preprocessor=None,
# **kwargs):
results = Parallel(n_jobs=self.n_jobs)(delayed(self.train_model)(
name, model, X_train, y_train, X_test, y_test,
use_preprocessing=True, preprocessor=preprocessor, **kwargs
) for name, model in tqdm(self.classifiers)
)
results = Parallel(n_jobs=self.n_jobs)(
delayed(self.train_model)(
name,
model,
X_train,
y_train,
X_test,
y_test,
use_preprocessing=True,
preprocessor=preprocessor,
**kwargs
)
for name, model in tqdm(self.classifiers)
)
Accuracy = [res["accuracy"] for res in results]
B_Accuracy = [res["balanced_accuracy"] for res in results]
ROC_AUC = [res["roc_auc"] for res in results]
Expand All @@ -470,41 +488,50 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
if self.custom_metric is not None:
CUSTOM_METRIC = [res["custom_metric"] for res in results]
if self.predictions:
predictions = {res["name"]: res["predictions"] for res in results}

predictions = {
res["name"]: res["predictions"] for res in results
}

else: # no preprocessing

if self.n_jobs is None:

for name, model in tqdm(self.classifiers): # do parallel exec
start = time.time()
try:
if "random_state" in model().get_params().keys():
fitted_clf = GenericBoostingClassifier(
base_model=model(random_state=self.random_state),
base_model=model(
random_state=self.random_state
),
verbose=self.verbose,
**kwargs
)

else:
fitted_clf = GenericBoostingClassifier(
base_model=model(), verbose=self.verbose, **kwargs
base_model=model(),
verbose=self.verbose,
**kwargs
)

fitted_clf.fit(X_train, y_train)

self.models_[name] = fitted_clf
y_pred = fitted_clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred, normalize=True)
accuracy = accuracy_score(
y_test, y_pred, normalize=True
)
b_accuracy = balanced_accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred, average="weighted")
try:
roc_auc = roc_auc_score(y_test, y_pred)
except Exception as exception:
roc_auc = None
if self.ignore_warnings is False:
print("ROC AUC couldn't be calculated for " + name)
print(
"ROC AUC couldn't be calculated for " + name
)
print(exception)
names.append(name)
Accuracy.append(accuracy)
Expand Down Expand Up @@ -546,13 +573,21 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
print(name + " model failed to execute")
print(exception)

else:
else:

results = Parallel(n_jobs=self.n_jobs)(delayed(self.train_model)(
name, model, X_train, y_train, X_test, y_test,
use_preprocessing=False, **kwargs
) for name, model in tqdm(self.classifiers)
)
results = Parallel(n_jobs=self.n_jobs)(
delayed(self.train_model)(
name,
model,
X_train,
y_train,
X_test,
y_test,
use_preprocessing=False,
**kwargs
)
for name, model in tqdm(self.classifiers)
)
Accuracy = [res["accuracy"] for res in results]
B_Accuracy = [res["balanced_accuracy"] for res in results]
ROC_AUC = [res["roc_auc"] for res in results]
Expand All @@ -562,8 +597,9 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
if self.custom_metric is not None:
CUSTOM_METRIC = [res["custom_metric"] for res in results]
if self.predictions:
predictions = {res["name"]: res["predictions"] for res in results}

predictions = {
res["name"]: res["predictions"] for res in results
}

if self.custom_metric is None:
scores = pd.DataFrame(
Expand Down Expand Up @@ -643,18 +679,29 @@ def provide_models(self, X_train, X_test, y_train, y_test):

return self.models_


def train_model(self, name, model, X_train, y_train, X_test, y_test,
use_preprocessing=False, preprocessor=None,
**kwargs):
def train_model(
self,
name,
model,
X_train,
y_train,
X_test,
y_test,
use_preprocessing=False,
preprocessor=None,
**kwargs
):
"""
Function to train a single model and return its results.
"""
other_args = {}

# Handle n_jobs parameter
try:
if "n_jobs" in model().get_params().keys() and "LogisticRegression" not in name:
if (
"n_jobs" in model().get_params().keys()
and "LogisticRegression" not in name
):
other_args["n_jobs"] = self.n_jobs
except Exception:
pass
Expand Down Expand Up @@ -688,13 +735,21 @@ def train_model(self, name, model, X_train, y_train, X_test, y_test,
]
)
if self.verbose > 0:
print("\n Fitting pipeline with preprocessing for " + name + " model...")
print(
"\n Fitting pipeline with preprocessing for "
+ name
+ " model..."
)
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)
else:
# Case with no preprocessing
if self.verbose > 0:
print("\n Fitting model without preprocessing for " + name + " model...")
print(
"\n Fitting model without preprocessing for "
+ name
+ " model..."
)
y_pred = fitted_clf.predict(X_test)

accuracy = accuracy_score(y_test, y_pred, normalize=True)
Expand Down Expand Up @@ -728,4 +783,4 @@ def train_model(self, name, model, X_train, y_train, X_test, y_test,
if self.ignore_warnings is False:
print(name + " model failed to execute")
print(exception)
return None
return None
Loading

0 comments on commit 20df6f6

Please sign in to comment.