Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-16208: Adding constrained GLM documentation to user guide #16394

Open
wants to merge 4 commits into
base: rel-3.46.0
Choose a base branch
from

Conversation

hannah-tillman
Copy link
Contributor

@hannah-tillman hannah-tillman added this to the 3.46.0.6 milestone Sep 18, 2024
@hannah-tillman hannah-tillman self-assigned this Sep 18, 2024
@wendycwong
Copy link
Contributor

wendycwong commented Sep 24, 2024

@hannah-tillman : Can you derive an python example from this:

def test_separate_linear_beta_gaussian():
'''
This test will check that when separate_linear_beta=True, those coefficients should be within the beta constraint
range.
'''
h2o_data = h2o.import_file(
path=pyunit_utils.locate("smalldata/glm_test/gaussian_20cols_10000Rows.csv"))
enum_columns = ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"]
for cname in enum_columns:
h2o_data[cname] = h2o_data[cname].asfactor()
myY = "C21"
myX = h2o_data.names.remove(myY)

linear_constraints = [] # this constraint is satisfied by default coefficient initialization
name = "C1.2"
values = 1
types = "Equal"
contraint_numbers = 0
linear_constraints.append([name, values, types, contraint_numbers])

name = "C11"
values = 1
types = "Equal"
contraint_numbers = 0
linear_constraints.append([name, values, types, contraint_numbers])

name = "constant"
values = 13.56 
types = "Equal"
contraint_numbers = 0
linear_constraints.append([name, values, types, contraint_numbers])

name = "C5.2"
values = 1
types = "LessThanEqual"
contraint_numbers = 1
linear_constraints.append([name, values, types, contraint_numbers])

name = "C12"
values = 1
types = "LessThanEqual"
contraint_numbers = 1
linear_constraints.append([name, values, types, contraint_numbers])

name = "C15"
values = 1
types = "LessThanEqual"
contraint_numbers = 1
linear_constraints.append([name, values, types, contraint_numbers])

name = "constant"
values = -5
types = "LessThanEqual"
contraint_numbers = 1
linear_constraints.append([name, values, types, contraint_numbers])

linear_constraints2 = h2o.H2OFrame(linear_constraints)
linear_constraints2.set_names(["names", "values", "types", "constraint_numbers"])

bc = []
name = "C1.1"
c1p1LowerBound = -36
c1p1UpperBound=-35
bc.append([name, c1p1LowerBound, c1p1UpperBound])

name = "C5.2"
c5p2LowerBound=-14
c5p2UpperBound=-13
bc.append([name, c5p2LowerBound, c5p2UpperBound])

name = "C11"
c11LowerBound=25
c11UpperBound=26
bc.append([name, c11LowerBound, c11UpperBound])

name = "C15"
c15LowerBound=14
c15UpperBound=15
bc.append([name, c15LowerBound, c15UpperBound])

beta_constraints = h2o.H2OFrame(bc)
beta_constraints.set_names(["names", "lower_bounds", "upper_bounds"])
      
m_sep = glm(family='gaussian', linear_constraints=linear_constraints2, solver="irlsm", lambda_=0.0,
             beta_constraints=beta_constraints, constraint_eta0=0.1, constraint_tau=10,
             constraint_alpha=0.01, constraint_beta=0.9, constraint_c0=100)
m_sep.train(training_frame=h2o_data,x=myX, y=myY)
coef_sep = m_sep.coef()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants