Showing posts with label ML. Show all posts
Showing posts with label ML. Show all posts

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


Multiple Linear Regression

Multiple Linear Regression is a regression model where we have multiple independent variables.

We need to predict values for the dependent variable as a function of the independent variables.



Formula for Multiple Linear Regression:


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

where

y is the dependent Variable
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>x</mi><mn>1</mn></msub></math> onwards are the the independent Variable
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>1</mn></msub></math> onwards is the coefficient (connector between dependent and Independent)
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>0</mn></msub></math> is the Constant

Always encode the categorical data if any after data import.

Code Snippet:

from sklearn.linear_model import LinearRegression
multi_regressor = LinearRegression()
multi_regressor.fit(X_train, y_train)

# Prediction
y_pred = multi_regressor.predict(X_test)





Simple Linear Regression

Simple Linear Regression is a linear regression model where we have one dependent and one independent variable.

We need to predict values for the dependent variable as a function of the independent variable.



Formula for Simple Linear Regression:


<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>y</mi><mo>=</mo><msub><mi>b</mi><mn>0</mn></msub><mo>+</mo><msub><mi>b</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub></math>

where

y is the dependent Variable
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>x</mi><mn>1</mn></msub></math> is the independent Variable
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>1</mn></msub></math> is the coefficient (connector between dependent and Independent)
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>0</mn></msub></math> is the Constant

Code Snippet:

from sklearn.linear_model import LinearRegression
simple_regressor = LinearRegression()
simple_regressor.fit(X_train, y_train)

# Prediction
y_pred = simple_regressor.predict(X_test)

Plot

plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, simple_regressor.predict(X_train), color = 'blue')
plt.show()