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’ or None.

Read more in the User Guide.

Parameters
transformer_listlist of (string, transformer) tuples

List of transformer objects to be applied to the data. The first half of each tuple is the name of the transformer.

n_jobsint or None, optional (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.

transformer_weightsdict, optional

Multiplicative weights for features per transformer. Keys are transformer names, values the weights.

verboseboolean, optional(default=False)

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

See also

sklearn.pipeline.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(self, X[, y])

Fit all transformers using X.

fit_transform(self, X[, y])

Fit all transformers, transform the data and concatenate results.

get_feature_names(self)

Get feature names from all transformers.

get_params(self[, deep])

Get parameters for this estimator.

set_params(self, \*\*kwargs)

Set the parameters of this estimator.

transform(self, X)

Transform X separately by each transformer, concatenate results.

__init__(self, transformer_list, n_jobs=None, transformer_weights=None, verbose=False)[source]

Initialize self. See help(type(self)) for accurate signature.

fit(self, X, y=None)[source]

Fit all transformers using X.

Parameters
Xiterable or array-like, depending on transformers

Input data, used to fit transformers.

yarray-like, shape (n_samples, …), optional

Targets for supervised learning.

Returns
selfFeatureUnion

This estimator

fit_transform(self, 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, shape (n_samples, …), optional

Targets for supervised learning.

Returns
X_tarray-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.

get_feature_names(self)[source]

Get feature names from all transformers.

Returns
feature_nameslist of strings

Names of the features produced by transform.

get_params(self, deep=True)[source]

Get parameters for this estimator.

Parameters
deepboolean, optional

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.

set_params(self, **kwargs)[source]

Set the parameters of this estimator.

Valid parameter keys can be listed with get_params().

Returns
self
transform(self, 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, shape (n_samples, sum_n_components)

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