Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ROC-AUC for classifiers #475

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ __pycache__/
*.py[cod]
*$py.class

tests/mq/

# C extensions
*.so

Expand Down
73 changes: 62 additions & 11 deletions lazypredict/Supervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np
import pandas as pd
from tqdm import tqdm
from tqdm.autonotebook import tqdm
import datetime
import time
from sklearn.pipeline import Pipeline
Expand All @@ -18,6 +18,9 @@
from sklearn.metrics import (
accuracy_score,
balanced_accuracy_score,
euclidean_distances,
precision_score,
recall_score,
roc_auc_score,
f1_score,
r2_score,
Expand Down Expand Up @@ -246,6 +249,8 @@ def fit(self, X_train, X_test, y_train, y_test):
B_Accuracy = []
ROC_AUC = []
F1 = []
PRECISION = []
RECALL = []
names = []
TIME = []
predictions = {}
Expand Down Expand Up @@ -289,35 +294,73 @@ def fit(self, X_train, X_test, y_train, y_test):
start = time.time()
try:
if "random_state" in model().get_params().keys():
pipe = Pipeline(
steps=[
("preprocessor", preprocessor),
("classifier", model(random_state=self.random_state)),
]
)
if "probability" not in model().get_params().keys():
pipe = Pipeline(
steps=[
("preprocessor", preprocessor),
("classifier", model(
random_state=self.random_state)),
]
)
else:
pipe = Pipeline(
steps=[
("preprocessor", preprocessor),
("classifier", model(
random_state=self.random_state, probability=True)),
]
)
else:
pipe = Pipeline(
steps=[("preprocessor", preprocessor), ("classifier", model())]
)
if "probability" not in model().get_params().keys():
pipe = Pipeline(
steps=[("preprocessor", preprocessor),
("classifier", model())]
)
else:
pipe = Pipeline(
steps=[("preprocessor", preprocessor),
("classifier", model(probability=True))]
)

pipe.fit(X_train, y_train)
self.models[name] = pipe
y_pred = pipe.predict(X_test)

try:
y_score = pipe.predict_proba(X_test)[:, 1]
except:
try:
y_score = pipe.decision_function(X_test)
except:
# Predict centroids and distances
centroids = pipe.named_steps['classifier'].centroids_
distances = euclidean_distances(X_test, centroids)

# Use negative distances to the positive class centroid as the score
# (Smaller distance => Higher score for positive class)
# Assuming binary classification with class labels 0 and 1
y_score = -distances[:, 1]

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")
precision = precision_score(y_test, y_pred, average="weighted")
recall = recall_score(y_test, y_pred, average="weighted")
try:
roc_auc = roc_auc_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_score)
except Exception as exception:
roc_auc = None
if self.ignore_warnings is False:
print("ROC AUC couldn't be calculated for " + name)
print(exception)

names.append(name)
Accuracy.append(accuracy)
B_Accuracy.append(b_accuracy)
ROC_AUC.append(roc_auc)
F1.append(f1)
PRECISION.append(precision)
RECALL.append(recall)
TIME.append(time.time() - start)
if self.custom_metric is not None:
custom_metric = self.custom_metric(y_test, y_pred)
Expand All @@ -331,6 +374,8 @@ def fit(self, X_train, X_test, y_train, y_test):
"Balanced Accuracy": b_accuracy,
"ROC AUC": roc_auc,
"F1 Score": f1,
"Precision": precision,
"Recall": recall,
self.custom_metric.__name__: custom_metric,
"Time taken": time.time() - start,
}
Expand All @@ -343,6 +388,8 @@ def fit(self, X_train, X_test, y_train, y_test):
"Balanced Accuracy": b_accuracy,
"ROC AUC": roc_auc,
"F1 Score": f1,
"Precision": precision,
"Recall": recall,
"Time taken": time.time() - start,
}
)
Expand All @@ -360,6 +407,8 @@ def fit(self, X_train, X_test, y_train, y_test):
"Balanced Accuracy": B_Accuracy,
"ROC AUC": ROC_AUC,
"F1 Score": F1,
"Precision": PRECISION,
"Recall": RECALL,
"Time Taken": TIME,
}
)
Expand All @@ -371,6 +420,8 @@ def fit(self, X_train, X_test, y_train, y_test):
"Balanced Accuracy": B_Accuracy,
"ROC AUC": ROC_AUC,
"F1 Score": F1,
"Precision": PRECISION,
"Recall": RECALL,
self.custom_metric.__name__: CUSTOM_METRIC,
"Time Taken": TIME,
}
Expand Down