Skip to content

fix: validate transform_n_nonzero_coefs for OMP in DictionaryLearning - #33581

Closed
themavik wants to merge 2 commits into
scikit-learn:mainfrom
themavik:fix/33454-dictionary-learning-omp-validation
Closed

themavik wants to merge 2 commits into
scikit-learn:mainfrom
themavik:fix/33454-dictionary-learning-omp-validation

Conversation

@themavik

Copy link
Copy Markdown

Summary

Fixes #33454.
Root cause: DictionaryLearning with transform_algorithm=omp and transform_n_nonzero_coefs > n_components raised a confusing error from orthogonal_mp_gram ("The number of atoms cannot be more than the number of features") instead of validating the constraint upfront.
Fix: Added validation in the transform method to raise a clear ValueError when transform_n_nonzero_coefs > n_components with OMP algorithm.

Changes

  • sklearn/decomposition/_dict_learning.py: Added parameter validation for OMP algorithm constraints in _transform
  • sklearn/decomposition/tests/test_dict_learning.py: Added regression test

Testing

  • Verified fix provides a clear error message for the reported scenario
  • Change is minimal and follows existing validation patterns

…#33454)

Root cause: DictionaryLearning with transform_algorithm='omp' and
transform_n_nonzero_coefs > n_components raised a confusing error from
orthogonal_mp_gram instead of validating the constraint upfront.

Made-with: Cursor
@github-actions

github-actions Bot commented Mar 19, 2026 •

Copy link
Copy Markdown

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


ruff format

ruff detected issues. Please run ruff format locally and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.12.2.

Details

--- sklearn/decomposition/tests/test_dict_learning.py
+++ sklearn/decomposition/tests/test_dict_learning.py
@@ -133,7 +133,6 @@
         dict_learning(X, n_components, alpha=alpha, positive_code=True)
 
 
-
 def test_dict_learning_omp_transform_n_nonzero_coefs_validation():
     """Check that transform_n_nonzero_coefs > n_components raises clear error."""
     n_components = 5

1 file would be reformatted, 935 files already formatted

Generated for commit: 392cf69. Link to the linter CI: here

@betatim betatim closed this Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Linter failure The linter CI is failing on this PR module:decomposition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DictionaryLearner dies with 'omp' only

2 participants