Introduction to Data Science: Classifier n 1 n 1 k k Suppose you - - PowerPoint PPT Presentation

introduction to data science classifier
SMART_READER_LITE
LIVE PREVIEW

Introduction to Data Science: Classifier n 1 n 1 k k Suppose you - - PowerPoint PPT Presentation

Classier evaluation Classier evaluation Leave-one-out Cross-Validation Leave-one-out Cross-Validation Leave-one-out Cross-Validation Classier evaluation Classier evaluation Leave-one-out Cross-Validation Resampled validation set


slide-1
SLIDE 1

Classier evaluation

How do we determine how well classifiers are performing? One way is to compute the error rate of the classifier, the percent of mistakes it makes when predicting class 1 / 50

Classier evaluation

## observed ## predicted No Yes ## No 9625 233 ## Yes 42 100 ## [1] 2.75 ## [1] 3.33 logis_fit <- glm(default ~ balance, data=Def logis_pred_prob <- predict(logis_fit, type=" logis_pred <- ifelse(logis_pred_prob > 0.5, print(table(predicted=logis_pred, observed=D # error rate mean(Default$default != logis_pred) * 100 # dummy error rate mean(Default$default != "No") * 100

2 / 50

Classier evaluation

We need a more precise language to describe classification mistakes: True Class + True Class - Total Predicted Class + True Positive (TP) False Positive (FP) P* Predicted Class - False Negative (FN) True Negative (TN) N* Total P N 3 / 50

Classier evaluation

Using these we can define statistics that describe classifier performance Name Definition Synonyms False Positive Rate (FPR) FP / N Type-I error, 1-Specificity True Positive Rate (TPR) TP / P 1 - Type-II error, power, sensitivity, recall Positive Predictive Value (PPV) TP / P* precision, 1-false discovery proportion Negative Predicitve Value (NPV) FN / N* 4 / 50

Classier evaluation

In the credit default case we may want to increase TPR (recall, make sure we catch all defaults) at the expense of FPR (1-Specificity, clients we lose because we think they will default) 5 / 50

Classier evaluation

This leads to a natural question: Can we adjust our classifiers TPR and FPR? Remember we are classifying Yes if What would happen if we use ?

log > 0 ⇒ P(Y = Yes|X) > 0.5 P(Y = Yes|X) P(Y = No|X) P(Y = Yes|X) > 0.2

6 / 50

Classier evaluation

7 / 50

Classier evaluation

A way of describing the TPR and FPR tradeoff is by using the ROC curve (Receiver Operating Characteristic) and the AUROC (area under the ROC) 8 / 50

Classier evaluation

9 / 50 default ~ balance*student + income

Classier evaluation

Consider comparing a logistic regression model using all predictors in the dataset, including an interaction term between balance and student. 10 / 50

Classier evaluation

Another metric that is frequently used to understand classification errors and tradeoffs is the precision-recall curve: 11 / 50

Classier evaluation

The bigger model shows a slightly higher precision at the same recall values and slightly higher area under the precision-recall curve. This is commonly found in datasets where there is a skewed distribution

  • f classes (e.g., there are many more "No" than "Yes" in this dataset).

The area under the PR curve tends to distinguish classifier performance than area under the ROC curve in these cases. 12 / 50

Model Selection

Our goal when we use a learning model like linear or logistic regression, decision trees, etc., is to learn a model that can predict outcomes for new unseen data. 13 / 50

Model Selection

We should therefore think of model evaluation based on expected predicted error: what will the prediction error be for data outside the training data. 14 / 50

Model Selection

We should therefore think of model evaluation based on expected predicted error: what will the prediction error be for data outside the training data. How then, do we measure our models' ability to predict unseen data, when we only have access to training data? 15 / 50

Cross-validation

The most common method to evaluate model generalization performance is cross-validation. It is used in two essential data analysis phases: Model Selection and Model Assessment. 16 / 50

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. 17 / 50

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. Consider a regression example: I will fit a linear regression model, what predictors should be included?, interactions?, data transformations? 18 / 50

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. Consider a regression example: I will fit a linear regression model, what predictors should be included?, interactions?, data transformations? Another example is what classification tree depth to use. 19 / 50

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. Consider a regression example: I will fit a linear regression model, what predictors should be included?, interactions?, data transformations? Another example is what classification tree depth to use. Which kind of algorithm to use, linear regression vs. decision tree vs. random forest 20 / 50

Cross-validation

Model Assessment

Determine how well does our selected model performs as a general model. 21 / 50

