4
4
# Andreas Mueller <[email protected] >
5
5
# License: BSD 3 clause
6
6
7
- from itertools import chain
7
+ from itertools import chain , combinations
8
8
import numbers
9
9
10
10
import numpy as np
19
19
from ..utils import safe_asarray
20
20
from ..utils import warn_if_not_float
21
21
from ..utils .extmath import row_norms
22
- from ..utils .fixes import combinations_with_replacement as comb_w_r
22
+ from ..utils .fixes import combinations_with_replacement as combinations_w_r
23
23
from ..utils .sparsefuncs_fast import inplace_csr_row_normalize_l1
24
24
from ..utils .sparsefuncs_fast import inplace_csr_row_normalize_l2
25
25
from ..utils .sparsefuncs import inplace_column_scale
@@ -390,7 +390,7 @@ def inverse_transform(self, X, copy=None):
390
390
391
391
392
392
class PolynomialFeatures (BaseEstimator , TransformerMixin ):
393
- """Generate polynomial ( interaction) features.
393
+ """Generate polynomial and interaction features.
394
394
395
395
Generate a new feature matrix consisting of all polynomial combinations
396
396
of the features with degree less than or equal to the specified degree.
@@ -401,7 +401,11 @@ class PolynomialFeatures(BaseEstimator, TransformerMixin):
401
401
----------
402
402
degree : integer
403
403
The degree of the polynomial features. Default = 2.
404
- include_bias : integer
404
+ interaction_only : boolean, default = False
405
+ If true, only interaction features are produced: features that are
406
+ products of at most ``degree`` *distinct* input features (so not
407
+ ``x[1] ** 2``, ``x[0] * x[2] ** 3``, etc.).
408
+ include_bias : boolean
405
409
If True (default), then include a bias column, the feature in which
406
410
all polynomial powers are zero (i.e. a column of ones - acts as an
407
411
intercept term in a linear model).
@@ -418,6 +422,11 @@ class PolynomialFeatures(BaseEstimator, TransformerMixin):
418
422
array([[ 1, 0, 1, 0, 0, 1],
419
423
[ 1, 2, 3, 4, 6, 9],
420
424
[ 1, 4, 5, 16, 20, 25]])
425
+ >>> poly = PolynomialFeatures(interaction_only=True)
426
+ >>> poly.fit_transform(X)
427
+ array([[ 1, 0, 1, 0],
428
+ [ 1, 2, 3, 6],
429
+ [ 1, 4, 5, 20]])
421
430
422
431
Attributes
423
432
----------
@@ -434,15 +443,17 @@ class PolynomialFeatures(BaseEstimator, TransformerMixin):
434
443
See :ref:`examples/plot_polynomial_regression.py
435
444
<example_plot_polynomial_regression.py>`
436
445
"""
437
- def __init__ (self , degree = 2 , include_bias = True ):
446
+ def __init__ (self , degree = 2 , interaction_only = False , include_bias = True ):
438
447
self .degree = degree
448
+ self .interaction_only = interaction_only
439
449
self .include_bias = include_bias
440
450
441
451
@staticmethod
442
- def _power_matrix (n_features , degree , include_bias ):
452
+ def _power_matrix (n_features , degree , interaction_only , include_bias ):
443
453
"""Compute the matrix of polynomial powers"""
454
+ comb = (combinations if interaction_only else combinations_w_r )
444
455
start = int (not include_bias )
445
- combn = chain .from_iterable (comb_w_r (range (n_features ), i )
456
+ combn = chain .from_iterable (comb (range (n_features ), i )
446
457
for i in range (start , degree + 1 ))
447
458
powers = np .vstack (np .bincount (c , minlength = n_features ) for c in combn )
448
459
return powers
@@ -452,8 +463,8 @@ def fit(self, X, y=None):
452
463
Compute the polynomial feature combinations
453
464
"""
454
465
n_samples , n_features = array2d (X ).shape
455
- self .powers_ = self ._power_matrix (n_features ,
456
- self .degree ,
466
+ self .powers_ = self ._power_matrix (n_features , self . degree ,
467
+ self .interaction_only ,
457
468
self .include_bias )
458
469
return self
459
470
0 commit comments