Polynomial regression with scikit-learn
Polynomial regression
Using numpy's polyfit
- numpy.polyfit(x, y, deg)
- Least squares polynomial fit
- Returns a vector of coefficients p that minimises the squared error.
In [1]:
import numpy as np
In [2]:
# create arrays of fake points
x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
In [4]:
# fit up to deg=3
z = np.polyfit(x, y, 3)
z
Out[4]:
Using scikit-learn's PolynomialFeatures
- Generate polynomial and interaction features
- Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree
In [24]:
# Import
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
In [25]:
# Create matrix and vectors
X = [[0.44, 0.68], [0.99, 0.23]]
y = [109.85, 155.72]
X_test = [0.49, 0.18]
In [28]:
# PolynomialFeatures (prepreprocessing)
poly = PolynomialFeatures(degree=2)
X_ = poly.fit_transform(X)
X_test_ = poly.fit_transform(X_test)
In [31]:
# Instantiate
lg = LinearRegression()
# Fit
lg.fit(X_, y)
# Obtain coefficients
lg.coef_
Out[31]:
In [32]:
# Predict
lg.predict(X_test_)
Out[32]: