Case Study (Salary & Position) :

SKLearrn (Support Vector Regression (SVR))

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

- Reshaping the Independent variable y

- Feature Scaling

- Support Vector Regression (SVR)

    - Creating SVR Model
    - Reshaping the Independent variable y
    - Fitting The Model
    - Predicting the Results
    - Making a Single Observation Prediction

- Visualising the SVR

- Visualising the SVR (Higher Resolution)

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

Reshaping the Independent variable y

- In feature scaling stage

    - fit_transform method of StandardScaler Class accepts 2D array

Feature Scaling

- Always apply feature scaling after splitting

- Linear Regression Models

    - No need to apply feature scaling for Linear Regression Models

    - The coefficients can compensate the high values of the features  

- SVR models

    - Apply feature scaling

    - There are not any coefficients to compensate the high values of the features


- Apply feature scaling for models which usually have an implicit equation  

     - Implicit relationship between the dependent variable Y and the features X

- Apply features Scaling on both the feature & the dependent variable

        - the feature taking values from 1 to 10 

        - the dependent variable taking values from 45000 to 1 million

- By applying features Scalings

        -  the feature will not be neglected by the SVR model

        -  the feature taking much lower values than the dependent variable 

- No need to apply features scaling

        - when the dependent variable takes binary values 0 and 1

Support Vector Regression (SVR)

Creating SVR Model

- SVR Class from svm Module of sklearn Library

- model -> Object of SVR Class

- kernel = 'rbf' -> Recommended

Reshaping the Independent variable y

- fit method accepts the independent variable as 1D array

Fitting The Model

- fit method -> training the model

Predicting the Results

- y_pred -> the predicted salaries

Making a Single Observation Prediction

- position level: 6.5

- sc_X.transform([[6.5]])

      -> apply feature scaling on the single observation 

      -> the same scaling used to scale features (X)

      -> the predict method accepts scaled feature

      -> fit_transform accepts 2D array -> [[]]

- apply sc_y.inverse_transform 

      -> to find original scale of predicted salary

      -> the predicted salary is scaled

Visualising the SVR

Visualising the SVR (Higher Resolution)

- Revising code for higher resolution and smoother curve