sklearn.pipeline.FeatureUnion

class sklearn.pipeline.FeatureUnion(transformer_list, *, n_jobs=None, transformer_weights=None, verbose=False)[source]

Concatenates results of multiple transformer objects.

This estimator applies a list of transformer objects in parallel to the input data, then concatenates the results. This is useful to combine several feature extraction mechanisms into a single transformer.

Parameters of the transformers may be set using its name and the parameter name separated by a ‘__’. A transformer may be replaced entirely by setting the parameter with its name to another transformer, or removed by setting to ‘drop’.

Read more in the User Guide.

New in version 0.13.

Parameters
transformer_listlist of tuple

List of tuple containing (str, transformer). The first element of the tuple is name affected to the transformer while the second element is a scikit-learn transformer instance. The transformer instance can also be "drop" for it to be ignored.

Changed in version 0.22: Deprecated None as a transformer in favor of ‘drop’.

n_jobsint, default=None

Number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.

Changed in version v0.20: n_jobs default changed from 1 to None

transformer_weightsdict, default=None

Multiplicative weights for features per transformer. Keys are transformer names, values the weights. Raises ValueError if key not present in transformer_list.

verbosebool, default=False

If True, the time elapsed while fitting each transformer will be printed as it is completed.

Attributes
n_features_in_int

Number of features seen during fit.

See also

make_union

Convenience function for simplified feature union construction.

Examples

>>> from sklearn.pipeline import FeatureUnion
>>> from sklearn.decomposition import PCA, TruncatedSVD
>>> union = FeatureUnion([("pca", PCA(n_components=1)),
...                       ("svd", TruncatedSVD(n_components=2))])
>>> X = [[0., 1., 3], [2., 2., 5]]
>>> union.fit_transform(X)
array([[ 1.5       ,  3.0...,  0.8...],
       [-1.5       ,  5.7..., -0.4...]])

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()

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_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.

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

Fit all transformers using X.

Parameters
Xiterable or array-like, depending on transformers

Input data, used to fit transformers.

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

Targets for supervised learning.

**fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.

Returns
selfobject

FeatureUnion class instance.

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

Fit all transformers, transform the data and concatenate results.

Parameters
Xiterable or array-like, depending on transformers

Input data to be transformed.

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

Targets for supervised learning.

**fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.

Returns
X_tarray-like or sparse matrix of shape (n_samples, sum_n_components)

The hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers.

get_feature_names()[source]

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

Get feature names from all transformers.

Returns
feature_nameslist of strings

Names of the features produced by transform.

get_feature_names_out(input_features=None)[source]

Get output feature names for transformation.

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

Input features.

Returns
feature_names_outndarray of str objects

Transformed feature names.

get_params(deep=True)[source]

Get parameters for this estimator.

Returns the parameters given in the constructor as well as the estimators contained within the transformer_list of the FeatureUnion.

Parameters
deepbool, default=True

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

Returns
paramsmapping of string to any

Parameter names mapped to their values.

property n_features_in_

Number of features seen during fit.

set_params(**kwargs)[source]

Set the parameters of this estimator.

Valid parameter keys can be listed with get_params(). Note that you can directly set the parameters of the estimators contained in tranformer_list.

Parameters
**kwargsdict

Parameters of this estimator or parameters of estimators contained in transform_list. Parameters of the transformers may be set using its name and the parameter name separated by a ‘__’.

Returns
selfobject

FeatureUnion class instance.

transform(X)[source]

Transform X separately by each transformer, concatenate results.

Parameters
Xiterable or array-like, depending on transformers

Input data to be transformed.

Returns
X_tarray-like or sparse matrix of shape (n_samples, sum_n_components)

The hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers.

Examples using sklearn.pipeline.FeatureUnion