skoot.utils.type_or_iterable_to_col_mapping

skoot.utils.type_or_iterable_to_col_mapping(cols, param, param_name, permitted_scalar_types)[source][source]

Map a parameter to various columns in a dict.

Many estimators accept either scalar values or iterables as parameters to allow for different values across different features. This function creates a dictionary mapping column names to parameter values and validates scalars within a tuple of permitted scalar types.

Note: this is primarily intended to be an internal method.

Parameters:

cols : list

The list of columns against which to map some function or parameters.

param : int, float, str, iterable or object

The parameter value.

param_name : str or unicode

The name of the parameter

permitted_scalar_types : type or iterable

The permitted types.

Returns:

param : dict

The param dictionary.

Examples

>>> cols = ["a", "c"]
>>> ticm = type_or_iterable_to_col_mapping  # too many characters...
>>> assert ticm(cols, 0.5, "n_components", float) == {'a': 0.5, 'c': 0.5}
>>> assert ticm(cols, "uniform", "strategy", str) == {'a': 'uniform',
...                                                   'c': 'uniform'}
>>> assert ticm(cols, [3, 5], "q", int) == {'a': 3, 'c': 5}
>>> assert ticm(cols, {"a": 3, "c": 5}, "q", int) == {'a': 3, 'c': 5}