| 
14 | 14 | 
 
  | 
15 | 15 | from sklearn.linear_model import (orthogonal_mp, orthogonal_mp_gram,  | 
16 | 16 |                                   OrthogonalMatchingPursuit,  | 
17 |  | -                                  OrthogonalMatchingPursuitCV)  | 
 | 17 | +                                  OrthogonalMatchingPursuitCV,  | 
 | 18 | +                                  LinearRegression)  | 
18 | 19 | from sklearn.utils.fixes import count_nonzero  | 
 | 20 | +from sklearn.utils import check_random_state  | 
19 | 21 | from sklearn.datasets import make_sparse_coded_signal  | 
20 | 22 | 
 
  | 
21 | 23 | n_samples, n_features, n_nonzero_coefs, n_targets = 20, 30, 5, 3  | 
@@ -93,7 +95,6 @@ def test_bad_input():  | 
93 | 95 | 
 
  | 
94 | 96 | 
 
  | 
95 | 97 | def test_perfect_signal_recovery():  | 
96 |  | -    # XXX: use signal generator  | 
97 | 98 |     idx, = gamma[:, 0].nonzero()  | 
98 | 99 |     gamma_rec = orthogonal_mp(X, y[:, 0], 5)  | 
99 | 100 |     gamma_gram = orthogonal_mp_gram(G, Xy[:, 0], 5)  | 
@@ -218,3 +219,17 @@ def test_omp_cv():  | 
218 | 219 |                                     n_nonzero_coefs=ompcv.n_nonzero_coefs_)  | 
219 | 220 |     omp.fit(X, y_)  | 
220 | 221 |     assert_array_almost_equal(ompcv.coef_, omp.coef_)  | 
 | 222 | + | 
 | 223 | + | 
 | 224 | +def test_omp_reaches_least_squares():  | 
 | 225 | +    # Use small simple data; it's a sanity check but OMP can stop early  | 
 | 226 | +    rng = check_random_state(0)  | 
 | 227 | +    n_samples, n_features = (10, 8)  | 
 | 228 | +    n_targets = 3  | 
 | 229 | +    X = rng.randn(n_samples, n_features)  | 
 | 230 | +    Y = rng.randn(n_samples, n_targets)  | 
 | 231 | +    omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_features)  | 
 | 232 | +    lstsq = LinearRegression()  | 
 | 233 | +    omp.fit(X, Y)  | 
 | 234 | +    lstsq.fit(X, Y)  | 
 | 235 | +    assert_array_almost_equal(omp.coef_, lstsq.coef_)  | 
0 commit comments