8000 FIX: issue #540, make omp robust to empty solution · seckcoder/scikit-learn@60118bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 60118bf

Browse files
committed
FIX: issue scikit-learn#540, make omp robust to empty solution
1 parent a6e7178 commit 60118bf

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

sklearn/linear_model/omp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def _cholesky_omp(X, y, n_nonzero_coefs, tol=None, copy_X=True):
6363

6464
alpha = np.dot(X.T, y)
6565
residual = y
66+
gamma = np.empty(0)
6667
n_active = 0
6768
indices = range(X.shape[1]) # keeping track of swapping
6869

@@ -157,6 +158,7 @@ def _gram_omp(Gram, Xy, n_nonzero_coefs, tol_0=None, tol=None,
157158
alpha = Xy
158159
tol_curr = tol_0
159160
delta = 0
161+
gamma = np.empty(0)
160162
n_active = 0
161163

162164
max_features = len(Gram) if tol is not None else n_nonzero_coefs

sklearn/linear_model/tests/test_omp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,12 @@ def test_swapped_regressors():
154154
gamma_hat_gram = orthogonal_mp_gram(G, new_Xy, 2)
155155
assert_equal(np.flatnonzero(gamma_hat), [0, 21])
156156
assert_equal(np.flatnonzero(gamma_hat_gram), [0, 21])
157+
158+
159+
def test_no_atoms():
160+
y_empty = np.zeros_like(y)
161+
Xy_empty = np.dot(X.T, y_empty)
162+
gamma_empty = orthogonal_mp(X, y_empty, 1)
163+
gamma_empty_gram = orthogonal_mp_gram(G, Xy_empty, 1)
164+
assert_equal(np.all(gamma_empty == 0), True)
165+
assert_equal(np.all(gamma_empty_gram == 0), True)

0 commit comments

Comments
 (0)
2A29
0