Case Study (Salary & Position) :

SKLearrn (Polynomial Linear Regression)

- Considering the scatter plot:

    - A Simple Linear Regression doesn't fit quite well to the data 

    - Polynomial Linear Regression fits perfectly

- Example:

    - Describing how diseases spread 

    - Describing how pandemics & epidemics spread across territory or population 

- Polynomial Equation:

    - y = b0 + b1 x1 + b2 x1^2 + b3 x1^3 + ... + bn x1^n 


- Why is it still called a linear regression?

    - Considering the scatter plot:

                - The relationship between Y and X is non-linear 

                - A nonlinear model fits to the data 

    - But the regression function is linear

                - a linear combination of coefficients

    - It is a special case of the multiple linear regression

Overview

- Importing the Relevant Libraries

- Loading the Data

- Declaring the Dependent and the Independent variables

- Splitting the dataset into the Training set and Test set

- Polynomial Regression Model

    - Polynomial Features Transform
    - Creating a Linear Regression 
    - Fitting The Model
    - Predicting the Results
    - Making a Single Observation Prediction

- Visualising the Polynomial Linear Regression 

- Visualising the Polynomial Linear Regression (Higher Resolution)

- Comparing the Results with Simple Linear Regression 

    - Simple Linear Regression 

        - Creating & Training the Model - Predicting a Single Observation 

    - Visualising the Simple Linear Regression

Importing the Relevant Libraries

Loading the Data

Declaring the Dependent and the Independent variables

- Exclude Position (Index 0)
- Position & Level are the same

Splitting the Dataset into the Training Set and Test Set

- Dataset is small, so we will not split it into training set & test set

Polynomial Regression Model

Polynomial Features Transform

- Creating new versions of input variables 

- Transforming features to polynomial features

- degree = 4  -> x1 to the power of 4  

- y = b0 + b1 x1 + b2 x1^2 + b3 x1^3 + b4 x1^4 

- PolynomialFeatures Class from preprocessing Module of sklearn Library

- poly_features -> Object of PolynomialFeatures Class

- poly_features.fit_transform(X) -> fitting & trasforming X at the same time

Creating a Linear Regression

- LinearRegression Class from linear_model Module of sklearn Library

- model -> Object of LinearRegression Class

Fitting The Model

- fit method -> training the model

Predicting the Results

- y_pred -> the predicted salaries

Making a Single Observation Prediction

- level: 6.5 -> Salary = 158,862

- fit_transform method acceptd 2D array -> [[]]

Visualising the Polynomial Linear Regression

- Red points -> Actual Values

- Blue line -> Predicted values

Visualising the Polynomial Linear Regression

- Revising code for higher resolution and smoother curve

Comparing the Result with Simple Linear Regression

Simple Linear Regression

Creating & Training the Model - Predicting a Single Observation

Visualising the Simple Linear Regression