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

Skip to content

[MRG+1] Check estimator pairwise #9701

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

Merged
merged 71 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
b2ac13c
initial commit
GKjohns Aug 29, 2017
124622b
add test for check_estimator on SVC(kernel='precomputed')
GKjohns Aug 29, 2017
bb352be
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 3, 2017
77fcb46
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 5, 2017
578865e
change tests to run on estimators with _pairwise set to True
GKjohns Sep 6, 2017
d6f3c27
fix typo in is_pairwise docstring
GKjohns Sep 7, 2017
d9fff0a
fix PEP8 issues: line length and unused import
GKjohns Sep 7, 2017
4f1dc37
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 13, 2017
e89b9e4
use is_pairwise() to check for precomputed kernel
GKjohns Sep 14, 2017
c9c6a49
fix precomputed test/train matricies for check_class_weight_classifiers
GKjohns Sep 18, 2017
7894231
fix PEP8 issues
GKjohns Sep 18, 2017
bbdc67f
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 18, 2017
d3fcb3e
add final empty line
GKjohns Sep 18, 2017
ffeb68e
ensure check_sample_weights_pandas_series actually operates on pandas…
GKjohns Sep 18, 2017
298fa84
remove blank lines as end of file, flake8
GKjohns Sep 18, 2017
a631951
remove unused import
GKjohns Sep 18, 2017
b58e6bf
add estimator check for estimators that are based on a metric as well…
GKjohns Sep 19, 2017
e222145
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 19, 2017
273d8ee
add extra line, PEP8
GKjohns Sep 20, 2017
68bacdb
add check to ensure test_check_estimator_pairwise actually checks a p…
GKjohns Sep 20, 2017
0efc4a6
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 20, 2017
92cbe2b
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 27, 2017
142eab4
alter gram_matrix_if_pairwise to account for pairwise metrics
GKjohns Sep 28, 2017
14f5f5f
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Sep 28, 2017
c06e404
make test for 2d y features work
GKjohns Sep 28, 2017
7c7f3c4
refactor is_pairwise_metric()
GKjohns Sep 28, 2017
7d53c90
remove extra line
GKjohns Sep 28, 2017
9fd424e
fix grammar in docstring
GKjohns Sep 28, 2017
e10fafd
fix gram_matrix_if_pairwise to accept flat 1-D X vector in python 3
GKjohns Sep 28, 2017
85bed8a
remove extra spaces
GKjohns Oct 2, 2017
3b9dd25
remove unused import in utils/estimator_checks.py
GKjohns Oct 6, 2017
dd94814
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 6, 2017
1e9886e
manually create distance matrix for gram_matrix_if_pairwise()
GKjohns Oct 6, 2017
78654a1
PEP8, add a space
GKjohns Oct 6, 2017
3777255
remove unused import
GKjohns Oct 6, 2017
4213c5a
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 11, 2017
4d6cc73
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 16, 2017
9b92c7b
make check_classifiers_train() check shape for pairwise estimators, f…
GKjohns Oct 18, 2017
687204f
use pairwise_distance() to create distance matrix
GKjohns Oct 18, 2017
c67242e
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 18, 2017
cb095fc
rename gram_matrix_if_pairwise() to maybe_pairwise(). refactor check_…
GKjohns Oct 18, 2017
eef2acc
cast X to numpy array in check_sample_weights_pandas_series() so mayb…
GKjohns Oct 19, 2017
0bdb936
PEP8 fix indentation
GKjohns Oct 19, 2017
84d8615
make is_pairwise helper functions private. Remove them from base. Con…
GKjohns Oct 20, 2017
f7b76d9
make check_estimator_sparse_data() acknowledge 'Sparse' as well as 's…
GKjohns Oct 23, 2017
42a508d
remove kneighbors tests for sparse matricies, not supported
GKjohns Oct 24, 2017
adf110a
bring tests for kneighbors on sparse data back, check for ValueError
GKjohns Oct 24, 2017
35ae372
fix check_estimator_sparse_data() test to account for TypeError and V…
GKjohns Oct 24, 2017
cd80816
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 24, 2017
688376f
PEP8 add newlines
GKjohns Oct 24, 2017
6f11f6a
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 25, 2017
a3e7b64
add Y argument (the input vector again) to pairwise_distances() hopef…
GKjohns Oct 25, 2017
c4aae76
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 27, 2017
2b131f3
use euclidean distance in maybe_pairwise() to try and mitigate weird …
GKjohns Oct 27, 2017
f776d1e
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 27, 2017
6259e79
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Oct 30, 2017
834918c
change maybe_pairwise() name to pairwise_estimator_convert_X()
GKjohns Oct 30, 2017
5032f35
change test_kneighbors_regressor_sparse() to only check for error on …
GKjohns Oct 30, 2017
892771f
PEP8 fix line length
GKjohns Oct 30, 2017
ad65839
PEP8 again
GKjohns Oct 30, 2017
b56899b
change check_classifiers_train() test to check pairwise eatimators fo…
GKjohns Nov 1, 2017
7050908
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Nov 1, 2017
e055fb8
PEP8 line length fix
GKjohns Nov 1, 2017
efeb067
update whats_new with changes to estimator checks
GKjohns Nov 1, 2017
8c9b92f
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Nov 5, 2017
3116c23
add change details to whats_new
GKjohns Nov 5, 2017
42fa8f4
remove unused lines in estimator_checks.pairwise_estimator_convert_X()
GKjohns Nov 6, 2017
69d7876
remove assert_raises() for precomputed metric in test_kneighbors_regr…
GKjohns Nov 6, 2017
3223b23
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
GKjohns Nov 9, 2017
44f4dd6
check if test data is sparse, check for ValueError instead of accurac…
GKjohns Nov 9, 2017
b5ede88
remove redundant backslash
GKjohns Nov 9, 2017
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
7 changes: 7 additions & 0 deletions doc/whats_new/v0.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,10 @@ Cluster
- Deprecate ``pooling_func`` unused parameter in
:class:`cluster.AgglomerativeClustering`. :issue:`9875` by :user:`Kumar Ashutosh
<thechargedneutron>`.

