sklearn.compose.ColumnTransformer

class sklearn.compose.ColumnTransformer(transformers, remainder=’passthrough’, n_jobs=1, transformer_weights=None)[source]

Applies transformers to columns of an array or pandas DataFrame.

EXPERIMENTAL: some behaviors may change between releases without deprecation.

This estimator allows different columns or column subsets of the input to be transformed separately and the results combined into a single feature space. This is useful for heterogeneous or columnar data, to combine several feature extraction mechanisms or transformations into a single transformer.

Read more in the User Guide.

New in version 0.20.

Parameters:
transformers : list of tuples

List of (name, transformer, column(s)) tuples specifying the transformer objects to be applied to subsets of the data.

name : string

Like in Pipeline and FeatureUnion, this allows the transformer and its parameters to be set using set_params and searched in grid search.

transformer : estimator or {‘passthrough’, ‘drop’}

Estimator must support fit and transform. Special-cased strings ‘drop’ and ‘passthrough’ are accepted as well, to indicate to drop the columns or to pass them through untransformed, respectively.

column(s) : string or int, array-like of string or int, slice or boolean mask array

Indexes the data on its second axis. Integers are interpreted as positional columns, while strings can reference DataFrame columns by name. A scalar string or int should be used where transformer expects X to be a 1d array-like (vector), otherwise a 2d array will be passed to the transformer.

remainder : {‘passthrough’, ‘drop’}, default ‘passthrough’

By default, all remaining columns that were not specified in transformers will be automatically passed through (default of 'passthrough'). This subset of columns is concatenated with the output of the transformers. By using remainder='drop', only the specified columns in transformers are transformed and combined in the output, and the non-specified columns are dropped.

n_jobs : int, optional

Number of jobs to run in parallel (default 1).

transformer_weights : dict, optional

Multiplicative weights for features per transformer. The output of the transformer is multiplied by these weights. Keys are transformer names, values the weights.

Attributes:
transformers_ : list

The collection of fitted transformers as tuples of (name, fitted_transformer, column).

named_transformers_ : Bunch object, a dictionary with attribute access

Access the fitted transformer by name.

See also

sklearn.compose.make_column_transformer
convenience function for combining the outputs of multiple transformer objects applied to column subsets of the original feature space.

Notes

The order of the columns in the transformed feature matrix follows the order of how the columns are specified in the transformers list. Columns of the original feature matrix that are not specified are dropped from the resulting transformed feature matrix, unless specified in the passthrough keyword. Those columns specified with passthrough are added at the right to the output of the transformers.

Examples

>>> from sklearn.compose import ColumnTransformer
>>> from sklearn.preprocessing import Normalizer
>>> ct = ColumnTransformer(
...     [("norm1", Normalizer(norm='l1'), [0, 1]),
...      ("norm2", Normalizer(norm='l1'), slice(2, 4))])
>>> X = np.array([[0., 1., 2., 2.],
...               [1., 1., 0., 1.]])
>>> # Normalizer scales each row of X to unit norm. A separate scaling
>>> # is applied for the two first and two last elements of each
>>> # row independently.
>>> ct.fit_transform(X)    
array([[0. , 1. , 0.5, 0.5],
       [0.5, 0.5, 0. , 1. ]])

Methods

fit(X[, y]) Fit all transformers using X.
fit_transform(X[, y]) Fit all transformers, transform the data and concatenate results.
get_feature_names() Get feature names from all transformers.
get_params([deep]) Get parameters for this estimator.
set_params(**kwargs) Set the parameters of this estimator.
transform(X) Transform X separately by each transformer, concatenate results.
__init__(transformers, remainder=’passthrough’, n_jobs=1, transformer_weights=None)[source]
fit(X, y=None)[source]

Fit all transformers using X.

Parameters:
X : array-like or DataFrame of shape [n_samples, n_features]

Input data, of which specified subsets are used to fit the transformers.

y : array-like, shape (n_samples, …), optional

Targets for supervised learning.

Returns:
self : ColumnTransformer

This estimator

fit_transform(X, y=None)[source]

Fit all transformers, transform the data and concatenate results.

Parameters:
X : array-like or DataFrame of shape [n_samples, n_features]

Input data, of which specified subsets are used to fit the transformers.

y : array-like, shape (n_samples, …), optional

Targets for supervised learning.

Returns:
X_t : array-like or sparse matrix, shape (n_samples, sum_n_components)

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.

get_feature_names()[source]

Get feature names from all transformers.

Returns:
feature_names : list of strings

Names of the features produced by transform.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deep : boolean, optional

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

Returns:
params : mapping of string to any

Parameter names mapped to their values.

named_transformers_

Access the fitted transformer by name.

Read-only attribute to access any transformer by given name. Keys are transformer names and values are the fitted transformer objects.

set_params(**kwargs)[source]

Set the parameters of this estimator.

Valid parameter keys can be listed with get_params().

Returns:
self
transform(X)[source]

Transform X separately by each transformer, concatenate results.

Parameters:
X : array-like or DataFrame of shape [n_samples, n_features]

The data to be transformed by subset.

Returns:
X_t : array-like or sparse matrix, shape (n_samples, sum_n_components)

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.