-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-16312: fix wrong error raised by duplicated/conflicted constraints.
- Loading branch information
wendycwong
committed
Jul 1, 2024
1 parent
64a7c5d
commit fc5ac52
Showing
5 changed files
with
424 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
144 changes: 144 additions & 0 deletions
144
h2o-py/tests/testdir_algos/glm/pyunit_GH_16312_contrained_GLM_bad_constraints_large.py
This file contains 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,144 @@ | ||
import h2o | ||
from h2o.estimators.glm import H2OGeneralizedLinearEstimator as glm | ||
from tests import pyunit_utils | ||
import numpy as np | ||
import pandas as pd | ||
|
||
# no need to check anything, this test just needs to run into completion duplicating/conflicting constraints | ||
def data_prep(seed): | ||
np.random.seed(seed) | ||
x1 = np.random.normal(0, 10, 100000) | ||
x2 = np.random.normal(10, 100 , 100000) | ||
x3 = np.random.normal(20, 200, 100000) | ||
x4 = np.random.normal(30, 3000, 100000) | ||
x5 = np.random.normal(400, 4000, 100000) | ||
|
||
y_raw = np.sin(x1)*100 + np.sin(x2)*100 + x3/20 + x3/30 + x5/400 | ||
y = np.random.normal(y_raw, 20) | ||
|
||
data = { | ||
'x1': x1, | ||
'x2': x2, | ||
'x3': x3, | ||
'x4': x4, | ||
'x5': x5, | ||
'y': y, | ||
} | ||
return h2o.H2OFrame(pd.DataFrame(data)) | ||
|
||
def test_bad_lambda_specification(): | ||
train_data = data_prep(123) | ||
family = 'gaussian' | ||
link = 'identity' | ||
nfolds = 0 | ||
lambda_ = 0.0 | ||
seed = 1234 | ||
calc_like = True | ||
compute_p_values = True | ||
solver = 'irlsm' | ||
predictors = ['x1', 'x2', 'x3', 'x4', 'x5'] | ||
response = "y" | ||
|
||
linear_constraints2 = [] | ||
|
||
name = "x2" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x3" | ||
values = -1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = 0 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x3" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x4" | ||
values = -1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = 0 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x2" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x3" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x4" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = 0 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
linear_constraints = h2o.H2OFrame(linear_constraints2) | ||
linear_constraints.set_names(["names", "values", "types", "constraint_numbers"]) | ||
|
||
params = { | ||
"family" : family, | ||
"link": link, | ||
"lambda_" : lambda_, | ||
"seed" : seed, | ||
"nfolds" : nfolds, | ||
"compute_p_values" : compute_p_values, | ||
"calc_like" : calc_like, | ||
"solver" : solver, | ||
"linear_constraints": linear_constraints | ||
} | ||
|
||
model = glm(**params) | ||
model.train(x = predictors, y = response, training_frame = train_data) | ||
print(model.coef()) | ||
coef_constrained = model.coef() | ||
print(glm.getConstraintsInfo(model)) | ||
|
||
params = { | ||
"family" : family, | ||
"link": link, | ||
"lambda_" : lambda_, | ||
"seed" : seed, | ||
"nfolds" : nfolds, | ||
"compute_p_values" : compute_p_values, | ||
"calc_like" : calc_like, | ||
"solver" : solver, | ||
} | ||
|
||
model_no_constraints = glm(**params) | ||
model_no_constraints.train(x = predictors, y = response, training_frame = train_data) | ||
coef_no_constraints = model_no_constraints.coef() | ||
print(coef_no_constraints) | ||
|
||
if __name__ == "__main__": | ||
pyunit_utils.standalone_test(test_bad_lambda_specification) | ||
else: | ||
test_bad_lambda_specification() |
131 changes: 131 additions & 0 deletions
131
h2o-py/tests/testdir_algos/glm/pyunit_GH_16312_contrained_GLM_lambda_test_large.py
This file contains 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,131 @@ | ||
import h2o | ||
from h2o.estimators.glm import H2OGeneralizedLinearEstimator as glm | ||
from tests import pyunit_utils | ||
import numpy as np | ||
import pandas as pd | ||
|
||
# no need to check anything, this test just needs to run into completion without NPE error. | ||
def data_prep(seed): | ||
np.random.seed(seed) | ||
x1 = np.random.normal(0, 10, 100000) | ||
x2 = np.random.normal(10, 100 , 100000) | ||
x3 = np.random.normal(20, 200, 100000) | ||
x4 = np.random.normal(30, 3000, 100000) | ||
x5 = np.random.normal(400, 4000, 100000) | ||
|
||
y_raw = np.sin(x1)*100 + np.sin(x2)*100 + x3/20 + x3/30 + x5/400 | ||
y = np.random.normal(y_raw, 20) | ||
|
||
data = { | ||
'x1': x1, | ||
'x2': x2, | ||
'x3': x3, | ||
'x4': x4, | ||
'x5': x5, | ||
'y': y, | ||
} | ||
return h2o.H2OFrame(pd.DataFrame(data)) | ||
|
||
def test_bad_lambda_specification(): | ||
train_data = data_prep(123) | ||
family = 'gaussian' | ||
link = 'identity' | ||
nfolds = 0 | ||
lambda_ = 0.0 | ||
seed = 1234 | ||
calc_like = True | ||
compute_p_values = True | ||
solver = 'irlsm' | ||
predictors = ['x1', 'x2', 'x3', 'x4', 'x5'] | ||
response = "y" | ||
|
||
linear_constraints2 = [] | ||
|
||
name = "x2" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x3" | ||
values = -1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = 0 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x3" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x4" | ||
values = -1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = 0 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x2" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x3" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "x4" | ||
values = 1 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = 0 | ||
types = "LessThanEqual" | ||
contraint_numbers = 2 | ||
linear_constraints2.append([name, values, types, contraint_numbers]) | ||
|
||
|
||
linear_constraints = h2o.H2OFrame(linear_constraints2) | ||
linear_constraints.set_names(["names", "values", "types", "constraint_numbers"]) | ||
|
||
linear_constraints = h2o.H2OFrame(linear_constraints2) | ||
linear_constraints.set_names(["names", "values", "types", "constraint_numbers"]) | ||
# check lower bound of beta constraint will not generate error but lambda will. | ||
params = { | ||
"family" : family, | ||
"link": link, | ||
"lambda_" : lambda_, | ||
"seed" : seed, | ||
"nfolds" : nfolds, | ||
"compute_p_values" : compute_p_values, | ||
"calc_like" : calc_like, | ||
"solver" : solver, | ||
"linear_constraints": linear_constraints | ||
} | ||
|
||
model = glm(**params) | ||
model.train(x = predictors, y = response, training_frame = train_data) | ||
print(model.coef()) | ||
|
||
|
||
if __name__ == "__main__": | ||
pyunit_utils.standalone_test(test_bad_lambda_specification) | ||
else: | ||
test_bad_lambda_specification() |
Oops, something went wrong.