-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[WIP] ENH Adds caching to multimetric scoring with a wrapper class #14261
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
994ca84
ENH Uses class based caching
thomasjpfan ae8d7a4
BUG Fixes imports
thomasjpfan c26f94e
ENH Only caches when used with make_scorer
thomasjpfan 6c343bf
DOC Adds whats_new
thomasjpfan 9e7b059
CLN Removes __class__ hack
thomasjpfan 62e85ee
CLN Removes attribute passthrough
thomasjpfan d5a00af
TST Uses context manager
thomasjpfan f047166
CLN Adds id of data
thomasjpfan 2b761a3
BUG Pass X as well
thomasjpfan 9733db9
BUG Adds setter to properties
thomasjpfan 900baf2
BUG Fixes property
thomasjpfan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | 8000Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
import numbers | ||
import time | ||
from traceback import format_exception_only | ||
from contextlib import contextmanager | ||
from functools import partial | ||
|
||
import numpy as np | ||
import scipy.sparse as sp | ||
|
@@ -24,7 +26,8 @@ | |
_message_with_time) | ||
from ..utils.validation import _is_arraylike, _num_samples | ||
from ..utils.metaestimators import _safe_split | ||
from ..metrics.scorer import check_scoring, _check_multimetric_scoring | ||
from ..metrics.scorer import (check_scoring, _check_multimetric_scoring, | ||
_BaseScorer) | ||
from ..exceptions import FitFailedWarning | ||
from ._split import check_cv | ||
from ..preprocessing import LabelEncoder | ||
|
@@ -596,15 +599,43 @@ def _score(estimator, X_test, y_test, scorer, is_multimetric=False): | |
return score | ||
|
||
|
||
@contextmanager | ||
def _cache_estimator(estimator): | ||
|
||
def _call_func(X, *args, name=None, func=None, cache=None, **kwargs): | ||
try: | ||
return cache[name, id(X)] | ||
except KeyError: | ||
result = func(X, *args, **kwargs) | ||
cache[name, id(X)] = result | ||
return result | ||
|
||
cache = {} | ||
names = ['predict', 'predict_proba', 'decision_function', 'score'] | ||
cache_funcs = {name: getattr(estimator, name) for name in names if | ||
hasattr(estimator, name)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well also check that it is callable?? |
||
|
||
# patch methods | ||
for name, func in cache_funcs.items(): | ||
setattr(estimator, name, | ||
partial(_call_func, name=name, func=func, cache=cache)) | ||
yield estimator | ||
|
||
# place methods back | ||
for name, func in cache_funcs.items(): | ||
setattr(estimator, name, func) | ||
|
||
|
||
def _multimetric_score(estimator, X_test, y_test, scorers): | ||
"""Return a dict of score for multimetric scoring""" | ||
scores = {} | ||
|
||
for name, scorer in scorers.items(): | ||
if y_test is None: | ||
score = scorer(estimator, X_test) | ||
else: | ||
score = scorer(estimator, X_test, y_test) | ||
with _cache_estimator(estimator) as cached_estimator: | ||
if y_test is None: | ||
score = scorer(cached_estimator, X_test) | ||
else: | ||
score = scorer(cached_estimator, X_test, y_test) | ||
|
||
if hasattr(score, 'item'): | ||
try: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name it orig_funcs