Cross-validation

Model Assessment

Determine how well does our selected model performs as a general model.

  • Ex. I've built a linear regression model with a specific set predictors. How

well will it perform on unseen data? 22 / 50

Cross-validation

Model Assessment

Determine how well does our selected model performs as a general model.

  • Ex. I've built a linear regression model with a specific set predictors. How

well will it perform on unseen data? The same question can be asked of a classification tree of specific depth. 23 / 50

Cross-validation

Cross-validation is a resampling method to obtain estimates of expected prediction error rate (or any other performance measure on unseen data). In some instances, you will have a large predefined test dataset that you should never use when training. In the absence of access to this kind of dataset, cross validation can be used. 24 / 50

Validation Set

The simplest option to use cross-validation is to create a validation set, where our dataset is randomly divided into training and validation sets. Then the validation is set aside, and not used at until until we are ready to compute test error rate (once, don't go back and check if you can improve it). 25 / 50 A linear regression model was not appropriate for this dataset. Use polynomial regression as an illustrative example.

Validation Set

Let's look at our running example using automobile data, where we want to build a regression model to predict miles per gallon given other auto attributes. 26 / 50

Validation Set

For polynomial regression, our regression model (for a single predictor ) is given as a degree polynomial. For model selection, we want to decide what degree we should use to model this data.

X d E[Y |X = x] = β0 + β1x + β2x2 + ⋯ + βdxd d

27 / 50 Using the validation set method, split our data into a training set, fit the regression model with different polynomial degrees on the training set, measure test error on the validation set.

Validation Set

d

28 / 50

Resampled validation set

The validation set approach can be prone to sampling issues. It can be highly variable as error rate is a random quantity and depends

  • n observations in training and validation sets.

29 / 50

Resampled validation set

The validation set approach can be prone to sampling issues. It can be highly variable as error rate is a random quantity and depends

  • n observations in training and validation sets.

We can improve our estimate of test error by averaging multiple measurements of it (remember the law of large numbers). 30 / 50 Resample validation set 10 times (yielding different validation and training sets) and averaging the resulting test errors.

Resampled validation set

31 / 50

Leave-one-out Cross-Validation

This approach still has some issues. Each of the training sets in our validation approach only uses 50% of data to train, which leads to models that may not perform as well as models trained with the full dataset and thus we can overestimate error. 32 / 50

Leave-one-out Cross-Validation

This approach still has some issues. Each of the training sets in our validation approach only uses 50% of data to train, which leads to models that may not perform as well as models trained with the full dataset and thus we can overestimate error. To alleviate this situation, we can extend our approach to the extreme: Make each single training point it's own validation set. 33 / 50 Procedure: For each observation in data set:

  • a. Train model on all but -th
  • bservation
  • b. Predict response for -th
  • bservation
  • c. Calculate prediction error

Leave-one-out Cross-Validation

i i i

34 / 50 This gives us the following cross- validation estimate of error.

Leave-one-out Cross-Validation

CV(n) = ∑

i

(yi − ^ yi)2 1 n

35 / 50

Leave-one-out Cross-Validation

use

  • bservations to train each model

no sampling effects introduced since error is estimated on each sample

n − 1

36 / 50

Leave-one-out Cross-Validation

use

  • bservations to train each model

no sampling effects introduced since error is estimated on each sample Disadvantages: Depending on the models we are trying to fit, it can be very costly to train models. Error estimate for each model is highly variable (since it comes from a single datapoint).

n − 1 n − 1

37 / 50

Leave-one-out Cross-Validation

On our running example 38 / 50

k-fold Cross-Validation

This discussion leads us to the most commonly used cross-validation approach k-fold Cross-Validation. 39 / 50 Procedure: Partition observations randomly into groups (folds). For each of the groups of

  • bservations:

Train model on observations in the other folds Estimate test-set error (e.g., Mean Squared Error) on this fold

k-fold Cross-Validation

k k k − 1

40 / 50 Procedure: Compute average error across folds where is mean squared error estimated on the -th fold

k-fold Cross-Validation

k CV(k) = ∑

i

MSEi 1 k MSEi i

41 / 50

k-fold Cross-Validation

Fewer models to fit (only of them) Less variance in each of the computed test error estimates in each fold.

k

42 / 50

k-fold Cross-Validation

Fewer models to fit (only of them) Less variance in each of the computed test error estimates in each fold. It can be shown that there is a slight bias (over estimating usually) in error estimate obtained from this procedure.

k

43 / 50

k-fold Cross-Validation

Running Example 44 / 50

Cross-Validation in Classication

Each of these procedures can be used for classification as well. In this case we would substitute MSE with performance metric of choice. E.g., error rate, accuracy, TPR, FPR, AUROC. 45 / 50

Cross-Validation in Classication

Each of these procedures can be used for classification as well. In this case we would substitute MSE with performance metric of choice. E.g., error rate, accuracy, TPR, FPR, AUROC. Note however that not all of these work with LOOCV (e.g. AUROC since it can't be defined over single data points). 46 / 50 Suppose you want to compare two classification models (logistic regression vs. a decision tree) on the Default dataset. We can use Cross-Validation to determine if one model is better than the other, using a -test for example.

Comparing models using cross-validation

t

47 / 50

Comparing models using cross-validation

Using hypothesis testing: term estimate std.error statistic p.value (Intercept) 0.0267 0.0020306 13.148828 0.0000000 methodtree 0.0030 0.0028717 1.044677 0.3099998 In this case, we do not observe any significant difference between these two classification methods. 48 / 50

Summary

Model selection and assessment are critical steps of data analysis. Error and accuracy statistics are not enough to understand classifier performance. Classifications can be done using probability cutoffs to trade, e.g., TPR- FPR (ROC curve), or precision-recall (PR curve). Area under ROC or PR curve summarize classifier performance across different cutoffs. 49 / 50

Summary

Resampling methods are general tools used for this purpose. k-fold cross-validation can be used to provide larger training sets to algorithms while stabilizing empirical estimates of expected prediction error 50 / 50

Introduction to Data Science: Classifier Evaluation and Model Selection

Héctor Corrada Bravo

University of Maryland, College Park, USA CMSC320: 2020-04-26

slide-2
SLIDE 2

Classier evaluation

How do we determine how well classifiers are performing? One way is to compute the error rate of the classifier, the percent of mistakes it makes when predicting class 1 / 50

slide-3
SLIDE 3

Classier evaluation

## observed ## predicted No Yes ## No 9625 233 ## Yes 42 100 ## [1] 2.75 ## [1] 3.33 logis_fit <- glm(default ~ balance, data=Def logis_pred_prob <- predict(logis_fit, type=" logis_pred <- ifelse(logis_pred_prob > 0.5, print(table(predicted=logis_pred, observed=D # error rate mean(Default$default != logis_pred) * 100 # dummy error rate mean(Default$default != "No") * 100

2 / 50

slide-4
SLIDE 4

Classier evaluation

We need a more precise language to describe classification mistakes: True Class + True Class - Total Predicted Class + True Positive (TP) False Positive (FP) P* Predicted Class - False Negative (FN) True Negative (TN) N* Total P N 3 / 50

slide-5
SLIDE 5

Classier evaluation

Using these we can define statistics that describe classifier performance Name Definition Synonyms False Positive Rate (FPR) FP / N Type-I error, 1-Specificity True Positive Rate (TPR) TP / P 1 - Type-II error, power, sensitivity, recall Positive Predictive Value (PPV) TP / P* precision, 1-false discovery proportion Negative Predicitve Value (NPV) FN / N* 4 / 50

slide-6
SLIDE 6

Classier evaluation

In the credit default case we may want to increase TPR (recall, make sure we catch all defaults) at the expense of FPR (1-Specificity, clients we lose because we think they will default) 5 / 50

slide-7
SLIDE 7

Classier evaluation

This leads to a natural question: Can we adjust our classifiers TPR and FPR? Remember we are classifying Yes if What would happen if we use ?

log > 0 ⇒ P(Y = Yes|X) > 0.5 P(Y = Yes|X) P(Y = No|X) P(Y = Yes|X) > 0.2

6 / 50

slide-8
SLIDE 8

Classier evaluation

7 / 50

slide-9
SLIDE 9

Classier evaluation

A way of describing the TPR and FPR tradeoff is by using the ROC curve (Receiver Operating Characteristic) and the AUROC (area under the ROC) 8 / 50

slide-10
SLIDE 10

Classier evaluation

9 / 50

slide-11
SLIDE 11

default ~ balance*student + income

Classier evaluation

Consider comparing a logistic regression model using all predictors in the dataset, including an interaction term between balance and student. 10 / 50

slide-12
SLIDE 12

Classier evaluation

Another metric that is frequently used to understand classification errors and tradeoffs is the precision-recall curve: 11 / 50

slide-13
SLIDE 13

Classier evaluation

The bigger model shows a slightly higher precision at the same recall values and slightly higher area under the precision-recall curve. This is commonly found in datasets where there is a skewed distribution

  • f classes (e.g., there are many more "No" than "Yes" in this dataset).

The area under the PR curve tends to distinguish classifier performance than area under the ROC curve in these cases. 12 / 50

slide-14
SLIDE 14

Model Selection

Our goal when we use a learning model like linear or logistic regression, decision trees, etc., is to learn a model that can predict outcomes for new unseen data. 13 / 50

slide-15
SLIDE 15

Model Selection

We should therefore think of model evaluation based on expected predicted error: what will the prediction error be for data outside the training data. 14 / 50

slide-16
SLIDE 16

Model Selection

We should therefore think of model evaluation based on expected predicted error: what will the prediction error be for data outside the training data. How then, do we measure our models' ability to predict unseen data, when we only have access to training data? 15 / 50

slide-17
SLIDE 17

Cross-validation

The most common method to evaluate model generalization performance is cross-validation. It is used in two essential data analysis phases: Model Selection and Model Assessment. 16 / 50

slide-18
SLIDE 18

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. 17 / 50

slide-19
SLIDE 19

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. Consider a regression example: I will fit a linear regression model, what predictors should be included?, interactions?, data transformations? 18 / 50

slide-20
SLIDE 20

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. Consider a regression example: I will fit a linear regression model, what predictors should be included?, interactions?, data transformations? Another example is what classification tree depth to use. 19 / 50

slide-21
SLIDE 21

Cross-validation

Model Selection

Decide what kind, and how complex of a model we should fit. Consider a regression example: I will fit a linear regression model, what predictors should be included?, interactions?, data transformations? Another example is what classification tree depth to use. Which kind of algorithm to use, linear regression vs. decision tree vs. random forest 20 / 50

slide-22
SLIDE 22

Cross-validation

Model Assessment

Determine how well does our selected model performs as a general model. 21 / 50

slide-23
SLIDE 23

Cross-validation

Model Assessment

Determine how well does our selected model performs as a general model.

  • Ex. I've built a linear regression model with a specific set predictors. How

well will it perform on unseen data? 22 / 50

slide-24
SLIDE 24

Cross-validation

Model Assessment

Determine how well does our selected model performs as a general model.

  • Ex. I've built a linear regression model with a specific set predictors. How

well will it perform on unseen data? The same question can be asked of a classification tree of specific depth. 23 / 50

slide-25
SLIDE 25

Cross-validation

Cross-validation is a resampling method to obtain estimates of expected prediction error rate (or any other performance measure on unseen data). In some instances, you will have a large predefined test dataset that you should never use when training. In the absence of access to this kind of dataset, cross validation can be used. 24 / 50

slide-26
SLIDE 26

Validation Set

The simplest option to use cross-validation is to create a validation set, where our dataset is randomly divided into training and validation sets. Then the validation is set aside, and not used at until until we are ready to compute test error rate (once, don't go back and check if you can improve it). 25 / 50

slide-27
SLIDE 27

A linear regression model was not appropriate for this dataset. Use polynomial regression as an illustrative example.

Validation Set

Let's look at our running example using automobile data, where we want to build a regression model to predict miles per gallon given other auto attributes. 26 / 50

slide-28
SLIDE 28

Validation Set

For polynomial regression, our regression model (for a single predictor ) is given as a degree polynomial. For model selection, we want to decide what degree we should use to model this data.

X d E[Y |X = x] = β0 + β1x + β2x2 + ⋯ + βdxd d

27 / 50

slide-29
SLIDE 29

Using the validation set method, split our data into a training set, fit the regression model with different polynomial degrees on the training set, measure test error on the validation set.

Validation Set

d

28 / 50

slide-30
SLIDE 30

Resampled validation set

The validation set approach can be prone to sampling issues. It can be highly variable as error rate is a random quantity and depends

  • n observations in training and validation sets.

29 / 50

slide-31
SLIDE 31

Resampled validation set

The validation set approach can be prone to sampling issues. It can be highly variable as error rate is a random quantity and depends

  • n observations in training and validation sets.

We can improve our estimate of test error by averaging multiple measurements of it (remember the law of large numbers). 30 / 50

slide-32
SLIDE 32

Resample validation set 10 times (yielding different validation and training sets) and averaging the resulting test errors.

Resampled validation set

31 / 50

slide-33
SLIDE 33

Leave-one-out Cross-Validation

This approach still has some issues. Each of the training sets in our validation approach only uses 50% of data to train, which leads to models that may not perform as well as models trained with the full dataset and thus we can overestimate error. 32 / 50

slide-34
SLIDE 34

Leave-one-out Cross-Validation

This approach still has some issues. Each of the training sets in our validation approach only uses 50% of data to train, which leads to models that may not perform as well as models trained with the full dataset and thus we can overestimate error. To alleviate this situation, we can extend our approach to the extreme: Make each single training point it's own validation set. 33 / 50

slide-35
SLIDE 35

Procedure: For each observation in data set:

  • a. Train model on all but -th
  • bservation
  • b. Predict response for -th
  • bservation
  • c. Calculate prediction error

Leave-one-out Cross-Validation

i i i

34 / 50

slide-36
SLIDE 36

This gives us the following cross- validation estimate of error.

Leave-one-out Cross-Validation

CV(n) = ∑

i

(yi − ^ yi)2 1 n

35 / 50

slide-37
SLIDE 37

Leave-one-out Cross-Validation

use

  • bservations to train each model

no sampling effects introduced since error is estimated on each sample

n − 1

36 / 50

slide-38
SLIDE 38

Leave-one-out Cross-Validation

use

  • bservations to train each model

no sampling effects introduced since error is estimated on each sample Disadvantages: Depending on the models we are trying to fit, it can be very costly to train models. Error estimate for each model is highly variable (since it comes from a single datapoint).

n − 1 n − 1

37 / 50

slide-39
SLIDE 39

Leave-one-out Cross-Validation

On our running example 38 / 50

slide-40
SLIDE 40

k-fold Cross-Validation

This discussion leads us to the most commonly used cross-validation approach k-fold Cross-Validation. 39 / 50

slide-41
SLIDE 41

Procedure: Partition observations randomly into groups (folds). For each of the groups of

  • bservations:

Train model on observations in the other folds Estimate test-set error (e.g., Mean Squared Error) on this fold

k-fold Cross-Validation

k k k − 1

40 / 50

slide-42
SLIDE 42

Procedure: Compute average error across folds where is mean squared error estimated on the -th fold

k-fold Cross-Validation

k CV(k) = ∑

i

MSEi 1 k MSEi i

41 / 50

slide-43
SLIDE 43

k-fold Cross-Validation

Fewer models to fit (only of them) Less variance in each of the computed test error estimates in each fold.

k

42 / 50

slide-44
SLIDE 44

k-fold Cross-Validation

Fewer models to fit (only of them) Less variance in each of the computed test error estimates in each fold. It can be shown that there is a slight bias (over estimating usually) in error estimate obtained from this procedure.

k

43 / 50

slide-45
SLIDE 45

k-fold Cross-Validation

Running Example 44 / 50

slide-46
SLIDE 46

Cross-Validation in Classication

Each of these procedures can be used for classification as well. In this case we would substitute MSE with performance metric of choice. E.g., error rate, accuracy, TPR, FPR, AUROC. 45 / 50

slide-47
SLIDE 47

Cross-Validation in Classication

Each of these procedures can be used for classification as well. In this case we would substitute MSE with performance metric of choice. E.g., error rate, accuracy, TPR, FPR, AUROC. Note however that not all of these work with LOOCV (e.g. AUROC since it can't be defined over single data points). 46 / 50

slide-48
SLIDE 48

Suppose you want to compare two classification models (logistic regression vs. a decision tree) on the Default dataset. We can use Cross-Validation to determine if one model is better than the other, using a -test for example.

Comparing models using cross-validation

t

47 / 50

slide-49
SLIDE 49

Comparing models using cross-validation

Using hypothesis testing: term estimate std.error statistic p.value (Intercept) 0.0267 0.0020306 13.148828 0.0000000 methodtree 0.0030 0.0028717 1.044677 0.3099998 In this case, we do not observe any significant difference between these two classification methods. 48 / 50

slide-50
SLIDE 50

Summary

Model selection and assessment are critical steps of data analysis. Error and accuracy statistics are not enough to understand classifier performance. Classifications can be done using probability cutoffs to trade, e.g., TPR- FPR (ROC curve), or precision-recall (PR curve). Area under ROC or PR curve summarize classifier performance across different cutoffs. 49 / 50

slide-51
SLIDE 51

Summary

Resampling methods are general tools used for this purpose. k-fold cross-validation can be used to provide larger training sets to algorithms while stabilizing empirical estimates of expected prediction error 50 / 50