Skip to content

[WIP] Choose out of bag scoring metric. Fixes #3455 #3723

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions sklearn/ensemble/bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ..externals.six import with_metaclass
from ..externals.six.moves import zip
from ..metrics import r2_score, accuracy_score
from ..metrics.scorer import check_scoring, get_score
from ..tree import DecisionTreeClassifier, DecisionTreeRegressor
from ..utils import check_random_state, check_X_y, check_array, column_or_1d
from ..utils.random import sample_without_replacement
Expand All @@ -29,6 +30,42 @@
MAX_INT = np.iinfo(np.int32).max


def oob_score(estimator, X, y, scoring, fit_params=None):
"""Compute an estimator's out of bag score.

Parameters
----------
estimator : estimator object implementing 'fit'
The object to use to fit the data.

X : array-like
The data to fit. Can be, for example a list, or an array at least 2d.

y : array-like
The target variable to try to predict.

scoring : string or callable
A string (see model evaluation documentation) or
a scorer callable object / function with signature
``scorer(estimator, X, y)``.

fit_params : dict, optional
Parameters to pass to the fit method of the estimator.

Returns
-------
score : float
Out of bag score of the estimator.
"""

estimator.oob_predict = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the set_params API.

scorer = check_scoring(estimator, scoring=scoring)
fit_params = fit_params if fit_params is not None else {}
estimator.fit(X, y, **fit_params)
return get_score(scorer, y, estimator.oob_prediction_,
estimator.oob_prediction_proba_, estimator.oob_decision_function_)


def _parallel_build_estimators(n_estimators, ensemble, X, y, sample_weight,
seeds, verbose):
"""Private function used to build a batch of estimators within a job."""
Expand Down
Loading