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 guidem [nocheck] [nochecks] #16394

Merged
merged 6 commits into from
Oct 21, 2024

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()

@wendycwong
Copy link
Contributor

wendycwong commented Oct 14, 2024

@hannah-tillman :

The equivalent R example for constrained GLM is:

h2o_data <- h2o.importFile("https://s3.amazonaws.com/h2o-public-test-data/smalldata/glm_test/gaussian_20cols_10000Rows.csv")
enum_columns = c("C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10")
for (cname in enum_columns)
h2o.asfactor(h2o_data[cname])
myY = "C21"
col_names <- names(h2o_data)
myX <- col_names[1:20]

constraints <- data.frame(names <- c("C1.2", "C11", "constant", "C5.2", "C12", "C15", "constant"),
values <- c(1, 1, 13.56, 1, 1, 1, -5),
types <- c("Equal", "Equal", "Equal", "LessThanEqual", "LessThanEqual", "LessThanEqual", "LessThanEqual"),
constraint_numbers <- c(0, 0, 0, 1, 1, 1, 1))
constraints_h2o <- as.h2o(constraints)
bc <- data.frame(names <- c("C1.1", "C5.2", "C11", "C15"), lower_bounds <- c(-36, -14, 25, 14), upper_bounds <- c(-35, -13, 26, 15))
bc_h2o <- as.h2o(bc)
m_sep <- h2o.glm(x=myX, y=myY, training_frame=h2o.data, 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)
h2o.coef(m_sep)

@wendycwong wendycwong changed the title GH-16208: Adding constrained GLM documentation to user guide GH-16208: Adding constrained GLM documentation to user guidem [nocheck] [nochecks] Oct 16, 2024
@wendycwong wendycwong merged commit dd44587 into rel-3.46.0 Oct 21, 2024
20 checks passed
@wendycwong wendycwong deleted the ht/16208-constrainedglm branch October 21, 2024 00:12
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