|
17 | 17 | from ..externals.six import with_metaclass
|
18 | 18 | from ..externals.six.moves import zip
|
19 | 19 | from ..metrics import r2_score, accuracy_score
|
| 20 | +from ..metrics.scorer import check_scoring, get_score |
20 | 21 | from ..tree import DecisionTreeClassifier, DecisionTreeRegressor
|
21 | 22 | from ..utils import check_random_state, check_X_y, check_array, column_or_1d
|
22 | 23 | from ..utils.random import sample_without_replacement
|
|
29 | 30 | MAX_INT = np.iinfo(np.int32).max
|
30 | 31 |
|
31 | 32 |
|
| 33 | +def oob_score(estimator, X, y, scoring, fit_params=None): |
| 34 | + """Compute an estimator's out of bag score. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + estimator : estimator object implementing 'fit' |
| 39 | + The object to use to fit the data. |
| 40 | +
|
| 41 | + X : array-like |
| 42 | + The data to fit. Can be, for example a list, or an array at least 2d. |
| 43 | +
|
| 44 | + y : array-like |
| 45 | + The target variable to try to predict. |
| 46 | +
|
| 47 | + scoring : string or callable |
| 48 | + A string (see model evaluation documentation) or |
| 49 | + a scorer callable object / function with signature |
| 50 | + ``scorer(estimator, X, y)``. |
| 51 | +
|
| 52 | + fit_params : dict, optional |
| 53 | + Parameters to pass to the fit method of the estimator. |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + score : float |
| 58 | + Out of bag score of the estimator. |
| 59 | + """ |
| 60 | + |
| 61 | + estimator.oob_predict = True |
| 62 | + scorer = check_scoring(estimator, scoring=scoring) |
| 63 | + fit_params = fit_params if fit_params is not None else {} |
| 64 | + estimator.fit(X, y, **fit_params) |
| 65 | + return get_score(scorer, y, estimator.oob_prediction_, |
| 66 | + estimator.oob_prediction_proba_, estimator.oob_decision_function_) |
| 67 | + |
| 68 | + |
32 | 69 | def _parallel_build_estimators(n_estimators, ensemble, X, y, sample_weight,
|
33 | 70 | seeds, verbose):
|
34 | 71 | """Private function used to build a batch of estimators within a job."""
|
|
0 commit comments