GetDummies#
- class scikitplot.preprocessing.GetDummies(columns=None, data_sep=', ', col_name_sep='_', dtype=<class 'float'>, sparse_output=False, handle_unknown='ignore', drop_first=None)[source]#
Multi-column multi-label string column one-hot encoder.
Custom transformer to expand string columns that contain multiple labels separated by
data_sep
into one-hot encoded columns bypandas.get_dummies
.Compatible with sklearn pipelines,
set_output
API, and supports both dense and sparse output.- Parameters:
- columnsstr, list of str or None, default=None
Column(s) to encode. If str, single column. If list, multiple columns. If None, automatically detect object (string) columns containing
data_sep
.- data_sepstr, default=”,”
Separator used inside string cells, e.g., “a,b,c”.
- col_name_sepstr, default=”_”
Separator for new dummy column names, e.g., “tags_a”.
- dtypedata-type, default=float
Data type for the output values. (sklearn default is float)
- sparse_outputbool, default=False
If True, return a SciPy sparse CSR matrix. If False, return pandas DataFrame (or numpy array if set_output=”default”).
- handle_unknown{“ignore”, “error”}, default=”ignore”
Strategy for unknown categories at transform time.
- drop_first{“first”, True, None}, default=None
Drop the first dummy in each feature (sorted order) to avoid collinearity.
See also
pandas.Series.str.get_dummies
Convert Series of strings to dummy codes.
pandas.from_dummies
Convert dummy codes back to categorical DataFrame.
sklearn.preprocessing.OneHotEncoder
General-purpose one-hot encoder.
sklearn.preprocessing.MultiLabelBinarizer
Multi-label binarizer for iterable of iterables.
References
[1]Çelik, M. (2023, December 9). How to converting pandas column of comma-separated strings into dummy variables?.
- fit(X, y=None)[source]#
Learn dummy categories from training data.
Stores column order, prefixes, and categories for later alignment.
- fit_transform(X, y=None, **fit_params)[source]#
Fit to data, then transform it.
Fits transformer to
X
andy
with optional parametersfit_params
and 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_paramsdict
Additional fit parameters.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)[source]#
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.
- set_output(*, transform=None)[source]#
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of
transform
andfit_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:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.