Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[MRG] Fix bug in graph lasso when n_features=2 (#13276)
* fix bug in graph lasso

* better fix

* add test for case n_features=2

* remove test for deprecated version

* update whats new

* note that the bug was a regression
  • Loading branch information
bellet authored and agramfort committed Feb 27, 2019
1 parent 3e715fd commit adc1e59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doc/whats_new/v0.20.rst
Expand Up @@ -29,6 +29,13 @@ Changelog
negative indexes in the columns list of the transformers.
:issue:`12946` by :user:`Pierre Tallotte <pierretallotte>`.

:mod:`sklearn.covariance`
......................

- |Fix| Fixed a regression in :func:`covariance.graphical_lasso` so that
the case `n_features=2` is handled correctly. :issue:`13276` by
:user:`Aurélien Bellet <bellet>`.

:mod:`sklearn.decomposition`
............................

Expand Down
2 changes: 1 addition & 1 deletion sklearn/covariance/graph_lasso_.py
Expand Up @@ -204,7 +204,7 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4,
# https://github.com/scikit-learn/scikit-learn/issues/4134
d_gap = np.inf
# set a sub_covariance buffer
sub_covariance = np.ascontiguousarray(covariance_[1:, 1:])
sub_covariance = np.copy(covariance_[1:, 1:], order='C')
for i in range(max_iter):
for idx in range(n_features):
# To keep the contiguous matrix `sub_covariance` equal to
Expand Down
17 changes: 17 additions & 0 deletions sklearn/covariance/tests/test_graphical_lasso.py
Expand Up @@ -85,6 +85,23 @@ def test_graphical_lasso_iris():
assert_array_almost_equal(icov, icov_R)


def test_graph_lasso_2D():
# Hard-coded solution from Python skggm package
# obtained by calling `quic(emp_cov, lam=.1, tol=1e-8)`
cov_skggm = np.array([[3.09550269, 1.186972],
[1.186972, 0.57713289]])

icov_skggm = np.array([[1.52836773, -3.14334831],
[-3.14334831, 8.19753385]])
X = datasets.load_iris().data[:, 2:]
emp_cov = empirical_covariance(X)
for method in ('cd', 'lars'):
cov, icov = graphical_lasso(emp_cov, alpha=.1, return_costs=False,
mode=method)
assert_array_almost_equal(cov, cov_skggm)
assert_array_almost_equal(icov, icov_skggm)


def test_graphical_lasso_iris_singular():
# Small subset of rows to test the rank-deficient case
# Need to choose samples such that none of the variances are zero
Expand Down

0 comments on commit adc1e59

Please sign in to comment.