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 #15984

Draft
wants to merge 10 commits into
base: rel-3.44.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
156 changes: 155 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,157 @@ 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... algorithm="gbm",
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())

""",
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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... max_categorical_levels=11,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=-2,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
min_rule_length="""
>>> 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... min_rule_length=4,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
max_rule_length="""
>>> 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... min_rule_length=3,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
model_type="""
>>> 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... model_type="rules",
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
distribution="""
>>> 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... distribution="bernoulli",
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
rule_generation_ntrees="""
>>> 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... rule_generation_ntrees=60,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
rule_importance="""
>>> 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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... rule_generation_ntrees=60,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
""",
predict_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"})
>>> x = ["age", "sibsp", "parch", "fare", "sex", "pclass"]
>>> y = "survived"
>>> rfit = H2ORuleFitEstimator(max_rule_length=10,
... max_num_rules=100,
... rule_generation_ntrees=60,
... seed=1)
>>> rfit.train(training_frame=df, x=x, y=y)
>>> print(rfit.rule_importance())
"""
)
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