Skip to content

Commit 1676a31

Browse files
author
=
committed
add polynomial features to user guide
1 parent 019fc9b commit 1676a31

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

doc/modules/preprocessing.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,41 @@ values than observed values.
426426

427427
:class:`Imputer` can be used in a Pipeline as a way to build a composite
428428
estimator that supports imputation. See :ref:`example_missing_values.py`
429+
430+
.. _polynomial_features:
431+
432+
Generating polynomial features
433+
==============================
434+
435+
Often it's useful to add complexity to the model by considering nonlinearity within input data. One basic representation is to use polynomial to get features' high-order and interaction terms, which is implemented in :class:`PolynomialFeatures`::
436+
437+
>>> import numpy as np
438+
>>> from sklearn.preprocessing import PolynomialFeatures
439+
>>> X = np.arange(6).reshape(3, 2)
440+
>>> X # doctest: +ELLIPSIS
441+
array([[0, 1],
442+
[2, 3],
443+
[4, 5]])
444+
>>> poly = PolynomialFeatures(2)
445+
>>> poly.fit_transform(X) # doctest: +ELLIPSIS
446+
array([[ 1, 0, 1, 0, 0, 1],
447+
[ 1, 2, 3, 4, 6, 9],
448+
[ 1, 4, 5, 16, 20, 25]])
449+
450+
The features of X have been transformed from :math:`(X_1, X_2)` to :math:`(1, X_1, X_2, X_1^2, X_1X_2, X_2^2)`.
451+
452+
In some cases, only interaction terms among features are required, and it can be gotten with the setting ``interaction_only=True``::
453+
>>> X = np.arange(9).reshape(3, 3)
454+
>>> X # doctest: +ELLIPSIS
455+
array([[0, 1, 2],
456+
[3, 4, 5],
457+
[6, 7, 8]])
458+
>>> poly = PolynomialFeatures(degree=3, interaction_only=True)
459+
>>> poly.fit_transform(X) # doctest: +ELLIPSIS
460+
array([[ 1, 0, 1, 2, 0, 0, 2, 0],
461+
[ 1, 3, 4, 5, 12, 15, 20, 60],
462+
[ 1, 6, 7, 8, 42, 48, 56, 336]])
463+
464+
The features of X have been transformed from :math:`(X_1, X_2, X_3)` to :math:`(1, X_1, X_2, X_3, X_1X_2, X_1X_3, X_2X_3, X_1X_2X_3)`.
465+
466+
See :ref:`example_linear_model_plot_polynomial_interpolation.py` for Ridge regression using created ploynomial features.

0 commit comments

Comments
 (0)