Search code examples
pythonscikit-learnfeature-extractionpolynomials

How to understand the behavior of sklearn.preprocessing.PolynomialFeatures?


In sklearn doc, it was introduced that the two dimensional feature of the form [a, b] would get its degree-2 polynomial features as [1, a, b, a^2, ab, b^2].

However, it's not clear how this happens. I cannot understand if the degree changes to 3, for example, what would the feature be like.

I tried to interpret it by [1, a, b, a^2, ab, b^2] = the upper triangle of ([a, b] @ [a, b].T ) because it seems like a quadratic form. But this would not be correct for degree 3.


Solution

  • [1, a, b, a², ab, b², a³, a²b, ab², b³] probably. These are the powers in the expansion of (1 + a + b)³.

    In the case of three variables, [1, a, a², a³, ab, ab², abc, ac, ac², b, b², b³, ba², bc, bc², c, c², c³, ca², cb²].