DropSimilar#
- class skrub.DropSimilar(threshold=1)[source]#
Drop columns found too redundant to the rest of the dataframe, according to association defined by Cramér’s V.
This is done by computing Cramér’s V between every possible two columns, and sorting these couples in descending order. Then, for every association above the given threshold, one of the two columns is dropped.
- Parameters:
- threshold
float, default=1 The Cramér association score value above which to start dropping columns.
- threshold
- Attributes:
See also
DropUninformativeDrops a single column if certain criteria indicate that it contains little to no information (amount of nulls, of distinct values…)
CleanerRuns several checks to sanitize a dataframe, including converting columns to standard formats or dropping certain columns.
Examples
>>> from skrub import DropSimilar >>> from skrub.datasets import toy_cities >>> df = toy_cities(size=5000, n_metrics=0) >>> df.head() uid cities encoded_cities start end 0 SHAoqcdajQ Vilnius 17.0 2004-09-02 03:22:56 2014-06-26 11:36:43 1 HVAFYLGCDW NaN NaN 1979-10-22 01:43:56 1987-10-15 06:30:05 2 oQIauSCbNL Rome 13.0 1986-08-09 19:01:10 2002-04-06 03:56:09 3 SjeSbCepzv Vilnius 17.0 2008-11-26 15:57:13 2021-11-16 23:16:13 4 ubagaIBHnG London 8.0 1982-09-13 20:54:54 2000-12-02 07:36:41
>>> ds = DropSimilar(threshold=0.8) >>> clean_df = ds.fit_transform(df)
dshas now removed a column for each pair with association above 0.8. These associations are stored in thetable_associations_attribute:>>> ds.table_associations_.head() left_column_name right_column_name cramer_v 0 cities encoded_cities 1.000000 1 uid cities 0.052979 2 uid encoded_cities 0.052979 3 encoded_cities end 0.046455 4 cities end 0.046455
A single pair is above the threshold,
citiesandencoded_cities, with an association score of 1. Since one is an encoding of the other, this is to be expected. Therefore, one of these two has been marked as dropped byds:>>> ds.to_drop_ ['encoded_cities']
This leaves us with the shortened dataframe:
>>> clean_df.head() uid cities start end 0 SHAoqcdajQ Vilnius 2004-09-02 03:22:56 2014-06-26 11:36:43 1 HVAFYLGCDW NaN 1979-10-22 01:43:56 1987-10-15 06:30:05 2 oQIauSCbNL Rome 1986-08-09 19:01:10 2002-04-06 03:56:09 3 SjeSbCepzv Vilnius 2008-11-26 15:57:13 2021-11-16 23:16:13 4 ubagaIBHnG London 1982-09-13 20:54:54 2000-12-02 07:36:41
Methods
fit_transform(X[, y])Fit to data, then transform it.
get_params([deep])Get parameters for this estimator.
set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
fit
get_feature_names_out
transform
- fit_transform(X, y=None)[source]#
Fit to data, then transform it.
Fits transformer to
Xandywith optional parametersfit_paramsand returns a transformed version ofX.- 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
dict Additional fit parameters. Pass only if the estimator accepts additional params in its
fitmethod.
- Returns:
- set_output(*, transform=None)[source]#
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of
transformandfit_transform."default": Default output format of a transformer"pandas": DataFrame output"polars": Polars outputNone: Transform configuration is unchanged
Added in version 1.4:
"polars"option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)[source]#
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:
- **params
dict Estimator parameters.
- **params
- Returns:
- selfestimator instance
Estimator instance.