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
1 change: 1 addition & 0 deletions pyglmnet/pyglmnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ def _prox(self, beta, thresh):
result[idxs_to_update] = (beta[idxs_to_update] -
thresh * beta[idxs_to_update] /
group_norms[idxs_to_update])
result[~idxs_to_update] = 0.0

return result

Expand Down
19 changes: 18 additions & 1 deletion tests/test_pyglmnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,33 @@ def test_group_lasso():
beta[groups == 2] = 0.

# create an instance of the GLM class
glm_group = GLM(distr='softplus', alpha=1.)
glm_group = GLM(distr='softplus', alpha=1., reg_lambda=0.2, group=groups)

# simulate training data
np.random.seed(glm_group.random_state)
Xr = np.random.normal(0.0, 1.0, [n_samples, n_features])
yr = simulate_glm(glm_group.distr, beta0, beta, Xr)

# scale and fit
scaler = StandardScaler().fit(Xr)
glm_group.fit(scaler.transform(Xr), yr)

# count number of nonzero coefs for each group.
# in each group, coef must be [all nonzero] or [all zero].
beta = glm_group.beta_
group_ids = np.unique(groups)
for group_id in group_ids:
if group_id == 0:
continue

target_beta = beta[groups == group_id]
n_nonzero = (target_beta != 0.0).sum()
assert n_nonzero in (len(target_beta), 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the test as it's compact. But the problem is that it passes on master. Ideally, you want the test to fail on master branch so the bug does not happen again

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I added the following assertion to the test code.

assert (beta[groups != 0] == 0.0).any()

I made sure that this assertion assert False at master branch, and assert True after fixed.


# one of the groups must be [all zero]
assert np.any([beta[groups == group_id].sum() == 0 \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the trailing backslash here but I can live with it

for group_id in group_ids if group_id != 0])


def test_glmnet():
"""Test glmnet."""
Expand Down