Skip to content

Commit

Permalink
feat(frontend): implement column_or_1d function for sklearn validatio…
Browse files Browse the repository at this point in the history
…n submod with test. Complex types are unsupported.
  • Loading branch information
Ishticode committed Sep 3, 2023
1 parent f99dbb6 commit b8ca4a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ivy/functional/frontends/sklearn/utils/validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ivy
from ivy.functional.frontends.numpy.func_wrapper import to_ivy_arrays_and_back
from ivy.func_wrapper import with_unsupported_dtypes


@to_ivy_arrays_and_back
Expand All @@ -13,3 +14,15 @@ def as_float_array(X, *, copy=True, force_all_finite=True):
else:
return_dtype = ivy.float64
return ivy.asarray(X, dtype=return_dtype)


@with_unsupported_dtypes({"1.3.0 and below": ("complex",)}, "sklearn")
@to_ivy_arrays_and_back
def column_or_1d(y, *, warn=False):
shape = y.shape
if len(shape) == 2 and shape[1] == 1:
y = ivy.reshape(y, (-1,))
elif len(shape) > 2:
raise ValueError(
"y should be a 1d array or a column vector")
return y
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from hypothesis import strategies as st
import ivy_tests.test_ivy.helpers as helpers
from ivy_tests.test_ivy.helpers import handle_frontend_test

Expand Down Expand Up @@ -26,3 +27,30 @@ def test_sklearn_as_float_array(
on_device=on_device,
X=x[0],
)


@handle_frontend_test(
fn_tree="sklearn.utils.column_or_1d",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("valid"),
shape=st.one_of(st.tuples(st.integers(1, 10), st.just(1)), st.tuples(st.integers(1, 10))),
),
)
def test_sklearn_column_or_1d(
dtype_and_x,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
dtypes, x = dtype_and_x
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=x[0],
)

0 comments on commit b8ca4a1

Please sign in to comment.