Skip to content

Commit 71a02e0

Browse files
author
William de Vazelhes
committed
Simplification for init of sdml
1 parent 4d61dba commit 71a02e0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

metric_learn/sdml.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ def _fit(self, pairs, y):
8282
# with a constant added so that they are all positive (plus an epsilon
8383
# to ensure definiteness). This is empirical.
8484
w, V = np.linalg.eigh(emp_cov)
85-
if any(w < 0.):
85+
min_eigval = np.min(w)
86+
if min_eigval < 0.:
8687
warnings.warn("Warning, the input matrix of graphical lasso is not "
8788
"positive semi-definite (PSD). The algorithm may diverge, "
8889
"and lead to degenerate solutions. "
8990
"To prevent that, try to decrease the balance parameter "
9091
"`balance_param` and/or to set use_covariance=False.",
9192
ConvergenceWarning)
92-
sigma0 = (V * (w - min(0, np.min(w)) + 1e-10)).dot(V.T)
93+
w -= min_eigval # we translate the eigenvalues to make them all positive
94+
w += 1e-10 # we add a small offset to avoid definiteness problems
95+
sigma0 = (V * w).dot(V.T)
9396
try:
9497
if HAS_SKGGM:
9598
theta0 = pinvh(sigma0)

0 commit comments

Comments
 (0)