Wednesday, February 26, 2020

Polynomial Regression

Polynomial Regression is a form of regression where the relationship between the dependent variable y and the independent variable x is modeled as an nth degree polynomial.

Formula for Polynomial Regression

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>y</mi><mo>=</mo><msub><mi>b</mi><mrow><mn>0</mn><mo>&#xA0;</mo></mrow></msub><mo>+</mo><mo>&#xA0;</mo><msub><mi>b</mi><mn>1</mn></msub><mo>&#xA0;</mo><msub><mi>x</mi><mrow><mn>1</mn><mo>&#xA0;</mo></mrow></msub><mo>+</mo><mo>&#xA0;</mo><msub><mi>b</mi><mn>2</mn></msub><mo>&#xA0;</mo><msubsup><mi>x</mi><mn>1</mn><mn>2</mn></msubsup><mo>&#xA0;</mo><mo>+</mo><mo>&#xA0;</mo><msub><mi>b</mi><mn>3</mn></msub><mo>&#xA0;</mo><msubsup><mi>x</mi><mn>1</mn><mn>3</mn></msubsup><mo>&#xA0;</mo><mo>+</mo><mo>&#xA0;</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>&#xA0;</mo><mo>+</mo><mo>&#xA0;</mo><msub><mi>b</mi><mi>n</mi></msub><mo>&#xA0;</mo><msubsup><mi>x</mi><mn>1</mn><mi>n</mi></msubsup></math>

Code snippet

from sklearn.preprocessing import PolynomialFeatures
poly_regr = PolynomialFeatures(degree = 2)
X_poly = poly_regr.fit_transform(X)
poly_regr.fit(X_poly, y)
reg = LinearRegression()

reg.fit(X_poly, y)

degree is the degree of the polynomial features.

Refer plots below with varying degree and observe that as degree value increases, the curve becomes more aligned to the data

Degree 2


Degree 3



Degree 4


Degree 5


No comments:

Post a Comment