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

Use len(classes_) instead of len(set(y_true)) #5275

Merged
merged 6 commits into from Jan 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions mlflow/sklearn/utils.py
Expand Up @@ -257,6 +257,13 @@ def _get_classifier_metrics(fitted_estimator, prefix, X, y_true, sample_weight):
return _get_metrics_value_dict(classifier_metrics)


def _get_class_labels_from_estimator(estimator):
"""
Extracts class labels from `estimator` if `estimator.classes` is available.
"""
return estimator.classes_ if hasattr(estimator, "classes_") else None


def _get_classifier_artifacts(fitted_estimator, prefix, X, y_true, sample_weight):
"""
Draw and record various common artifacts for classifier
Expand Down Expand Up @@ -291,10 +298,15 @@ def _get_classifier_artifacts(fitted_estimator, prefix, X, y_true, sample_weight
def plot_confusion_matrix(*args, **kwargs):
import matplotlib

num_classes = len(set(y_true))
class_labels = _get_class_labels_from_estimator(fitted_estimator)
if class_labels is None:
class_labels = set(y_true)

with matplotlib.rc_context(
{
"font.size": min(10.0, 50.0 / num_classes),
"figure.dpi": 288,
"figure.figsize": [6.0, 4.0],
Comment on lines +307 to +308
Copy link
Member Author

Choose a reason for hiding this comment

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

This might not be the best approach.

"font.size": min(10.0, 50.0 / len(class_labels)),
"axes.labelsize": 10.0,
}
):
Expand Down