Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG+1] learning_curve takes error_score parameter and propagates it. #7397

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

Merged
merged 1 commit into from
Sep 30, 2016
Merged
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
16 changes: 12 additions & 4 deletions sklearn/learning_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

def learning_curve(estimator, X, y, train_sizes=np.linspace(0.1, 1.0, 5),
cv=None, scoring=None, exploit_incremental_learning=False,
n_jobs=1, pre_dispatch="all", verbose=0):
n_jobs=1, pre_dispatch="all", verbose=0,
error_score='raise'):
"""Learning curve.

Determines cross-validated training and test scores for different training
Expand Down Expand Up @@ -75,7 +76,7 @@ def learning_curve(estimator, X, y, train_sizes=np.linspace(0.1, 1.0, 5),
- An iterable yielding train/test splits.

For integer/None inputs, if the estimator is a classifier and ``y`` is
either binary or multiclass,
either binary or multiclass,
:class:`sklearn.model_selection.StratifiedKFold` is used. In all
other cases, :class:`sklearn.model_selection.KFold` is used.

Expand All @@ -102,6 +103,12 @@ def learning_curve(estimator, X, y, train_sizes=np.linspace(0.1, 1.0, 5),
verbose : integer, optional
Controls the verbosity: the higher, the more messages.

error_score : 'raise' (default) or numeric
Value to assign to the score if an error occurs in estimator fitting.
If set to 'raise', the error is raised. If a numeric value is given,
FitFailedWarning is raised. This parameter does not affect the refit
step, which will always raise the error.

Returns
-------
train_sizes_abs : array, shape = (n_unique_ticks,), dtype int
Expand Down Expand Up @@ -156,7 +163,8 @@ def learning_curve(estimator, X, y, train_sizes=np.linspace(0.1, 1.0, 5),
else:
out = parallel(delayed(_fit_and_score)(
clone(estimator), X, y, scorer, train[:n_train_samples], test,
verbose, parameters=None, fit_params=None, return_train_score=True)
verbose, parameters=None, fit_params=None, return_train_score=True,
error_score=error_score)
for train, test in cv for n_train_samples in train_sizes_abs)
out = np.array(out)[:, :2]
n_cv_folds = out.shape[0] // n_unique_ticks
Expand Down Expand Up @@ -289,7 +297,7 @@ def validation_curve(estimator, X, y, param_name, param_range, cv=None,
- An iterable yielding train/test splits.

For integer/None inputs, if the estimator is a classifier and ``y`` is
either binary or multiclass,
either binary or multiclass,
:class:`sklearn.model_selection.StratifiedKFold` is used. In all
other cases, :class:`sklearn.model_selection.KFold` is used.

Expand Down
27 changes: 27 additions & 0 deletions sklearn/tests/test_learning_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def _is_training_data(self, X):
return X is self.X_subset


class MockEstimatorFailing(BaseEstimator):
"""Dummy classifier to test error_score in learning curve"""
def fit(self, X_subset, y_subset):
raise ValueError()

def score(self, X=None, y=None):
return None


def test_learning_curve():
X, y = make_classification(n_samples=30, n_features=1, n_informative=1,
n_redundant=0, n_classes=2,
Expand Down Expand Up @@ -136,6 +145,24 @@ def test_learning_curve_verbose():
assert("[learning_curve]" in out)


def test_learning_curve_error_score():
X, y = make_classification(n_samples=30, n_features=1, n_informative=1,
n_redundant=0, n_classes=2,
n_clusters_per_class=1, random_state=0)
estimator = MockEstimatorFailing()
_, _, test_scores = learning_curve(estimator, X, y, cv=3, error_score=0)
all_zeros = not np.any(test_scores)
assert(all_zeros)


def test_learning_curve_error_score_default_raise():
X, y = make_classification(n_samples=30, n_features=1, n_informative=1,
n_redundant=0, n_classes=2,
n_clusters_per_class=1, random_state=0)
estimator = MockEstimatorFailing()
assert_raises(ValueError, learning_curve, estimator, X, y, cv=3)


def test_learning_curve_incremental_learning_not_possible():
X, y = make_classification(n_samples=2, n_features=1, n_informative=1,
n_redundant=0, n_classes=2,
Expand Down