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

feat: Integrate F1 score function to frontend, align with sklearn met… #28487

Merged
merged 2 commits into from
Mar 11, 2024
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
49 changes: 49 additions & 0 deletions ivy/functional/frontends/sklearn/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,55 @@ def accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None):
return ret


@to_ivy_arrays_and_back
def f1_score(y_true, y_pred, *, sample_weight=None):
# Ensure that y_true and y_pred have the same shape
if y_true.shape != y_pred.shape:
raise IvyValueError("y_true and y_pred must have the same shape")

# Check if sample_weight is provided and normalize it
if sample_weight is not None:
sample_weight = ivy.array(sample_weight)
if sample_weight.shape[0] != y_true.shape[0]:
raise IvyValueError(
"sample_weight must have the same length as y_true and y_pred"
)
sample_weight = sample_weight / ivy.sum(sample_weight)
else:
sample_weight = ivy.ones_like(y_true)

# Calculate true positives, predicted positives, and actual positives
true_positives = ivy.logical_and(ivy.equal(y_true, 1), ivy.equal(y_pred, 1)).astype(
"int64"
)
predicted_positives = ivy.equal(y_pred, 1).astype("int64")
actual_positives = ivy.equal(y_true, 1).astype("int64")

# Apply sample weights
weighted_true_positives = ivy.multiply(true_positives, sample_weight)
weighted_predicted_positives = ivy.multiply(predicted_positives, sample_weight)
weighted_actual_positives = ivy.multiply(actual_positives, sample_weight)

# Compute precision and recall with checks for division by zero
precision = 0.0
recall = 0.0
if ivy.sum(weighted_predicted_positives) > 0:
precision = ivy.sum(weighted_true_positives) / ivy.sum(
weighted_predicted_positives
)
if ivy.sum(weighted_actual_positives) > 0:
recall = ivy.sum(weighted_true_positives) / ivy.sum(weighted_actual_positives)

# Compute F1 score with a check to avoid division by zero
if precision + recall > 0:
ret = 2 * (precision * recall) / (precision + recall)
else:
ret = ivy.array(0.0) # If both precision and recall are zero, F1 score is zero

ret = ret.astype("float64")
return ret


@to_ivy_arrays_and_back
def precision_score(y_true, y_pred, *, sample_weight=None):
# Ensure that y_true and y_pred have the same shape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,69 @@ def test_sklearn_accuracy_score(
)


@handle_frontend_test(
fn_tree="sklearn.metrics.f1_score",
arrays_and_dtypes=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("integer"),
num_arrays=2,
min_value=0,
max_value=1, # F1 score is for binary classification
shared_dtype=True,
shape=(helpers.ints(min_value=2, max_value=5)),
),
sample_weight=st.lists(
st.floats(min_value=0.1, max_value=1), min_size=2, max_size=5
),
)
def test_sklearn_f1_score(
arrays_and_dtypes,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
sample_weight,
):
dtypes, values = arrays_and_dtypes
# Ensure the values are binary by rounding and converting to int
for i in range(2):
values[i] = np.round(values[i]).astype(int)

# Adjust sample_weight to have the correct length
sample_weight = np.array(sample_weight).astype(float)
if len(sample_weight) != len(values[0]):
# If sample_weight is shorter, extend it with ones
sample_weight = np.pad(
sample_weight,
(0, max(0, len(values[0]) - len(sample_weight))),
"constant",
constant_values=1.0,
)
# If sample_weight is longer, truncate it
sample_weight = sample_weight[: len(values[0])]

# Detach tensors if they require grad before converting to NumPy arrays
if backend_fw == "torch":
values = [
value.detach().numpy()
if isinstance(value, torch.Tensor) and value.requires_grad
else value
for value in values
]

helpers.test_frontend_function(
input_dtypes=dtypes,
backend_to_test=backend_fw,
test_flags=test_flags,
fn_tree=fn_tree,
frontend=frontend,
on_device=on_device,
y_true=values[0],
y_pred=values[1],
sample_weight=sample_weight,
)


@handle_frontend_test(
fn_tree="sklearn.metrics.precision_score",
arrays_and_dtypes=helpers.dtype_and_values(
Expand Down
Loading