Skip to content

Commit

Permalink
PUDBEV-8485 fix python examples, fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
maurever committed Feb 11, 2022
1 parent 4356601 commit cd7fbe6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
59 changes: 28 additions & 31 deletions h2o-py/h2o/model/metrics_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,9 +1808,8 @@ def auuc(self, metric=None):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
Expand Down Expand Up @@ -1842,9 +1841,8 @@ def qini(self):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
Expand Down Expand Up @@ -1873,13 +1871,13 @@ def aecu(self, metric="qini"):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.aecu()
>>> perf = uplift_model.model_performance()
>>> perf.aecu()
"""
assert metric in ['qini', 'lift', 'gain'], \
"AECU metric "+metric+" should be 'qini','lift' or 'gain'."
Expand All @@ -1906,13 +1904,13 @@ def uplift(self, metric="AUTO"):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.uplift()
>>> perf = uplift_model.model_performance()
>>> perf.uplift()
"""
assert metric in ['AUTO', 'qini', 'lift', 'gain']

Expand Down Expand Up @@ -1941,13 +1939,13 @@ def uplift_random(self, metric="AUTO"):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.uplift()
>>> perf = uplift_model.model_performance()
>>> perf.uplift()
"""
assert metric in ['AUTO', 'qini', 'lift', 'gain']

Expand All @@ -1974,13 +1972,13 @@ def n(self):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.n()
>>> perf = uplift_model.model_performance()
>>> perf.n()
"""
return self._metric_json["thresholds_and_metric_scores"]["n"]

Expand All @@ -2003,13 +2001,13 @@ def thresholds(self):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.thresholds()
>>> perf = uplift_model.model_performance()
>>> perf.thresholds()
"""
return self._metric_json["thresholds_and_metric_scores"]["thresholds"]

Expand All @@ -2032,13 +2030,13 @@ def thresholds_and_metric_scores(self):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.thresholds_and_metric_scores()
>>> perf = uplift_model.model_performance()
>>> perf.thresholds_and_metric_scores()
"""
return self._metric_json["thresholds_and_metric_scores"]

Expand All @@ -2061,17 +2059,17 @@ def auuc_table(self):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.auuc_table()
>>> perf = uplift_model.model_performance()
>>> perf.auuc_table()
"""
return self._metric_json["auuc_table"]

def eacu_table(self):
def aecu_table(self):
"""
Retrieve all types of AECU values in a table.
Expand All @@ -2090,13 +2088,13 @@ def eacu_table(self):
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
>>> uplift_model.aecu_table()
>>> perf = uplift_model.model_performance()
>>> perf.aecu_table()
"""
return self._metric_json["aecu_table"]

Expand All @@ -2123,9 +2121,8 @@ def plot_uplift(self, server=False, save_to_file=None, plot=True, metric="AUTO")
>>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10,
... max_depth=5,
... treatment_column=treatment_column,
... uplift_metric="qini",
... uplift_metric="kl",
... distribution="bernoulli",
... gainslift_bins=10,
... min_rows=10,
... auuc_type="gain")
>>> uplift_model.train(y=response_column, x=predictors, training_frame=train)
Expand Down
23 changes: 22 additions & 1 deletion h2o-py/h2o/model/model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,28 @@ def auuc_table(self, train=False, valid=False):
raise H2OValueError("auuc_table() is only available for Uplift Binomial classifiers.")
m[k] = None if v is None else v.auuc_table()
return list(m.values())[0] if len(m) == 1 else m


def qini(self, train=False, valid=False):
"""
Get the Qini value (Area Under Uplift Curve - Area Under Random Curve for Qini uplift).
If all are False (default), then return the training metric value.
If more than one options is set to True, then return a dictionary of metrics where the keys are "train",
"valid".
:param bool train: If train is True, then return the Qini value for the training data.
:param bool valid: If valid is True, then return the Qini value for the validation data.
:returns: The Qini value.
"""
tm = ModelBase._get_metrics(self, train, valid, False)
m = {}
for k, v in viewitems(tm):
if not(v is None) and not(is_type(v, h2o.model.metrics_base.H2OBinomialUpliftModelMetrics)):
raise H2OValueError("auuc() is only available for Uplift Binomial classifiers.")
m[k] = None if v is None else v.qini()
return list(m.values())[0] if len(m) == 1 else m

def aic(self, train=False, valid=False, xval=False):
"""
Get the AIC (Akaike Information Criterium).
Expand Down

0 comments on commit cd7fbe6

Please sign in to comment.