Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de0581a

Browse files
GKjohnsjnothman
authored andcommitted
TST Check estimator pairwise (#9701)
1 parent c24a13e commit de0581a

File tree

6 files changed

+165
-26
lines changed

6 files changed

+165
-26
lines changed

doc/whats_new/v0.20.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,10 @@ Cluster
196196
- Deprecate ``pooling_func`` unused parameter in
197197
:class:`cluster.AgglomerativeClustering`. :issue:`9875` by :user:`Kumar Ashutosh
198198
<thechargedneutron>`.
199+
200+
Changes to estimator checks
201+
---------------------------
202+
203+
- Allow tests in :func:`estimator_checks.check_estimator` to test functions
204+
that accept pairwise data.
205+
:issue:`9701` by :user:`Kyle Johnson <gkjohns>`

sklearn/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ def is_classifier(estimator):
551551
def is_regressor(estimator):
552552
"""Returns True if the given estimator is (probably) a regressor.
553553
554-
555554
Parameters
556555
----------
557556
estimator : object

sklearn/neighbors/regression.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# License: BSD 3 clause (C) INRIA, University of Amsterdam
1010

1111
import numpy as np
12+
from scipy.sparse import issparse
1213

1314
from .base import _get_weights, _check_weights, NeighborsBase, KNeighborsMixin
1415
from .base import RadiusNeighborsMixin, SupervisedFloatMixin
@@ -139,6 +140,11 @@ def predict(self, X):
139140
y : array of int, shape = [n_samples] or [n_samples, n_outputs]
140141
Target values
141142
"""
143+
if issparse(X) and self.metric == 'precomputed':
144+
raise ValueError(
145+
"Sparse matrices not supported for prediction with "
146+
"precomputed kernels. Densify your matrix."
147+
)
142148
X = check_array(X, accept_sparse='csr')
143149

144150
neigh_dist, neigh_ind = self.kneighbors(X)

sklearn/neighbors/tests/test_neighbors.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import numpy as np
44
from scipy.sparse import (bsr_matrix, coo_matrix, csc_matrix, csr_matrix,
5-
dok_matrix, lil_matrix)
5+
dok_matrix, lil_matrix, issparse)
66

77
from sklearn import metrics
88
from sklearn import neighbors, datasets
@@ -731,10 +731,22 @@ def test_kneighbors_regressor_sparse(n_samples=40,
731731
knn = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
732732
algorithm='auto')
733733
knn.fit(sparsemat(X), y)
734+
735+
knn_pre = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
736+
metric='precomputed')
737+
knn_pre.fit(pairwise_distances(X, metric='euclidean'), y)
738+
734739
for sparsev in SPARSE_OR_DENSE:
735740
X2 = sparsev(X)
736741
assert_true(np.mean(knn.predict(X2).round() == y) > 0.95)
737742

743+
X2_pre = sparsev(pairwise_distances(X, metric='euclidean'))
744+
if issparse(sparsev(X2_pre)):
745+
assert_raises(ValueError, knn_pre.predict, X2_pre)
746+
else:
747+
assert_true(
748+
np.mean(knn_pre.predict(X2_pre).round() == y) > 0.95)
749+
738750

739751
def test_neighbors_iris():
740752
# Sanity checks on the iris dataset

0 commit comments

Comments
 (0)