简体   繁体   中英

Polynomial Features and polynomial regression in sklearn

I have two questions:

  1. What is the output of fit_transform on a polynomial feature (what the numbers mean)? Correct me if I am wrong, but as far as I understood this method fit and transform our varriables to a polynomal model (of our choice of degree).
    For instance:
from sklearn.preprocessing import PolynomialFeatures

poly=PolynomialFeatures(degree=2)

poly.fit_transform(df[[firstColumn,secondColumn]],df[targetColumn])

So, the outcome is a 2-dimensional polynomial with df[firstColumn] and df[secondColumn] as varriables.

2) In polynomial regression, why do we need to use fit_tranform? What is the logic behind it?
For instance,

Xpoly=poly.fit_transform(X)

lin=LinearRegression()

lin.fit(Xpoly,y)

From sklearn documentation :

sklearn.preprocessing.PolynomialFeatures
Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].

So, this does exactly as you think.

fit_transform(self, X, y=None, **fit_params)
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

In sklearn, fit() just calculates the parameters and saves them as an internal objects state. Afterwards, you can call its transform() method to apply the transformation to a particular set of examples.

fit_transform() joins these two steps and is used for the initial fitting of parameters on the training set x, but it also returns a transformed x′. Internally, it just calls first fit() and then transform() on the same data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM