snowflake.ml.modeling.preprocessing.OneHotEncoder

class snowflake.ml.modeling.preprocessing.OneHotEncoder(*, categories: Union[str, Dict[str, Union[ndarray[Any, dtype[int64]], ndarray[Any, dtype[float64]], ndarray[Any, dtype[str_]], ndarray[Any, dtype[bool_]]]]] = 'auto', drop: Optional[Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]] = None, sparse: bool = False, handle_unknown: str = 'error', min_frequency: Optional[Union[int, float]] = None, max_categories: Optional[int] = None, input_cols: Optional[Union[str, Iterable[str]]] = None, output_cols: Optional[Union[str, Iterable[str]]] = None, passthrough_cols: Optional[Union[str, Iterable[str]]] = None, drop_input_cols: Optional[bool] = False)

Bases: BaseTransformer

Encode categorical features as a one-hot numeric array.

The feature is converted to a matrix containing a column for each category. For each row, a column is 0 if the category is absent, or 1 if it exists. The categories can be detected from the data, or you can provide them. If you provide the categories, you can handle unknown categories in one of several different ways (see handle_unknown parameter below).

Categories that do not appear frequently in a feature may be consolidated into a pseudo-category called “infrequent.” The threshold below which a category is considered “infrequent” is configurable using the min_frequency parameter.

It is useful to drop one category from features in situations where perfectly collinear features cause problems, such as when feeding the resulting data into an unregularized linear regression model. However, dropping a category breaks the symmetry of the original representation and can therefore induce a bias in downstream models, for instance for penalized linear classification or regression models. You can choose from a handful of strategies for specifying the category to be dropped. See drop parameter below.

The results of one-hot encoding can be represented in two ways.
  • Dense representation creates a binary column for each category. For each row, exactly one column will

    contain a 1.

  • Sparse representation creates a compressed sparse row (CSR) matrix that indicates which columns contain a

    nonzero value in each row. As all columns but one contain zeroes, this is an efficient way to represent the results.

The order of input columns are preserved as the order of features.

For more details on what this transformer does, see sklearn.preprocessing.OneHotEncoder.

Args:
categories: ‘auto’ or dict {column_name: np.ndarray([category])}, default=’auto’

Categories (unique values) per feature: - ‘auto’: Determine categories automatically from the training data. - dict: categories[column_name] holds the categories expected in

the column provided. The passed categories should not mix strings and numeric values within a single feature, and should be sorted in case of numeric values.

The used categories can be found in the categories_ attribute.

drop: {‘first’, ‘if_binary’} or an array-like of shape (n_features,), default=None

Specifies a methodology to use to drop one of the categories per feature. This is useful in situations where perfectly collinear features cause problems, such as when feeding the resulting data into an unregularized linear regression model. However, dropping one category breaks the symmetry of the original representation and can therefore induce a bias in downstream models, for instance for penalized linear classification or regression models. - None: retain all features (the default). - ‘first’: drop the first category in each feature. If only one

category is present, the feature will be dropped entirely.

  • ‘if_binary’: drop the first category in each feature with two categories. Features with 1 or more than 2 categories are left intact.

  • array: drop[i] is the category in feature input_cols[i] that should be dropped.

When max_categories or min_frequency is configured to group infrequent categories, the dropping behavior is handled after the grouping.

sparse: bool, default=False

Will return a column with sparse representation if set True else will return a separate column for each category.

handle_unknown: {‘error’, ‘ignore’}, default=’error’

Specifies the way unknown categories are handled during transform(). - ‘error’: Raise an error if an unknown category is present during transform. - ‘ignore’: When an unknown category is encountered during

transform, the resulting one-hot encoded columns for this feature will be all zeros.

min_frequency: int or float, default=None

Specifies the minimum frequency below which a category will be considered infrequent. - If int, categories with a smaller cardinality will be considered

infrequent.

  • If float, categories with a smaller cardinality than min_frequency * n_samples will be considered infrequent.

max_categories: int, default=None

Specifies an upper limit to the number of output features for each input feature when considering infrequent categories. If there are infrequent categories, max_categories includes the category representing the infrequent categories along with the frequent categories. If None, there is no limit to the number of output features.

input_cols: Optional[Union[str, List[str]]], default=None

Single or multiple input columns.

output_cols: Optional[Union[str, List[str]]], default=None

Single or multiple output columns.

passthrough_cols: Optional[Union[str, List[str]]]

A string or a list of strings indicating column names to be excluded from any operations (such as train, transform, or inference). These specified column(s) will remain untouched throughout the process. This option is helpful in scenarios requiring automatic input_cols inference, but need to avoid using specific columns, like index columns, during training or inference.

drop_input_cols: Optional[Union[str, List[str]]]

Remove input columns from output if set True. False by default.

Attributes:
categories_: dict {column_name: ndarray([category])}

The categories of each feature determined during fitting.

drop_idx_: ndarray([index]) of shape (n_features,)
  • drop_idx_[i] is the index in _categories_list[i] of the category to be dropped for each feature.

  • drop_idx_[i] = None if no category is to be dropped from the feature with index i, e.g. when drop=’if_binary’ and the feature isn’t binary.

  • drop_idx_ = None if all the transformed features will be retained.

If infrequent categories are enabled by setting min_frequency or max_categories to a non-default value and drop_idx[i] corresponds to a infrequent category, then the entire infrequent category is dropped.

infrequent_categories_: list [ndarray([category])]

Defined only if infrequent categories are enabled by setting min_frequency or max_categories to a non-default value. infrequent_categories_[i] are the infrequent categories for feature input_cols[i]. If the feature input_cols[i] has no infrequent categories infrequent_categories_[i] is None.

Methods

fit(dataset)

Fit OneHotEncoder to dataset.

get_input_cols()

Input columns getter.

get_label_cols()

Label column getter.

get_output_cols()

Output columns getter.

get_params([deep])

Get parameters for this transformer.

get_passthrough_cols()

Passthrough columns getter.

get_sample_weight_col()

Sample weight column getter.

get_sklearn_args([default_sklearn_obj, ...])

Modified snowflake.ml.framework.base.Base.get_sklearn_args with sparse and sparse_output handling.

set_drop_input_cols([drop_input_cols])

set_input_cols(input_cols)

Input columns setter.

set_label_cols(label_cols)

Label column setter.

set_output_cols(output_cols)

Output columns setter.

set_params(**params)

Set the parameters of this transformer.

set_passthrough_cols(passthrough_cols)

Passthrough columns setter.

set_sample_weight_col(sample_weight_col)

Sample weight column setter.

to_lightgbm()

to_sklearn()

to_xgboost()

transform(dataset)

Transform dataset using one-hot encoding.

Attributes

infrequent_categories_

Infrequent categories for each feature.