class dask_ml.preprocessing.PolynomialFeatures(degree=2, interaction_only=False, include_bias=True, preserve_dataframe=False)

Generate polynomial and interaction features.

Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].


The degree of the polynomial features. Default = 2.

interaction_onlyboolean, default = False

If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).


If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).


If True, preserve pandas and dask dataframes after transforming. Using False (default) returns numpy or dask arrays and mimics sklearn’s default behaviour

powers_array, shape (n_output_features, n_input_features)

powers_[i, j] is the exponent of the jth input in the ith output.


The total number of input features.


The total number of polynomial output features. The number of output features is computed by iterating over all suitably sized combinations of input features.


Be aware that the number of features in the output array scales polynomially in the number of features of the input array, and exponentially in the degree. High degrees can cause overfitting.

See examples/linear_model/plot_polynomial_interpolation.py


>>> X = np.arange(6).reshape(3, 2)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> poly = PolynomialFeatures(2)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.,  0.,  1.],
       [ 1.,  2.,  3.,  4.,  6.,  9.],
       [ 1.,  4.,  5., 16., 20., 25.]])
>>> poly = PolynomialFeatures(interaction_only=True)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.],
       [ 1.,  2.,  3.,  6.],
       [ 1.,  4.,  5., 20.]])


fit(self, X[, y])

Compute number of output features.

fit_transform(self, X[, y])

Fit to data, then transform it.

get_feature_names(self[, input_features])

Return feature names for output features

get_params(self[, deep])

Get parameters for this estimator.

set_params(self, \*\*params)

Set the parameters of this estimator.

transform(self, X[, y])

Transform data to polynomial features

__init__(self, degree=2, interaction_only=False, include_bias=True, preserve_dataframe=False)

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

fit(self, X, y=None)

Compute number of output features.

Xarray-like, shape (n_samples, n_features)

The data.

fit_transform(self, X, y=None, **fit_params)

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Xnumpy array of shape [n_samples, n_features]

Training set.

ynumpy array of shape [n_samples]

Target values.

X_newnumpy array of shape [n_samples, n_features_new]

Transformed array.

get_feature_names(self, input_features=None)

Return feature names for output features

input_featureslist of string, length n_features, optional

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.

output_feature_nameslist of string, length n_output_features
get_params(self, deep=True)

Get parameters for this estimator.

deepboolean, optional

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

paramsmapping of string to any

Parameter names mapped to their values.

set_params(self, **params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

transform(self, X, y=None)

Transform data to polynomial features

Xarray-like or sparse matrix, shape [n_samples, n_features]

The data to transform, row by row. Sparse input should preferably be in CSC format.

XPnp.ndarray or CSC sparse matrix, shape [n_samples, NP]

The matrix of features, where NP is the number of polynomial features generated from the combination of inputs.