Skip to content

Commit 21dd86c

Browse files
issue #297: visualize group sparsity in example group_logistic_regression (#299)
1 parent af83e70 commit 21dd86c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

examples/plot_group_logistic_regression.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from skglm.solvers import GroupProxNewton
1717
from skglm.utils.data import make_correlated_data, grp_converter
1818

19+
import matplotlib.pyplot as plt
20+
1921
n_features = 30
2022
X, y, _ = make_correlated_data(
2123
n_samples=10, n_features=30, random_state=0)
@@ -42,3 +44,23 @@
4244
# %%
4345
# Fit check that groups are either all 0 or all non zero
4446
print(clf.coef_.reshape(-1, grp_size))
47+
48+
# %%
49+
# Visualise group-level sparsity
50+
51+
coef_by_group = clf.coef_.reshape(-1, grp_size)
52+
group_norms = np.linalg.norm(coef_by_group, axis=1)
53+
54+
plt.figure(figsize=(8, 4))
55+
plt.bar(np.arange(n_groups), group_norms)
56+
plt.xlabel("Group index")
57+
plt.ylabel("L2 norm of coefficients")
58+
plt.title("Group Sparsity Pattern")
59+
plt.tight_layout()
60+
plt.show()
61+
62+
# %%
63+
# This plot shows the L2 norm of the coefficients for each group.
64+
# Groups with a zero norm have been set inactive by the model,
65+
# illustrating how Group Logistic Regression enforces sparsity at the group level.
66+
# (Note: This example uses a tiny synthetic dataset, so the pattern has limited interpretability.)

0 commit comments

Comments
 (0)