-
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.
completed python test on collinear columns.
- Loading branch information
wendycwong
committed
Jan 29, 2024
1 parent
1cbee81
commit bf9c7e9
Showing
6 changed files
with
135 additions
and
134 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
87 changes: 87 additions & 0 deletions
87
h2o-py/tests/testdir_algos/glm/pyunit_GH_6722_constraints_binomial.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,87 @@ | ||
import h2o | ||
from h2o.estimators.glm import H2OGeneralizedLinearEstimator | ||
from tests import pyunit_utils | ||
|
||
# In this test, I setup inactive constraints and the coefficients with and without the constraints should be similar. | ||
def test_constraints_binomial(): | ||
train = h2o.import_file(path=pyunit_utils.locate("smalldata/glm_test/binomial_20_cols_10KRows.csv")) | ||
for ind in range(10): | ||
train[ind] = train[ind].asfactor() | ||
train["C21"] = train["C21"].asfactor() | ||
response = "C21" | ||
predictors = list(range(0,20)) | ||
|
||
loose_init_const = [] # this constraint is satisfied by default coefficient initialization | ||
|
||
h2o_glm = H2OGeneralizedLinearEstimator(family="binomial", compute_p_values=True, remove_collinear_columns=True, | ||
lambda_=0.0, solver="irlsm", seed=12345, standardize=True) | ||
h2o_glm.train(x=predictors, y=response, training_frame=train) | ||
print(h2o_glm.coef()) | ||
|
||
name = "C19" | ||
values = 0.5 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
loose_init_const.append([name, values, types, contraint_numbers]) | ||
|
||
name = "C20" | ||
values = -0.8 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
loose_init_const.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = -10 # 490 | ||
types = "LessThanEqual" | ||
contraint_numbers = 0 | ||
loose_init_const.append([name, values, types, contraint_numbers]) | ||
|
||
name = "C12" | ||
values = 2 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
loose_init_const.append([name, values, types, contraint_numbers]) | ||
|
||
name = "C13" | ||
values = -3 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
loose_init_const.append([name, values, types, contraint_numbers]) | ||
|
||
name = "constant" | ||
values = -25 # 80 | ||
types = "LessThanEqual" | ||
contraint_numbers = 1 | ||
loose_init_const.append([name, values, types, contraint_numbers]) | ||
|
||
linear_constraints2 = h2o.H2OFrame(loose_init_const) | ||
linear_constraints2.set_names(["names", "values", "types", "constraint_numbers"]) | ||
# GLM model with default coef init values | ||
h2o_glm_no_init = H2OGeneralizedLinearEstimator(family="binomial", compute_p_values=True, remove_collinear_columns=True, | ||
lambda_=0.0, solver="irlsm", linear_constraints=linear_constraints2, seed=12345) | ||
h2o_glm_no_init.train(x=predictors, y=response, training_frame=train) | ||
# GLM model with GLM coefficients built without constraints | ||
startCoef = [-11.777796439159427, 11.145918459656793, -2.240633964081059, -9.670607471364166, -0.04431844452487864, | ||
1.2000980684830767, -12.867841086167251, 4.8420193776134255, -10.14685080419689, 10.702942010340731, | ||
0.07976027968540254, 0.44773830396822184, 0.09737389144010582, 14.973662194333558, 15.70206320835652, | ||
0.1556959973853935, -6.6746924507580205, 6.942172294878739, 3.7876501542686576, 2.9159771977964435, | ||
14.612327184401229, 7.192082757108297, 1.119764323405339, 7.153590379475222, -6.823123247481835, | ||
-9.77308069726764, -7.931619499230349, 6.91629118878521, 5.867602968167905, 23.314287012503986, | ||
5.617830967874223, 0.8813224259105378, 20.820182196763465, 4.232709126684858, -3.76117280137443, | ||
-10.4467656317731, -8.919929474793474, -12.750260480508844, -21.187033417068225, 0.9223288064815389, | ||
-16.59577334951634, -2.2764714329170515, -17.577171729916557, -18.51039273019755, -6.294489874768877, | ||
-16.24717884256297, 1.8769336927775133, -3.3038587822340184, -16.90849120430868, 4.833742270371561, | ||
1.5702458956118868, -12.40449842919434, -6.102490765991213, -11.496005185120135, -7.071687013766151, | ||
-3.850864374953074, -8.957320649739758, -7.468575396622829, 0.5634858217928193, -6.818408867710658, | ||
-1.0014411907757605, -4.983361240945651, -9.280641693445801, 9.266595896543917, 7.3179414322586345, | ||
-5.057799375650119, -11.176969277606156, 35.87638467667283] | ||
h2o_glm_init = H2OGeneralizedLinearEstimator(family="binomial", compute_p_values=True, remove_collinear_columns=True, | ||
lambda_=0.0, solver="irlsm", linear_constraints=linear_constraints2, | ||
startval=startCoef, seed=12345) | ||
h2o_glm_init.train(x=predictors, y=response, training_frame=train) | ||
print("Done") | ||
|
||
if __name__ == "__main__": | ||
pyunit_utils.standalone_test(test_constraints_binomial) | ||
else: | ||
test_constraints_binomial() |
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
82 changes: 0 additions & 82 deletions
82
h2o-py/tests/testdir_algos/glm/pyunit_GH_6722_constraints_on_collinear_cols3.py
This file was deleted.
Oops, something went wrong.