Skip to content

Commit 50d40fb

Browse files
committed
FIX : omp default param could fail
1 parent 7c9a353 commit 50d40fb

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

sklearn/decomposition/dict_learning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
228228
if algorithm in ('lars', 'omp'):
229229
regularization = n_nonzero_coefs
230230
if regularization is None:
231-
regularization = max(n_features / 10, 1)
231+
regularization = min(max(n_features / 10, 1), n_components)
232232
else:
233233
regularization = alpha
234234
if regularization is None:

sklearn/decomposition/tests/test_dict_learning.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ def test_sparse_encode_error():
176176
assert_less(np.sqrt(np.sum((np.dot(code, V) - X) ** 2)), 0.1)
177177

178178

179+
def test_sparse_encode_error_default_sparsity():
180+
X = np.random.randn(100, 64)
181+
D = np.random.randn(2, 64)
182+
code = sparse_encode(X, D, algorithm='omp', n_nonzero_coefs=None)
183+
assert_equal(code.shape, (100, 2))
184+
185+
179186
def test_unknown_method():
180187
n_components = 12
181188
rng = np.random.RandomState(0)

0 commit comments

Comments
 (0)