1 of 12

ניתוח נתונים ולמידת מכונה

אמית רננים – חלופה ליחידה 3

מוטי בזר

mottibz@gmail.com

https://www.commridge.com

Linear Regression using

Scikit-Learn

Some of the materials are with permission from

Jose Portilla, Ariel Bar Itzhak, Koby Mike

2 of 12

The Philosophy Behind Scikit-Learn (Sklearn)

  • One-stop-shop library containing many machine learning algorithms
  • Utilizes a generalized “estimator API” framework for calling models
    • Import, fit, and use in a uniform way across all algorithms
    • Easy algorithms swap for testing
    • Hides algorithms complexity from the users
  • Includes many tools (train-test split, cross validation, metrics, etc.)

2

3 of 12

Supervised Machine Learning (ML) Process (1 of 3)

  • Reminder on the ML process:

3

Deploy

4 of 12

Supervised Machine Learning (ML) Process (2 of 3)

4

Train-Test Split

The ML Models building process

(initial steps)

5 of 12

Supervised Machine Learning (ML) Process (3 of 3)

5

The ML Models building process up to deployment

6 of 12

Scikit-Learn generalized “estimator API” framework

6

1. Train-test split

2. Import the desired algorithm

3. Define the model object

4. Perform the fit() operation

5. Perform the predict() operation

6. Import desired error evaluation function

7. Evaluate model’s performance

This framework will be similar for all Supervised

machine learning based Scikit-Learn algorithms

7 of 12

Exploring multiple features

  • In the introduction section, we explored the relationship between “total spend” and “sales”
  • Let’s expand that in understanding the relationship between each of the advertising channels and sales
  • Let’s go to the notebook (first section)

7

8 of 12

Regression Performance Evaluation Metrics

  • Now that we know how to fit and predict, we need a way to understand how good are the predictions
  • We will use the test set for that, to compare our predictions to the actual values
  • The most common evaluation metrics for regression are:
    • MAE: Mean Absolute Error
    • MSE: Mean Squared Error
    • RMSE: Root Mean Square Error

8

9 of 12

Regression Performance Evaluation Metrics

  • MAE: Mean Square Error
    • Calculate the mean of the absolute values of the errors:

    • But MAE does not punish large errors:

    • We want the error metric to account �for high errors and punish them

    • This makes MAE non popular

9

10 of 12

Regression Performance Evaluation Metrics

  • MSE: Mean Squared Error
    • Punishes large errors by squaring the error values:
    • The issue with MSE is that it provides different units�than y (it provides y squared). RMSE addresses that

  • RMSE: Root Mean Square Error
    • It calculates the root of the MSE:
    • This is the most popular metric as�it provides the same units as y
    • When discussing what is a good RMSE, it all depends on the context
      • RMSE of $10 is great when predicting a house price, but horrible when predicting the price of a candy

10

11 of 12

Regression Performance Evaluation Metrics

  • R2 is an additional very popular metric for evaluating regression predictions results

11

 

 

 

Sum of all differences between each y and the mean of all y values

Green horizonal line represents the mean of all y values

This is MSE

  • Values are between 0-1
  • 1 = Best result (SSerror is very small)
  • 0 = Worst result (SSerror is very high)
  • A negative result means that the prediction is worse compared to the mean

12 of 12

Regression Performance Evaluation Metrics

  • Let’s go back to the notebook to calculate the prediction metrics

12