-
Notifications
You must be signed in to change notification settings - Fork 37
ENH Add support for GLasso and Adaptive (reweighted) GLasso #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Perceptronium
wants to merge
16
commits into
scikit-learn-contrib:main
Choose a base branch
from
Perceptronium:graphical_lasso
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c199221
add support for graphical lasso and adaptive (reweighted) graphical l…
Perceptronium 5696d40
Update estimators to use barebones solver, make it at least as fast a…
Perceptronium 3e3df79
add Reweighted GLasso regularization path example
Perceptronium c9da9d4
Merge branch 'main' of https://github.com/scikit-learn-contrib/skglm …
Perceptronium ca6960f
fix issues in glasso reg path example
Perceptronium 4538002
fix glasso solver issues, move estimator to own file, create dedicate…
Perceptronium a4ea3fd
remove snakecase function names in test functions
Perceptronium ff9c2fe
Merge branch 'graphical_lasso' of https://github.com/Perceptronium/sk…
floriankozikowski 3148357
adjust weight updates, still need to test
floriankozikowski 082c00d
trying out different update methods, no success so far
floriankozikowski 7fc0f21
Merge branch 'main' of github.com:scikit-learn-contrib/skglm into gra…
mathurinm a4e192f
empty
floriankozikowski 33ca52c
add explicit handling of zeros in penalties.derivative, works now, to…
floriankozikowski fc70009
leave original name of old strategy based version, so tests dont fail
floriankozikowski a927e50
fix minor name dependency
floriankozikowski b62990d
ci trigger
mathurinm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Authors: Can Pouliquen | ||
# Mathurin Massias | ||
""" | ||
======================================================================= | ||
Regularization paths for the Graphical Lasso and its Adaptive variation | ||
======================================================================= | ||
Highlight the importance of using non-convex regularization for improved performance, | ||
solved using the reweighting strategy. | ||
""" | ||
|
||
import numpy as np | ||
from numpy.linalg import norm | ||
import matplotlib.pyplot as plt | ||
from sklearn.metrics import f1_score | ||
|
||
from skglm.covariance import GraphicalLasso, AdaptiveGraphicalLasso | ||
from skglm.utils.data import make_dummy_covariance_data | ||
|
||
|
||
p = 100 | ||
n = 1000 | ||
S, Theta_true, alpha_max = make_dummy_covariance_data(n, p) | ||
alphas = alpha_max*np.geomspace(1, 1e-4, num=10) | ||
|
||
penalties = [ | ||
"L1", | ||
"Log", | ||
"L0.5", | ||
"MCP", | ||
] | ||
n_reweights = 5 | ||
models_tol = 1e-4 | ||
models = [ | ||
GraphicalLasso(algo="primal", | ||
warm_start=True, | ||
tol=models_tol), | ||
AdaptiveGraphicalLasso(warm_start=True, | ||
strategy="log", | ||
n_reweights=n_reweights, | ||
tol=models_tol), | ||
AdaptiveGraphicalLasso(warm_start=True, | ||
strategy="sqrt", | ||
n_reweights=n_reweights, | ||
tol=models_tol), | ||
AdaptiveGraphicalLasso(warm_start=True, | ||
strategy="mcp", | ||
n_reweights=n_reweights, | ||
tol=models_tol), | ||
] | ||
|
||
my_glasso_nmses = {penalty: [] for penalty in penalties} | ||
my_glasso_f1_scores = {penalty: [] for penalty in penalties} | ||
|
||
sk_glasso_nmses = [] | ||
sk_glasso_f1_scores = [] | ||
|
||
|
||
for i, (penalty, model) in enumerate(zip(penalties, models)): | ||
for alpha_idx, alpha in enumerate(alphas): | ||
print(f"======= {penalty} penalty, alpha {alpha_idx+1}/{len(alphas)} =======") | ||
model.alpha = alpha | ||
model.fit(S) | ||
Theta = model.precision_ | ||
|
||
my_nmse = norm(Theta - Theta_true)**2 / norm(Theta_true)**2 | ||
|
||
my_f1_score = f1_score(Theta.flatten() != 0., | ||
Theta_true.flatten() != 0.) | ||
|
||
my_glasso_nmses[penalty].append(my_nmse) | ||
my_glasso_f1_scores[penalty].append(my_f1_score) | ||
|
||
|
||
plt.close('all') | ||
fig, axarr = plt.subplots(2, 1, sharex=True, figsize=([6.11, 3.91]), | ||
layout="constrained") | ||
cmap = plt.get_cmap("tab10") | ||
for i, penalty in enumerate(penalties): | ||
|
||
for j, ax in enumerate(axarr): | ||
|
||
if j == 0: | ||
metric = my_glasso_nmses | ||
best_idx = np.argmin(metric[penalty]) | ||
ystop = np.min(metric[penalty]) | ||
else: | ||
metric = my_glasso_f1_scores | ||
best_idx = np.argmax(metric[penalty]) | ||
ystop = np.max(metric[penalty]) | ||
|
||
ax.semilogx(alphas/alpha_max, | ||
metric[penalty], | ||
color=cmap(i), | ||
linewidth=2., | ||
label=penalty) | ||
|
||
ax.vlines( | ||
x=alphas[best_idx] / alphas[0], | ||
ymin=0, | ||
ymax=ystop, | ||
linestyle='--', | ||
color=cmap(i)) | ||
line = ax.plot( | ||
[alphas[best_idx] / alphas[0]], | ||
0, | ||
clip_on=False, | ||
marker='X', | ||
color=cmap(i), | ||
markersize=12) | ||
|
||
ax.grid(which='both', alpha=0.9) | ||
|
||
axarr[0].legend(fontsize=14) | ||
axarr[0].set_title(f"{p=},{n=}", fontsize=18) | ||
axarr[0].set_ylabel("NMSE", fontsize=18) | ||
axarr[1].set_ylabel("F1 score", fontsize=18) | ||
axarr[1].set_xlabel(r"$\lambda / \lambda_\mathrm{{max}}$", fontsize=18) | ||
|
||
plt.show(block=False) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.