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-15989: Craft DT Example #15998

Open
wants to merge 8 commits into
base: rel-3.44.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
50 changes: 50 additions & 0 deletions h2o-bindings/bin/custom/python/gen_dt.py
Expand Up @@ -6,3 +6,53 @@
Builds a Decision Tree (DT) on a preprocessed dataset.
"""
)
examples = dict(
categorical_encoding="""
>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, categorical_encoding="auto")
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
""",
ignore_const_cols="""
>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, ignore_const_cols=True)
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
""",
max_depth="""
>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, max_depth=20)
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
""",
min_rows="""
>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, min_rows=10)
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
"""
)
52 changes: 52 additions & 0 deletions h2o-py/h2o/estimators/decision_tree.py
Expand Up @@ -107,6 +107,19 @@ def ignore_const_cols(self):
Ignore constant columns.

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

:examples:

>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
shaunyogeshwaran marked this conversation as resolved.
Show resolved Hide resolved
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, ignore_const_cols=True)
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
"""
return self._parms.get("ignore_const_cols")

Expand All @@ -122,6 +135,19 @@ def categorical_encoding(self):

Type: ``Literal["auto", "enum", "one_hot_internal", "one_hot_explicit", "binary", "eigen", "label_encoder",
"sort_by_response", "enum_limited"]``, defaults to ``"auto"``.
shaunyogeshwaran marked this conversation as resolved.
Show resolved Hide resolved

:examples:

>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
shaunyogeshwaran marked this conversation as resolved.
Show resolved Hide resolved
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, categorical_encoding="auto")
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
"""
return self._parms.get("categorical_encoding")

Expand Down Expand Up @@ -164,6 +190,19 @@ def max_depth(self):
Max depth of tree.

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

:examples:

>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, max_depth=20)
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
"""
return self._parms.get("max_depth")

Expand All @@ -178,6 +217,19 @@ def min_rows(self):
Fewest allowed (weighted) observations in a leaf.

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

:examples:

>>> import h2o
>>> from h2o.estimators import H2ODecisionTreeEstimator
>>> h2o.init()
>>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv")
>>> target_variable = 'CAPSULE'
>>> prostate[target_variable] = prostate[target_variable].asfactor()
>>> train, test = prostate.split_frame(ratios=[0.7])
>>> sdt_h2o = H2ODecisionTreeEstimator(model_id="decision_tree.hex", max_depth=5, min_rows=10)
>>> sdt_h2o.train(y=target_variable, training_frame=train)
>>> pred_test = sdt_h2o.predict(test)
"""
return self._parms.get("min_rows")

Expand Down