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-15915: Craft MS Examples #16001

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
146 changes: 146 additions & 0 deletions h2o-bindings/bin/custom/python/gen_modelselection.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ def coef_norm(self, predictor_size=None):

:param predictor_size: predictor subset size, will only return model coefficients of that subset size.
:return: list of Python Dicts of coefficients for all models built with different predictor numbers

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff_norm = maxrModel.coef_norm()
>>> print(coeff_norm)
"""
model_ids = self._model_json["output"]["best_model_ids"]
if not(self.actual_params["build_glm_model"]) and self.actual_params["mode"]=="maxrsweep":
Expand Down Expand Up @@ -95,6 +110,21 @@ def coef(self, predictor_size=None):

:param predictor_size: predictor subset size, will only return model coefficients of that subset size.
:return: list of Python Dicts of coefficients for all models built with different predictor numbers

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
if not self.actual_params["build_glm_model"] and self.actual_params["mode"]=="maxrsweep":
coef_names = self._model_json["output"]["coefficient_names"]
Expand Down Expand Up @@ -148,6 +178,7 @@ def coef(self, predictor_size=None):
def result(self):
"""
Get result frame that contains information about the model building process like for modelselection and anovaglm.

:return: the H2OFrame that contains information about the model building process like for modelselection and anovaglm.
"""
return H2OFrame._expr(expr=ExprNode("result", ASTId(self.key)))._frame(fill_cache=True)
Expand Down Expand Up @@ -225,3 +256,118 @@ def get_best_model_predictors(self):
mode=maxr, the model returned is no longer guaranteed to have the best R2 value.
"""
)

examples = dict(
build_glm_model="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", build_glm_model=False)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
influence="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", influence="dfbetas")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
multinode_mode="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", multinode_mode=False)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
nparallelism="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", nparallelism=0)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
p_values_threshold="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", p_values_threshold=0.0)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
custom_metric_func="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", early_stopping=False)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
obj_reg="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", obj_reg=-1.0)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
""",
mode="""
>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
)
151 changes: 151 additions & 0 deletions h2o-py/h2o/estimators/model_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,21 @@ def obj_reg(self):
Likelihood divider in objective value computation, default (of -1.0) will set it to 1/nobs

Type: ``float``, defaults to ``-1.0``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", obj_reg=-1.0)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("obj_reg")

Expand Down Expand Up @@ -1130,6 +1145,21 @@ def custom_metric_func(self):
Reference to custom evaluation function, format: `language:keyName=funcName`

Type: ``str``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", early_stopping=False)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("custom_metric_func")

Expand All @@ -1144,6 +1174,21 @@ def nparallelism(self):
number of models to build in parallel. Defaults to 0.0 which is adaptive to the system capability

Type: ``int``, defaults to ``0``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", nparallelism=0)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("nparallelism")

Expand Down Expand Up @@ -1190,6 +1235,21 @@ def mode(self):
than 'maxr', 'backward' for backward selection.

Type: ``Literal["allsubsets", "maxr", "maxrsweep", "backward"]``, defaults to ``"maxr"``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("mode")

Expand All @@ -1207,6 +1267,21 @@ def build_glm_model(self):
themselves. Defaults to false.

Type: ``bool``, defaults to ``False``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", build_glm_model=False)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("build_glm_model")

Expand All @@ -1222,6 +1297,21 @@ def p_values_threshold(self):
below this threshold

Type: ``float``, defaults to ``0.0``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", p_values_threshold=0.0)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("p_values_threshold")

Expand All @@ -1236,6 +1326,21 @@ def influence(self):
If set to dfbetas will calculate the difference in beta when a datarow is included and excluded in the dataset.

Type: ``Literal["dfbetas"]``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", influence="dfbetas")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("influence")

Expand All @@ -1251,6 +1356,21 @@ def multinode_mode(self):
Defaults to false.

Type: ``bool``, defaults to ``False``.

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr", multinode_mode=False)
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
return self._parms.get("multinode_mode")

Expand Down Expand Up @@ -1303,6 +1423,21 @@ def coef_norm(self, predictor_size=None):

:param predictor_size: predictor subset size, will only return model coefficients of that subset size.
:return: list of Python Dicts of coefficients for all models built with different predictor numbers

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff_norm = maxrModel.coef_norm()
>>> print(coeff_norm)
"""
model_ids = self._model_json["output"]["best_model_ids"]
if not(self.actual_params["build_glm_model"]) and self.actual_params["mode"]=="maxrsweep":
Expand Down Expand Up @@ -1356,6 +1491,21 @@ def coef(self, predictor_size=None):

:param predictor_size: predictor subset size, will only return model coefficients of that subset size.
:return: list of Python Dicts of coefficients for all models built with different predictor numbers

:examples:

>>> import h2o
>>> from h2o.estimators import H2OModelSelectionEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/prostate.csv")
>>> predictors = ["AGE", "RACE", "CAPSULE", "DCAPS", "PSA", "VOL", "DPROS"]
>>> response = "GLEASON"
>>> maxrModel = H2OModelSelectionEstimator(max_predictor_number=7, seed=12345, mode="maxr")
>>> maxrModel.train(x=predictors, y=response, training_frame=prostate)
>>> results = maxrModel.result()
>>> print(results)
>>> coeff = maxrModel.coef()
>>> print(coeff)
"""
if not self.actual_params["build_glm_model"] and self.actual_params["mode"]=="maxrsweep":
coef_names = self._model_json["output"]["coefficient_names"]
Expand Down Expand Up @@ -1409,6 +1559,7 @@ def coef(self, predictor_size=None):
def result(self):
"""
Get result frame that contains information about the model building process like for modelselection and anovaglm.

:return: the H2OFrame that contains information about the model building process like for modelselection and anovaglm.
"""
return H2OFrame._expr(expr=ExprNode("result", ASTId(self.key)))._frame(fill_cache=True)
Expand Down