Skip to content

learning_curve with exploit_incremental_learning=True fails to compute training scores when sample_weight is provided #33283

Closed

Description

@atheendre130505

Describe the bug and give evidence about its user-facing impact

When using learning_curve with exploit_incremental_learning=True and passing sample_weight
(or other parameters) to the scorer, the training scores for all training sizes except the final one are returned as NaN.

This happens because _incremental_fit_estimator pre-computes the training scores' parameters once for the full training set, but then attempts to use them for subsets of the training data. This causes a ValueError (consistent length mismatch) inside the scorer, which is silently caught and replaced with NaN.

This affects any user trying to evaluate learning curves incrementally while using weighted metrics or metadata-aware scorers.

Steps/Code to Reproduce

import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import learning_curve
from sklearn.metrics import make_scorer, accuracy_score
import sklearn

# Enable metadata routing to pass sample_weight to scorer
sklearn.set_config(enable_metadata_routing=True)

X, y = make_classification(n_samples=100, n_features=5, random_state=42)
weights = np.ones(100)
scorer = make_scorer(accuracy_score).set_score_request(sample_weight=True)

train_sizes, train_scores, _ = learning_curve(
    SGDClassifier(), X, y, 
    train_sizes=[0.3, 0.6, 0.9],
    cv=3,
    exploit_incremental_learning=True,
    scoring=scorer,
    params={'sample_weight': weights}
)

print("Training scores (expecting no NaNs):")
print(train_scores)

Expected Results

The training scores should be successfully computed for all training sizes. Example: [[0.96 0.96 0.96], [0.94 0.94 0.94], [0.92 0.92 0.92]] (no NaN values).

Actual Results

[[ nan  nan  nan]
 [ nan  nan  nan]
 [0.93 0.95 0.92]]

Versions

System:
    python: 3.14.2
    machine: Windows-10
Python dependencies:
      sklearn: 1.8.0.dev0
        numpy: 2.4.1
        scipy: 1.17.0

Interest in fixing the bug

Yes, I am interested in fixing this.

Activity

  1. juanf-0gravity commented on Feb 14, 2026

    @juanf-0gravity
  2. StefanieSenger commented on Feb 15, 2026

    @StefanieSenger
    Member

    Hi @atheendre130505, thanks for reporting.

    This is indeed not only a bug, but two bugs (and at least one is my fault 😆).

    Also, nice fix @juanf-0gravity. I've seen it too late.

  3. removed
    Needs Triage
    on Feb 15, 2026
  4. atheendre130505 commented on Feb 15, 2026

    @atheendre130505
    Author

    @juanf-0gravity @StefanieSenger , has the issue been solved?, It has only been triaged now.
    would like to make a pr for this, if any changes are yet to be made(intent to fix the bug was raised with the issue). thankyou.

  5. juanf-0gravity commented on Feb 15, 2026

    @juanf-0gravity
  6. StefanieSenger commented on Feb 15, 2026

    @StefanieSenger
    Member

    the fit_params also aren't being subsetted properly for partial_fit batches inside the loop. That one's still up for grabs if you want to take it @atheendre130505!

    I've addressed this already in ##33285.

    Next steps are reviewing the two PRs.

  7. StefanieSenger commented on Feb 15, 2026

    @StefanieSenger
    Member

    On a different note, @atheendre130505: The reproducer code would not run without setting set_fit_request and set_partial_fit_request on SGDClassifier. In sklearn 1.8.0.dev0, too, you would have gotten an UnsetMetadataPassedError. How did you come across that issue?

  8. adrinjalali commented on Feb 16, 2026

    @adrinjalali
    Member

    @atheendre130505 it seems you've autogenerated this issue with some AI agents, and doesn't seem to be coming from a real usecase. This hurts us because it diverts energy and focus from other projects which actually impact many users.

    @juanf-0gravity it seems your PR is also autogenerated and a complete fix. The PR was also opened before "Needs Triage" was removed, which shows you haven't read our contributing guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions