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
Update AutoML to use objective decision function during scoring for custom objectives #1934
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
bf4b7a7
init
angela97lin 7c751d3
Merge branch 'main' into 1868_objective
angela97lin 5ba8267
Merge branch 'main' into 1868_objective
angela97lin 6656538
Merge branch 'main' into 1868_objective
angela97lin ef32f72
Merge branch 'main' into 1868_objective
angela97lin 1ba4628
wip
angela97lin edaf8a0
Merge branch 'main' into 1868_objective
angela97lin 31667fc
this is kinda getting there
angela97lin 386c192
Merge branch '1868_objective' of github.com:alteryx/evalml into 1868_…
angela97lin fcb4afb
release notes
angela97lin 692532f
clean up
angela97lin 3425491
add test for coverage
angela97lin f3096a0
Merge branch 'main' into 1868_objective
angela97lin 0ac490f
Merge branch 'main' into 1868_objective
angela97lin 0bb3f88
Merge branch 'main' into 1868_objective
angela97lin 6107697
Merge branch 'main' into 1868_objective
angela97lin e549258
cleanup, add time series
angela97lin 11d70d4
Merge branch '1868_objective' of github.com:alteryx/evalml into 1868_…
angela97lin 70df88d
fix tests for time series mocks
angela97lin cbcc457
trying subclassing
angela97lin d4eaa6c
testing mixin
angela97lin aa54e25
fix tests and cleanup
angela97lin 6786b33
separate out time series test
angela97lin 0187e34
clean up _score_all_objectives
angela97lin 15656dc
remove commented out code
angela97lin a191d96
moved optimize_threshold to mixin
angela97lin cf4a911
clean up test for multiclass
angela97lin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import sys | ||
import traceback | ||
from collections import OrderedDict | ||
|
||
from evalml.exceptions import PipelineScoreError | ||
|
||
|
||
class BinaryClassificationPipelineMixin(): | ||
_threshold = None | ||
|
||
@property | ||
def threshold(self): | ||
"""Threshold used to make a prediction. Defaults to None.""" | ||
return self._threshold | ||
|
||
@threshold.setter | ||
def threshold(self, value): | ||
self._threshold = value | ||
|
||
def _predict_with_objective(self, X, ypred_proba, objective): | ||
ypred_proba = ypred_proba.iloc[:, 1] | ||
if objective is None: | ||
return ypred_proba > self.threshold | ||
return objective.decision_function(ypred_proba, threshold=self.threshold, X=X) | ||
|
||
def _compute_predictions(self, X, y, objectives, time_series=False): | ||
"""Compute predictions/probabilities based on objectives.""" | ||
y_predicted = None | ||
y_predicted_proba = None | ||
if any(o.score_needs_proba for o in objectives) or self.threshold is not None: | ||
y_predicted_proba = self.predict_proba(X, y) if time_series else self.predict_proba(X) | ||
if any(not o.score_needs_proba for o in objectives) and self.threshold is None: | ||
y_predicted = self._predict(X, y, pad=True) if time_series else self._predict(X) | ||
return y_predicted, y_predicted_proba | ||
|
||
def _score_all_objectives(self, X, y, y_pred, y_pred_proba, objectives): | ||
scored_successfully = OrderedDict() | ||
exceptions = OrderedDict() | ||
for objective in objectives: | ||
try: | ||
if not objective.is_defined_for_problem_type(self.problem_type): | ||
raise ValueError(f'Invalid objective {objective.name} specified for problem type {self.problem_type}') | ||
y_pred_to_use = y_pred | ||
if self.threshold is not None and not objective.score_needs_proba: | ||
y_pred_to_use = self._predict_with_objective(X, y_pred_proba, objective) | ||
score = self._score(X, y, y_pred_proba if objective.score_needs_proba else y_pred_to_use, objective) | ||
scored_successfully.update({objective.name: score}) | ||
except Exception as e: | ||
tb = traceback.format_tb(sys.exc_info()[2]) | ||
exceptions[objective.name] = (e, tb) | ||
if exceptions: | ||
# If any objective failed, throw an PipelineScoreError | ||
raise PipelineScoreError(exceptions, scored_successfully) | ||
# No objectives failed, return the scores | ||
return scored_successfully |
This file contains 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 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 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 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.
Moved all of this to
BinaryClassificationPipelineMixin