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-15983: Craft RuleFit Examples [nocheck] [nochecks] #15984

Closed
wants to merge 17 commits into from
58 changes: 57 additions & 1 deletion h2o-bindings/bin/custom/python/gen_rulefit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def rule_importance(self):

def predict_rules(self, frame, rule_ids):
"""
Evaluates validity of the given rules on the given data.
Evaluates validity of the given rules on the given data.

:param frame: H2OFrame on which rule validity is to be evaluated
:param rule_ids: string array of rule ids to be evaluated against the frame
Expand Down Expand Up @@ -52,3 +52,59 @@ def predict_rules(self, frame, rule_ids):
"""
),
)

examples = dict(

algorithm="""
>>> import h2o
>>> h2o.init()
>>> from h2o.estimators import H2ORuleFitEstimator
>>> f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
>>> df = h2o.import_file(path=f, col_types={'pclass': "enum", 'survived': "enum"})
>>> train, test = df.split_frame(ratios=[0.8], seed=1)
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... algorithm="auto",
... seed=1)
>>> rfit.train(training_frame=train, x=x, y=y)
>>> print(rfit.rule_importance())
>>> rfit.predict(test)

""",
max_categorical_levels="""
>>> import h2o
>>> h2o.init()
>>> from h2o.estimators import H2ORuleFitEstimator
>>> f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
>>> df = h2o.import_file(path=f, col_types={'pclass': "enum", 'survived': "enum"})
>>> train, test = df.split_frame(ratios=[0.8], seed=1)
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... max_categorical_levels=10,
... seed=1)
>>> rfit.train(training_frame=train, x=x, y=y)
>>> print(rfit.rule_importance())
>>> rfit.predict(test)
""",
max_num_rules="""
>>> import h2o
>>> h2o.init()
>>> from h2o.estimators import H2ORuleFitEstimator
>>> f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
>>> df = h2o.import_file(path=f, col_types={'pclass': "enum", 'survived': "enum"})
>>> train, test = df.split_frame(ratios=[0.8], seed=1)
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... max_num_rules=-1,
... seed=1)
>>> rfit.train(training_frame=train, x=x, y=y)
>>> print(rfit.rule_importance())
>>> rfit.predict(test)
"""
)
56 changes: 55 additions & 1 deletion h2o-py/h2o/estimators/rulefit.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ def algorithm(self):
The algorithm to use to generate rules.

Type: ``Literal["auto", "drf", "gbm"]``, defaults to ``"auto"``.

:examples:

>>> import h2o
>>> h2o.init()
>>> from h2o.estimators import H2ORuleFitEstimator
>>> f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
>>> df = h2o.import_file(path=f, col_types={'pclass': "enum", 'survived': "enum"})
>>> train, test = df.split_frame(ratios=[0.8], seed=1)
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... algorithm="auto",
... seed=1)
>>> rfit.train(training_frame=train, x=x, y=y)
>>> print(rfit.rule_importance())
>>> rfit.predict(test)
"""
return self._parms.get("algorithm")

Expand Down Expand Up @@ -249,6 +267,24 @@ def max_num_rules(self):
by diminishing returns in model deviance.

Type: ``int``, defaults to ``-1``.

:examples:

>>> import h2o
>>> h2o.init()
>>> from h2o.estimators import H2ORuleFitEstimator
>>> f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
>>> df = h2o.import_file(path=f, col_types={'pclass': "enum", 'survived': "enum"})
>>> train, test = df.split_frame(ratios=[0.8], seed=1)
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... max_num_rules=-1,
... seed=1)
>>> rfit.train(training_frame=train, x=x, y=y)
>>> print(rfit.rule_importance())
>>> rfit.predict(test)
"""
return self._parms.get("max_num_rules")

Expand Down Expand Up @@ -370,6 +406,24 @@ def max_categorical_levels(self):
for categorical_encoding == EnumLimited.

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

:examples:

>>> import h2o
>>> h2o.init()
>>> from h2o.estimators import H2ORuleFitEstimator
>>> f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv"
>>> df = h2o.import_file(path=f, col_types={'pclass': "enum", 'survived': "enum"})
>>> train, test = df.split_frame(ratios=[0.8], seed=1)
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... max_categorical_levels=10,
... seed=1)
>>> rfit.train(training_frame=train, x=x, y=y)
>>> print(rfit.rule_importance())
>>> rfit.predict(test)
"""
return self._parms.get("max_categorical_levels")

Expand Down Expand Up @@ -397,7 +451,7 @@ def rule_importance(self):

def predict_rules(self, frame, rule_ids):
"""
Evaluates validity of the given rules on the given data.
Evaluates validity of the given rules on the given data.

:param frame: H2OFrame on which rule validity is to be evaluated
:param rule_ids: string array of rule ids to be evaluated against the frame
Expand Down
Loading