utils.wrappers.base#

Classes

Wrapper(prediction_function, feature_names)

Base Wrapper

class utils.wrappers.base.Wrapper(prediction_function, feature_names)[source]#

Bases: object

Base Wrapper

Warning: This class should not be used directly. Use derived classes instead.

default_label#

Default dict key for single value model outputs.

Type:

str

abstract __call__(x)[source]#
Return type:

Union[dict, List[dict]]

convert_1d_input_to_arr(x_dict)[source]#

Transforms a 1d input (a single dict) into an array of shape (1, N_Features).

Note

If the feature_names parameter is not None in the initialization of the Wrapper. The transformed output array is resorted in this order and to only contain the specified features.

Parameters:

x_dict (dict) – Input dictionary of type {feature_name: feature_value}.

Returns:

The transformed input array of shape (1, N_Features).

Return type:

(np.ndarray)

Examples

Basic usage:

>>> x_input = {'feature_1': 'value_1', 'feature_2': 2}
>>> x_array = Wrapper.convert_1d_input_to_arr(x_input)
>>> print(x_array, x_array.shape)
>>> [['value_1', 2]], (1, 2)
convert_2d_input_to_arr(x_dicts)[source]#

Transforms a 2d input (a list of dicts) into an array of shape (N_Instances, N_Features).

Note

If the feature_names parameter is not None in the initialization of the Wrapper. The transformed output array is resorted in this order and to only contain the specified features.

Parameters:

x_dicts (list[dict]) – List of input dictionaries of type {feature_name: feature_value}.

Returns:

The transformed input array of shape (N_Instances, N_Features).

Return type:

(np.ndarray)

Examples

Basic usage:

>>> x_inputs = [{'feature_1': 'value_1', 'feature_2': 2}, {'feature_1': 'value_1', 'feature_2': 3}]
>>> x_array = Wrapper.convert_1d_input_to_arr(x_inputs)
>>> print(x_array, x_array.shape)
>>> [['value_1', 2], ['value_1', 3]], (2, 2)
convert_arr_output_to_dict(y_prediction)[source]#

Transforms a prediction output into a dict of outputs.

Note

If the feature_names parameter is not None in the initialization of the Wrapper. The transformed output array is resorted in this order and to only contain the specified features.

Parameters:

y_prediction (np.ndarray) – Output array from model, of shape (1, N_Outputs) or (N_Outputs)

Returns:

The transformed output dictionary. If the output dimension of the model is 1 (e.g. regression,

classification labels) the output dict contains one element mapping from ‘output’ key to the model output (e.g. original output was [[1]] or [1] then {‘output’: 1} is the transformed output).

Return type:

(dict)

Examples

Onedimensional Model Output (Labels):

>>> model_output = np.asarray([1])
>>> Wrapper.convert_arr_output_to_dict(model_output)
>>> {'output': 1}
>>> model_output = np.asarray([[1]])
>>> Wrapper.convert_arr_output_to_dict(model_output)
>>> {'output': 1}

Multidimnesnional Model Output (Probas):

>>> model_output = np.asarray([[0.05, 0.5, 0.45]])
>>> Wrapper.convert_arr_output_to_dict(model_output)
>>> {0: 0.05, 1: 0.5, 2: 0.45}
Raises:

ValueError – If the model outputs are strings.