Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v0.20.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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