Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _compute_binary_metrics(
for class_idx in [0, 1]:
if class_idx == 0:
# Invert for class 0 (negative class)
inv_preds = 1 - preds if torch.is_floating_point(preds) else 1 - preds
inv_preds = 1 - preds
inv_target = 1 - target

precision_val = binary_precision(inv_preds, inv_target, threshold, validate_args=validate_args).item()
Expand Down
20 changes: 10 additions & 10 deletions tests/unittests/classification/test_classification_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
classification_report as functional_classification_report,
)

from .._helpers import seed_all
from unittests._helpers import seed_all

seed_all(42)

Expand All @@ -46,34 +46,34 @@ def make_prediction(dataset=None, binary=False):
# import some data to play with
dataset = datasets.load_iris()

X = dataset.data
x = dataset.data
y = dataset.target

if binary:
# restrict to a binary classification task
X, y = X[y < 2], y[y < 2]
x, y = x[y < 2], y[y < 2]

n_samples, n_features = X.shape
n_samples, n_features = x.shape
p = np.arange(n_samples)

rng = check_random_state(37)
rng.shuffle(p)
X, y = X[p], y[p]
x, y = x[p], y[p]
half = int(n_samples / 2)

# add noisy features to make the problem harder and avoid perfect results
rng = np.random.RandomState(0)
X = np.c_[X, rng.randn(n_samples, 200 * n_features)]
x = np.c_[x, rng.randn(n_samples, 200 * n_features)]

# run classifier, get class probabilities and label predictions
clf = SVC(kernel="linear", probability=True, random_state=0)
y_pred_proba = clf.fit(X[:half], y[:half]).predict_proba(X[half:])
y_pred_proba = clf.fit(x[:half], y[:half]).predict_proba(x[half:])

if binary:
# only interested in probabilities of the positive case
y_pred_proba = y_pred_proba[:, 1]

y_pred = clf.predict(X[half:])
y_pred = clf.predict(x[half:])
y_true = y[half:]
return y_true, y_pred, y_pred_proba

Expand Down Expand Up @@ -368,7 +368,7 @@ def test_multilabel_classification_report(self, output_dict, use_probabilities):

# Check for any aggregate metrics that might be present
possible_avg_keys = ["micro avg", "macro avg", "weighted avg", "samples avg", "accuracy"]
found_aggregates = [key for key in result.keys() if key in possible_avg_keys]
found_aggregates = [key for key in result if key in possible_avg_keys]
assert len(found_aggregates) > 0, f"No aggregate metrics found. Available keys: {list(result.keys())}"

else:
Expand Down Expand Up @@ -498,7 +498,7 @@ def test_multilabel_classification_report(use_probabilities):
# Check for any aggregate metrics that might be present
# (don't require specific ones as implementations may differ)
possible_avg_keys = ["micro avg", "macro avg", "weighted avg", "samples avg", "accuracy"]
found_aggregates = [key for key in result.keys() if key in possible_avg_keys]
found_aggregates = [key for key in result if key in possible_avg_keys]
assert len(found_aggregates) > 0, f"No aggregate metrics found. Available keys: {list(result.keys())}"

else:
Expand Down