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 recall_score function in Ivy with Test #27986

Merged
merged 17 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 32 additions & 0 deletions ivy/functional/frontends/sklearn/metrics/_classification.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ivy
from ivy.functional.frontends.numpy.func_wrapper import to_ivy_arrays_and_back
from sklearn.utils.multiclass import type_of_target
from ivy.utils.exceptions import IvyValueError


@to_ivy_arrays_and_back
Expand All @@ -17,3 +18,34 @@ def accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None):
ret = ret / y_true.shape[0]
ret = ret.astype("float64")
return ret


@to_ivy_arrays_and_back
def recall_score(y_true, y_pred, *, average="binary", sample_weight=None):
# Determine the type of the target variable
y_type = type_of_target(y_true)
# Check if the 'average' parameter has a valid value
if average != "binary" and average != "micro" and average != "macro":
raise IvyValueError(
"Invalid value for 'average'. Supported values are 'binary', 'micro', or"
" 'macro'."
)
# Calculate true positive and actual positive counts based on the target type
if y_type.startswith("multilabel"):
true_positive = ivy.sum(ivy.logical_and(y_true, y_pred) * sample_weight, axis=1)
actual_positive = ivy.sum(y_true * sample_weight, axis=1)
else:
true_positive = ivy.sum(ivy.logical_and(y_true, y_pred) * sample_weight)
actual_positive = ivy.sum(y_true * sample_weight)
# Calculate recall for each class or overall
recall = true_positive / ivy.maximum(
actual_positive, ivy.to_scalar(ivy.array([1], "float64"))
)
# Perform additional calculations for micro or macro averaging
if average == "micro":
recall = ivy.sum(true_positive) / ivy.maximum(
ivy.sum(actual_positive), ivy.to_scalar(ivy.array([1], "float64"))
)
elif average == "macro":
recall = ivy.mean(recall)
return recall
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,49 @@
import numpy as np


@handle_frontend_test(
fn_tree="your.module.path.recall_score", # Update with the correct module path
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @muzakkirhussain011 could you name it correctly with the module path?

arrays_and_dtypes=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float_and_integer"),
num_arrays=2,
min_value=-2,
max_value=2,
shared_dtype=True,
shape=(helpers.ints(min_value=2, max_value=5)),
),
average=st.sampled_from(["binary", "micro", "macro"]),
sample_weight=st.none() | st.floats(min_value=0, max_value=1),
)
def test_recall_score(
arrays_and_dtypes,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
average,
sample_weight,
):
dtypes, values = arrays_and_dtypes
# Ensure discrete values if float dtype
for i in range(2):
if "float" in dtypes[i]:
values[i] = np.floor(values[i])

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],
average=average,
sample_weight=sample_weight,
)


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