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

[MRG] add _pairwise property to BaseSearchCV #13925

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions sklearn/model_selection/_search.py
Expand Up @@ -398,6 +398,11 @@ def __init__(self, estimator, scoring=None, n_jobs=None, iid='deprecated',
def _estimator_type(self):
return self.estimator._estimator_type

@property
def _pairwise(self):
# allows cross-validation to see 'precomputed' metrics
return getattr(self.estimator, '_pairwise', False)

def score(self, X, y=None):
"""Returns the score on the given data, if the estimator has been refit.

Expand Down
27 changes: 27 additions & 0 deletions sklearn/model_selection/tests/test_search.py
Expand Up @@ -1787,3 +1787,30 @@ def get_n_splits(self, *args, **kw):
'inconsistent results. Expected \\d+ '
'splits, got \\d+'):
ridge.fit(X[:train_size], y[:train_size])


def test_search_cv__pairwise_property():
"""
Test implementation of BaseSearchCV has the _pairwise property
which matches the _pairwise property of its estimator.
"""

class PairwiseCV(BaseSearchCV):
Copy link
Member

Choose a reason for hiding this comment

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

why not using GridSearchCV instead of creating a dummy class?

def __init__(self, estimator, **kwargs):
super().__init__(estimator, **kwargs)

# simple estimator with _pairwise property
est = BaseEstimator()

attr_message = "BaseSearchCV _pairwise property must match estimator"

# check estimator with and without _pairwise
for _pairwise_setting in [True, False]:
setattr(est, '_pairwise', _pairwise_setting)
cv = PairwiseCV(est)

# check if cv is pairwise
Copy link
Member

Choose a reason for hiding this comment

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

this comment is redundant with the variable name

cv_is_pairwise = getattr(cv, '_pairwise', False)
Copy link
Member

Choose a reason for hiding this comment

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

why not just use cv._pairwise?

Copy link
Author

Choose a reason for hiding this comment

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

I was following the convention of safe_split but hit the 80 character limit so I broke it into a separate line. I suppose since checking cv._pairwise won't throw an error for not being instantiated, we can just use it here. I'll make the swap


# verity the _pairwise property in cv matches est
assert _pairwise_setting == cv_is_pairwise, attr_message