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

add MCC to BinaryClassificationEvaluator #3051

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import TYPE_CHECKING, Literal

import numpy as np
from sklearn.metrics import average_precision_score
from sklearn.metrics import average_precision_score, matthews_corrcoef
from sklearn.metrics.pairwise import paired_cosine_distances, paired_euclidean_distances, paired_manhattan_distances

from sentence_transformers.evaluation.SentenceEvaluator import SentenceEvaluator
Expand Down Expand Up @@ -124,6 +124,7 @@ def _append_csv_headers(self, similarity_fn_names: list[str]) -> None:
"recall",
"f1_threshold",
"ap",
"mcc",
]

for v in similarity_fn_names:
Expand Down Expand Up @@ -275,11 +276,15 @@ def compute_metrices(self, model: SentenceTransformer) -> dict[str, dict[str, fl
f1, precision, recall, f1_threshold = self.find_best_f1_and_threshold(scores, labels, greater_is_better)
ap = average_precision_score(labels, scores * (1 if greater_is_better else -1))

predicted_labels = (scores >= f1_threshold) if greater_is_better else (scores <= f1_threshold)
mcc = matthews_corrcoef(labels, predicted_labels)

logger.info(f"Accuracy with {name}: {acc * 100:.2f}\t(Threshold: {acc_threshold:.4f})")
logger.info(f"F1 with {name}: {f1 * 100:.2f}\t(Threshold: {f1_threshold:.4f})")
logger.info(f"Precision with {name}: {precision * 100:.2f}")
logger.info(f"Recall with {name}: {recall * 100:.2f}")
logger.info(f"Average Precision with {name}: {ap * 100:.2f}\n")
logger.info(f"Matthews Correlation with {name}: {mcc:.4f}\n")
Copy link

@reissaavedra reissaavedra Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @JINO-ROHIT ,
I think you could also add this output to the docstring of the BinaryClassificationEvaluator class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion!


output_scores[similarity_fn_name] = {
"accuracy": acc,
Expand All @@ -289,6 +294,7 @@ def compute_metrices(self, model: SentenceTransformer) -> dict[str, dict[str, fl
"precision": precision,
"recall": recall,
"ap": ap,
"mcc": mcc,
}

return output_scores
Expand Down