dirty_cat.SimilarityEncoder

Usage examples at the bottom of this page.

class dirty_cat.SimilarityEncoder(similarity='ngram', ngram_range=(2, 4), categories='auto', dtype=<class 'numpy.float64'>, handle_unknown='ignore', handle_missing='', hashing_dim=None, n_prototypes=None, random_state=None, n_jobs=None)[source]

Encode string categorical features as a numeric array.

The input to this transformer should be an array-like of strings. The method is based on calculating the morphological similarities between the categories. This encoding is an alternative to OneHotEncoder in the case of dirty categorical variables.

Parameters:
similarityNone

Deprecated in dirty_cat 0.3, will be removed in 0.5. Was used to specify the type of pairwise string similarity to use. Since 0.3, only the ngram similarity is supported.

ngram_rangetuple (min_n, max_n), default=(2, 4)

The range of values for the n_gram similarity.

categoriestyping.Union[typing.Literal[“auto”, “k-means”, “most_frequent”], typing.List[typing.List[str]]] # noqa

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

column. The passed categories must be sorted and should not mix strings and numeric values.

  • ‘most_frequent’Computes the most frequent values for every

    categorical variable

  • ‘k-means’Computes the K nearest neighbors of K-mean centroids

    in order to choose the prototype categories

The categories used can be found in the categories_ attribute.

dtypenumber type, default np.float64

Desired dtype of output.

handle_unknown‘error’ or ‘ignore’ (default)

Whether to raise an error or ignore if an unknown categorical feature is present during transform (default is to ignore). When this parameter is set to ‘ignore’ and an unknown category is encountered during transform, the resulting encoded columns for this feature will be all zeros. In the inverse transform, an unknown category will be denoted as None.

handle_missing‘error’ or ‘’ (default)

Whether to raise an error or impute with blank string ‘’ if missing values (NaN) are present during fit (default is to impute). When this parameter is set to ‘’, and a missing value is encountered during fit_transform, the resulting encoded columns for this feature will be all zeros. In the inverse transform, the missing category will be denoted as None.

hashing_dimint type or None.

If None, the base vectorizer is CountVectorizer, else it’s set to HashingVectorizer with a number of features equal to hashing_dim.

n_prototypesnumber of prototype we want to use.

Useful when most_frequent or k-means is used. Must be a positive non-null integer.

random_stateeither an int used as a seed, a RandomState instance or None.

Useful when k-means strategy is used.

n_jobsint, optional

maximum number of processes used to compute similarity matrices. Used only if fast=True in SimilarityEncoder.transform

References

For a detailed description of the method, see Similarity encoding for learning with dirty categorical variables by Cerda, Varoquaux, Kegl. 2018 (accepted for publication at: Machine Learning journal, Springer).

Attributes:
categories_typing.List[np.array]

The categories of each feature determined during fitting (in order corresponding with output of transform).

_infrequent_enabledbool, default=False

Avoid taking into account the existence of infrequent categories.

Methods

fit(X[, y])

Fit the SimilarityEncoder to X.

fit_transform(X[, y])

Fit SimilarityEncoder to data, then transform it.

get_feature_names([input_features])

DEPRECATED: get_feature_names is deprecated in 1.0 and will be removed in 1.2.

get_feature_names_out([input_features])

Get output feature names for transformation.

get_most_frequent(prototypes)

Get the most frequent category prototypes.

get_params([deep])

Get parameters for this estimator.

inverse_transform(X)

Convert the data back to the original representation.

set_params(**params)

Set the parameters of this estimator.

transform(X[, fast])

Transform X using specified encoding scheme.

fit(X, y=None)[source]

Fit the SimilarityEncoder to X.

Parameters:
Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.

yNone

Unused, only here for compatibility.

Returns:
SimilarityEncoder

The fitted SimilarityEncoder instance.

fit_transform(X, y=None, **fit_params)[source]

Fit SimilarityEncoder to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:
Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_params

Additional fit parameters.

Returns:
array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names(input_features=None)

DEPRECATED: get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.

Return feature names for output features.

For a given input feature, if there is an infrequent category, the most ‘infrequent_sklearn’ will be used as a feature name.

Parameters:
input_featureslist of str of shape (n_features,)

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.

Returns:
output_feature_namesndarray of shape (n_output_features,)

Array of feature names.

get_feature_names_out(input_features=None)

Get output feature names for transformation.

Parameters:
input_featuresarray-like of str or None, default=None

Input features.

  • If input_features is None, then feature_names_in_ is used as feature names in. If feature_names_in_ is not defined, then the following input feature names are generated: [“x0”, “x1”, …, “x(n_features_in_ - 1)”].

  • If input_features is an array-like, then input_features must match feature_names_in_ if feature_names_in_ is defined.

Returns:
feature_names_outndarray of str objects

Transformed feature names.

get_most_frequent(prototypes)[source]

Get the most frequent category prototypes.

Parameters:
prototypestyping.List[str]

The list of values for a category variable.

Returns:
np.array

The n_prototypes most frequent values for a category variable.

get_params(deep=True)

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

property infrequent_categories_

Infrequent categories for each feature.

inverse_transform(X)

Convert the data back to the original representation.

When unknown categories are encountered (all zeros in the one-hot encoding), None is used to represent this category. If the feature with the unknown category has a dropped category, the dropped category will be its inverse.

For a given input feature, if there is an infrequent category, ‘infrequent_sklearn’ will be used to represent the infrequent category.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_encoded_features)

The transformed data.

Returns:
X_trndarray of shape (n_samples, n_features)

Inverse transformed array.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

transform(X, fast=True)[source]

Transform X using specified encoding scheme.

Parameters:
Xarray-like, shape [n_samples, n_features]

The data to encode.

fastbool

Whether to use the fast computation of ngrams.

Returns:
X_new2-d array, shape [n_samples, n_features_new]

Transformed input.

Examples using dirty_cat.SimilarityEncoder

Dirty categories: machine learning with non normalized strings

Dirty categories: machine learning with non normalized strings

Dirty categories: machine learning with non normalized strings
Investigating dirty categories

Investigating dirty categories

Investigating dirty categories
Fitting scalable, non-linear models on data with dirty categories

Fitting scalable, non-linear models on data with dirty categories

Fitting scalable, non-linear models on data with dirty categories
Scalability considerations for similarity encoding

Scalability considerations for similarity encoding

Scalability considerations for similarity encoding