Changes to estimator checks
---------------------------

- Allow tests in :func:`estimator_checks.check_estimator` to test functions
that accept pairwise data.
:issue:`9701` by :user:`Kyle Johnson <gkjohns>`
1 change: 0 additions & 1 deletion sklearn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ def is_classifier(estimator):
def is_regressor(estimator):
"""Returns True if the given estimator is (probably) a regressor.


Parameters
----------
estimator : object
Expand Down
6 changes: 6 additions & 0 deletions sklearn/neighbors/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# License: BSD 3 clause (C) INRIA, University of Amsterdam

import numpy as np
from scipy.sparse import issparse

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

neigh_dist, neigh_ind = self.kneighbors(X)
Expand Down
14 changes: 13 additions & 1 deletion sklearn/neighbors/tests/test_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
from scipy.sparse import (bsr_matrix, coo_matrix, csc_matrix, csr_matrix,
dok_matrix, lil_matrix)
dok_matrix, lil_matrix, issparse)

from sklearn import metrics
from sklearn import neighbors, datasets
Expand Down Expand Up @@ -731,10 +731,22 @@ def test_kneighbors_regressor_sparse(n_samples=40,
knn = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
algorithm='auto')
knn.fit(sparsemat(X), y)

knn_pre = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
metric='precomputed')
knn_pre.fit(pairwise_distances(X, metric='euclidean'), y)

for sparsev in SPARSE_OR_DENSE:
X2 = sparsev(X)
assert_true(np.mean(knn.predict(X2).round() == y) > 0.95)

X2_pre = sparsev(pairwise_distances(X, metric='euclidean'))
if issparse(sparsev(X2_pre)):
assert_raises(ValueError, knn_pre.predict, X2_pre)
else:
assert_true(
np.mean(knn_pre.predict(X2_pre).round() == y) > 0.95)


def test_neighbors_iris():
# Sanity checks on the iris dataset
Expand Down
Loading