Skip to content
Merged
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
22 changes: 22 additions & 0 deletions examples/plot_group_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from skglm.solvers import GroupProxNewton
from skglm.utils.data import make_correlated_data, grp_converter

import matplotlib.pyplot as plt

n_features = 30
X, y, _ = make_correlated_data(
n_samples=10, n_features=30, random_state=0)
Expand All @@ -42,3 +44,23 @@
# %%
# Fit check that groups are either all 0 or all non zero
print(clf.coef_.reshape(-1, grp_size))

# %%
# Visualise group-level sparsity

coef_by_group = clf.coef_.reshape(-1, grp_size)
group_norms = np.linalg.norm(coef_by_group, axis=1)

plt.figure(figsize=(8, 4))
plt.bar(np.arange(n_groups), group_norms)
plt.xlabel("Group index")
plt.ylabel("L2 norm of coefficients")
plt.title("Group Sparsity Pattern")
plt.tight_layout()
plt.show()

# %%
# This plot shows the L2 norm of the coefficients for each group.
# Groups with a zero norm have been set inactive by the model,
# illustrating how Group Logistic Regression enforces sparsity at the group level.
# (Note: This example uses a tiny synthetic dataset, so the pattern has limited interpretability.)