hpt.utils package

hpt.utils.api module

Collection of classes defining the API this package depends on.

class hpt.utils.api.BaseLearner[source]

Bases: ABC

Interface for a generic learner (follows sklearn’s API).

abstract fit(X, y)[source]

Fit learner to the provided features X and labels y.

abstract predict_proba(X)[source]

Predict class probabilities for X.

hpt.utils.classpath module

Utils pertaining to importing and loading objects.

hpt.utils.classpath.get_full_name(obj)[source]

Returns identifier name for the given callable.

Should be equal to the import path:

obj == import_object(get_full_name(obj))

Parameters:

obj (object) – The object to find the classpath for.

Return type:

The object’s classpath.

hpt.utils.classpath.import_object(import_path)[source]

Imports the object at the given module/class path.

Parameters:

import_path (str) – The import path for the object to import.

Return type:

The imported object (this can be a class, a callable, a variable).

hpt.utils.dict module

Utils for handling dictionaries.

hpt.utils.dict.apply_recursively(dct, apply, pred=<function <lambda>>)[source]

Applies a function recursively to the provided dictionary, possibly filtering the fields to which it is applied.

Parameters:
  • dct (dict) – The dictionary to which the callable apply will be recursively applied.

  • apply (Callable) – The function to apply to the dictionary’s fields.

  • pred (Callable) – Predicate to filter which fields to apply the function to. Receives the (key, value) pair as an input.

Return type:

dict

hpt.utils.dict.fit_dict(dct, func, accept_kwargs=True)[source]

Returns a subset of (k, v) pairs from dct whose key matches an argument of the callable func.

If accept_kwargs==True and the function does accept key-word arguments, then all arguments are accepted.

Return type:

dict

hpt.utils.dict.join_dictionaries(*dicts)[source]

Join multiple dictionaries into one.

Return type:

dict

hpt.utils.fairness_criteria module

Computing fairness criteria on data.

File extracted from: https://github.com/fairmlbook/fairmlbook.github.io/blob/master/code/creditscore/criteria.py

class hpt.utils.fairness_criteria.CriteriaData(cdfs, performance, totals)[source]

Bases: object

Class for computing fairness criteria of data.

Create a CriteriaData instance from marginals.

cdfs and performance are both dataframes, with index = score and columns being the groups.

cdfs[group][score] = fraction of people of that group with score below that score. performance[group][score] = fraction of people with that group & score that succeed. totals[group] = total number of people of that group.

totals can be either a dictionary or an array (if an array, in the same order as cdfs.columns)

compute_area(cutoffs=None, performance=None)[source]
compute_area_slices(pdfs=None, performance=None)[source]
compute_curves()[source]
compute_profit(cutoffs, target_rate)[source]
coverage(cutoffs)[source]
demographic_cutoffs(target_rate, target_is_profit=False)[source]
efficiency(cutoffs, target_rate)[source]
evaluate_opportunity(cutoffs)[source]
fixed_cutoffs(target_rate, target_is_profit=False)[source]
classmethod from_individuals(data, groups=None, binsize=0.025)[source]

Recover CriteriaData instance from individual performances.

data should be a dataframe with three columns: the group key, the predictor score, and the result. Scores will be binned into bins of size binsize to compute the performance per group/score.

groups is the set of group keys to use; it may be specified to restrict the set, or to define the order.

get_best_demographic(target_rate)[source]
get_best_fixed(target_rate)[source]
get_best_opportunity(target_rate)[source]
get_pdfs()[source]
opportunity_cutoffs(target_rate, target_is_profit=False)[source]
profit_cutoffs(target_rate, _=None)[source]
score_two_sided_profit(point, target_rate)[source]
property trisearch
two_sided_optimum(target_rate)[source]
two_sided_ranges(p)[source]
hpt.utils.fairness_criteria.first_index_above(array, value)[source]

Find the smallest index i for which array[i] > value.

If no such value exists, return len(array).

hpt.utils.fairness_criteria.trisearch(f, lo, hi, tol)[source]

Trinary search: minimize f(x) over [lo, hi], to within +/-tol in x.

Works assuming f is quasiconvex.

hpt.utils.fairness_criteria.trisearch_int(f, lo, hi, tol=1)[source]

Trinary search: minimize f(x) over [lo, hi], to within +/-tol in x.

Works assuming f is quasiconvex.

hpt.utils.load_yaml module

Utils specific to the tuners module.

Author: @sgpjesus

class hpt.utils.load_yaml.YamlValidator[source]

Bases: object

assert_argument_exists(argument)[source]

Checks if a given argument for the Model Class to be isntantiated is expected by the Class signature. Uses global object of model to check signature.

Parameters:

argument (str) – Argument to be checked.

Returns:

True if check passes.

Return type:

bool

Raises:

TypeError – If argument is not expected in Class.

assert_class_exists(path)[source]

Checks if a given module and Class exists in the current python environment. Saves class in global object to assert arguments.

Parameters:
  • path (str) – Classpath to the Class to be checked.

  • AttributeError (Returns) – If Class does not exist within module.

  • ValueError – If classpath is malformed.

  • -------

  • bool – True if check passes.

Raises:
  • ModuleNotFoundError – If module does not exist.

  • AttributeError – If Class does not exist within module.

  • ValueError – If classpath is malformed.

Return type:

bool

hpt.utils.load_yaml.load_hyperparameter_space(path_or_dict)[source]

Loads the hyperparameter space encoded as a YAML in the given path. If given a dict, space is already loaded and this function will return the same object.

Parameters:

path_or_dict (Union[str, dict]) – Either the path to the YAML file, or a dictionary containing a hyperparameter space following the expected structure.

Return type:

The loaded hyperparameter space.

hpt.utils.trial module

An optuna Trial to output random hyperparameters outside optuna.

class hpt.utils.trial.RandomValueTrial(number=0, seed=None, sampler=None)[source]

Bases: FixedTrial

A Trial following optuna’s API to be used by non-optuna tuners. Does not depend on an optuna.Study and can be used as a standalone object.