This lesson is being piloted (Beta version)

Evaluation Metrics with Scikit-Learn

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How do we measure the accuracy of ML model

Objectives
  • Learn different metrics with sklearn

4 Evaluation Metrics

4.1 Regression model Evaluation Metrics

4.1.1 Correlation Coefficient (R) or Coefficient of Determination (R2):

image

from sklearn import metrics
metrics.r2_score(y_test,y_pred)

4.1.2 Root Mean Square Error (RMSE) or Mean Square Error (MSE)

image

from sklearn import metrics
metrics.mean_squared_error(y_test,y_pred,squared=False) # RMSE
metrics.mean_squared_error(y_test,y_pred,squared=True) # MSE

4.2. Classification model Evaluation Metrics

4.2.1 Confusion Matrix

For binary output (classification problem with only 2 output type, also most popular):

image

4.2.2 Accuracy

The most common metric for classification is accuracy, which is the fraction of samples predicted correctly as shown below:

image

from sklearn import metrics
metrics.accuracy_score(y_test,y_pred)

4.2.3 Precision

Precision is the fraction of predicted positives events that are actually positive as shown below:

image

4.2.4 Recall

Recall (also known as sensitivity) is the fraction of positives events that you predicted correctly as shown below:

image

4.2.5 F1 score

The f1 score is the harmonic mean of recall and precision, with a higher score as a better model. The f1 score is calculated using the following formula:

image

More information on Precision, Recall and F1 score can be found here

metrics.precision_recall_fscore_support(y_test,y_pred,average='binary')

4.2.6 AUC-ROC curve

image

image

ROC Interpretation

image

Code to calculate FPR, TPR:

from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_test,y_pred)

Code to calculate AUC score:

from sklearn.metrics import roc_auc_score
auc_score = roc_auc_score(y_test,y_pred)

We will go into detail how to plot AUC-ROC curve in the next chapter with a classification problem

Key Points

  • sklearn, metrics