Skip to content

Polynomial features min degree and max degree not working properly #25944

@marshallcase

Description

@marshallcase

Describe the bug

I'm trying to use the PolynomialFeatures to generate 2nd order terms and exclude linear ones. According to the documentation, this should look like this:

poly = PolynomialFeatures(degree=(2,2),interaction_only=True)

But when I run the fit() method on this object it throws a ValueError (see below).

Steps/Code to Reproduce

import numpy as np
from sklearn.preprocessing import PolynomialFeatures


test = np.array([[1,2],[3,4],[5,6]])
poly = PolynomialFeatures(degree=(2,2),interaction_only=True)
poly.fit(test)

Expected Results

I expect the method to fit the dataset without throwing a ValueError. The transform() method should then return a matrix with shape (3,7) - 3 data points and 7 features (3 2nd order and 1 bias).

Actual Results

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_1639628/2995905071.py in <module>
----> 1 poly = PolynomialFeatures(degree=(2,2),interaction_only=True).fit(test)

/sw/pkgs/arc/python3.9-anaconda/2021.11/lib/python3.9/site-packages/sklearn/preprocessing/_data.py in fit(self, X, y)
   1704         n_samples, n_features = self._validate_data(
   1705             X, accept_sparse=True).shape
-> 1706         combinations = self._combinations(n_features, self.degree,
   1707                                           self.interaction_only,
   1708                                           self.include_bias)

/sw/pkgs/arc/python3.9-anaconda/2021.11/lib/python3.9/site-packages/sklearn/preprocessing/_data.py in _combinations(n_features, degree, interaction_only, include_bias)
   1643         start = int(not include_bias)
   1644         return chain.from_iterable(comb(range(n_features), i)
-> 1645                                    for i in range(start, degree + 1))
   1646 
   1647     @property

TypeError: can only concatenate tuple (not "int") to tuple

Versions

System:
    python: 3.9.7 (default, Sep 16 2021, 13:09:58)  [GCC 7.5.0]
executable: /sw/pkgs/arc/python3.9-anaconda/2021.11/bin/python3.9
   machine: Linux-4.18.0-305.65.1.el8_4.x86_64-x86_64-with-glibc2.28

Python dependencies:
          pip: 21.2.4
   setuptools: 58.0.4
      sklearn: 0.24.2
        numpy: 1.23.1
        scipy: 1.7.1
       Cython: 0.29.24
       pandas: 1.3.4
   matplotlib: 3.4.3
       joblib: 1.1.0
threadpoolctl: 2.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions