Moritz Moritz - 3 months ago 54
Python Question

sklearn: how to get coefficients of polynomial features

I now, it is possible to obtain the polynomial features as numbers by

polynomial_features.transform(X)
. According to the manual, for a degree of two the features are:
[1, a, b, a^2, ab, b^2]
. But how do I obtain a description of the features for higher orders ?
.get_params()
does not show any list of features.

Answer
import numpy as np
from sklearn.preprocessing import PolynomialFeatures

X = np.array([2,3])

poly = PolynomialFeatures(3)
Y = poly.fit_transform(X)
print Y
# prints [[ 1  2  3  4  6  9  8 12 18 27]]
print poly.powers_

This code will print:

[[0 0]
 [1 0]
 [0 1]
 [2 0]
 [1 1]
 [0 2]
 [3 0]
 [2 1]
 [1 2]
 [0 3]]

So if the i'th cell is (x,y), that means that Y[i]=(a**x)*(b**y). For instance, in the code example [2 1] equals to (2**2)*(3**1)=12.

Comments