8000 Ensure that numpy API __array_function__ for external data types is n… · alexshacked/scikit-learn@0f70387 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f70387

Browse files
committed
Ensure that numpy API __array_function__ for external data types is not activated in sklearn flows(scikit-learn#15865)
1 parent e030010 commit 0f70387

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

sklearn/inspection/tests/test_permutation_importance.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@
2323
from sklearn.preprocessing import scale
2424
from sklearn.utils import parallel_backend
2525
from sklearn.utils._testing import _convert_container
26+
from sklearn.utils.estimator_checks import _NotAnArray
2627

2728

28-
29+
def test_array_function_not_called():
30+
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4],
31+
[2, 1], [2, 2], [2, 3], [2, 4],
32+
[3, 1], [3, 2], [3, 3], [3, 4]])
33+
X = _NotAnArray(X)
34+
y = _NotAnArray([1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2])
35+
estimator = LogisticRegression()
36+
estimator.fit(X, y)
37+
rng = np.random.RandomState(42)
38+
permutation_importance(estimator, X, y, n_repeats=5,
39+
random_state=rng, n_jobs=1)
40+
2941
@pytest.mark.parametrize("n_jobs", [1, 2])
3042
def test_permutation_importance_correlated_feature_regression(n_jobs):
3143
# Make sure that feature highly correlated to the target have a higher

sklearn/model_selection/tests/test_validation.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from sklearn.model_selection.tests.test_search import FailingClassifier
1616

17+
from sklearn.utils.estimator_checks import _NotAnArray
1718
from sklearn.utils._testing import assert_almost_equal
1819
from sklearn.utils._testing import assert_raises
1920
from sklearn.utils._testing import assert_raise_message
@@ -226,6 +227,17 @@ def get_params(self, deep=False):
226227
return {'a': self.a, 'allow_nd': self.allow_nd}
227228

228229

230+
def test_array_function_not_called():
231+
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4],
232+
[2, 1], [2, 2], [2, 3], [2, 4],
233+
[3, 1], [3, 2], [3, 3], [3, 4]])
234+
X = _NotAnArray(X)
235+
y = _NotAnArray([1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2])
236+
estimator = LogisticRegression()
237+
grid = GridSearchCV(estimator, param_grid={'C': [1, 10]})
238+
cross_validate(grid, X, y, n_jobs=2)
239+
240+
229241
# XXX: use 2D array, since 1D X is being detected as a single sample in
230242
# check_consistent_length
231243
X = np.ones((10, 2))

0 commit comments

Comments
 (0)
0