Classification: Prof. Gheith Abandah
Classification: Prof. Gheith Abandah
1
Introduction
• YouTube Video: Machine Learning - Supervised
Learning Classification from Cognitive Class
https://youtu.be/Lf2bCQIktTo
2
Outline
1. MNIST dataset
2. Training a binary classifier
3. Performance measures
4. Multiclass classification
5. Multilabel classification
6. Exercise
3
1. MNIST Dataset
4
1.1. Get the Data
5
1.2. Extract Features and Labels
6
1.3. Examine One Image
7
1.4. Split the Data
8
Outline
1. MNIST dataset
2. Training a binary classifier
3. Performance measures
4. Multiclass classification
5. Multilabel classification
6. Exercise
9
2. Training a Binary Classifier
• A binary classifier can classify two classes.
• For example, classifier for the number 5, capable of
distinguishing between two classes, 5 and not-5.
Stochastic Gradient
Descent (SGD) classifier
10
2. Training a Binary Classifier
• Note that a better classifier for this problem is the
Random Forest Classifier.
11
Outline
1. MNIST dataset
2. Training a binary classifier
3. Performance measures
4. Multiclass classification
5. Multilabel classification
6. Exercise
12
3. Performance Measures
13
3.1. Accuracy
14
3.2. Confusion Matrix
15
3.2. Confusion Matrix
• Scikit Learn has a function for finding the confusion
matrix.
16
3.3. Precision and Recall
Precision Recall
18
3.4. Precision/Recall Tradeoff
19
3.4. Precision/Recall Tradeoff
• The function cross_val_predict() can return
decision scores instead of predictions.
20
Outline
1. MNIST dataset
2. Training a binary classifier
3. Performance measures
4. Multiclass classification
5. Multilabel classification
6. Exercise
21
4. Multiclass Classification
• Multiclass classifiers can distinguish between more
than two classes.
• Some algorithms (such as Random Forest classifiers
or Naive Bayes classifiers) are capable of handling
multiple classes directly.
• Others (such as Support Vector Machine classifiers
or Linear classifiers) are strictly binary classifiers.
• There are two main strategies to perform multiclass
classification using multiple binary classifiers.
22
4.1. One-versus-All (OvA) Strategy
23
4.2. One-versus-One (OvO)
Strategy
• Train a binary classifier for every pair of digits.
• If there are N classes, need N × (N – 1) / 2 classifiers.
For MNIST, need 45 classifiers.
• To classify an image, run the image through all 45
classifiers and see which class wins the most duels.
• The main advantage of OvO is that each classifier only
needs to be trained on a subset of the training set.
• OvO is preferred for algorithms (such as Support Vector
Machine) that scale poorly with the size of the training
set.
24
4.3. Scikit Learn Support of
Multiclass Classification
• Scikit-Learn detects when you try to use a binary
classification algorithm for a multiclass
classification task, and it automatically runs OvA
(except for SVM classifiers for which it uses OvO).
25
4.3. Scikit Learn Support of
Multiclass Classification
• Note that the multiclass task is harder than the
binary task.
• Binary task:
• Multiclass task:
26
Outline
1. MNIST dataset
2. Training a binary classifier
3. Performance measures
4. Multiclass classification
5. Multilabel classification
6. Exercise
27
5. Multilabel Classification
• Classifiers that output multiple classes for each
instance.
Popular algorithm
28
Summary
1. MNIST dataset
2. Training a binary classifier
3. Performance measures
4. Multiclass classification
5. Multilabel classification
6. Exercise
29
Exercise
• Try to build a classifier for the MNIST dataset that
achieves over 97% accuracy on the test set. Hint:
the KNeighborsClassifier works quite well for this
task; you just need to find good hyperparameter
values (try a grid search on the weights and
n_neighbors hyperparameters).
30