Path: blob/master/lessons/lesson_08/code/MultiVariable_LogisticRegression-lab.ipynb
1904 views
Multi-Variable Logistic Regression and Classification Matrix
Authors: Sam Stack(DC)
Exercise Objectives
Hand on experience using Multi-Variable Logistic Regression
Review and Exploration of the Classification Matrix and its evaluation Metrics
Introduction to One vs. One and One vs. Rest Classifiers.
Lets get some data. One of the most popular classification datasets for Machine learning is the Iris Dataset, which can be loaded directly from sklearn.datasets
Sklearn datasets are imported as dictionaries and use keys to access specific aspects.
iris.data
: actual matrix of observationsiris.target
: target column for classificationiris.feature_names
: column names
Break down of classes 0 : Setosa 1 : Versicolour 2 : Virginica
Modelling This data is extreamly neat and tidy so no cleaning necessary and we can get right into modelling.
With a multivariable confusion matrix, some of our labellings (True Pos., True Neg., False Pos., False Neg.) get a little warped. We are no longer predicting one class from a null class we are classifiying into 3 distinguished classes.
The True diagonal stays the same as these are properly classified observations.
Class 0 | Class 1 | Class 2 | |
---|---|---|---|
Pred Class 0 | 15 | 0 | 0 |
Pred Class 1 | 0 | 11 | 0 |
Pred Class 2 | 0 | 1 | 11 |
It is better to stick with True and False labels with multi-class to avoid ...Confusion
If you need to reffer to a False Positive or True Negative it is better to first select a specific class, such as Class 2
and refer to classification or misclassification relative to said choosen class instead of the set of all classes as a whole.
Example: True Negatives relative to Class 2 are True Positives for Class 0 and Class 1.
Speaking of our Classes? How are probabilities calculated with multi class?
Are they Probability of
Class 0
vs.Not Class 0
?Or Probability of
Class 0
vs.Class 1
vs.Class 2
?
Looks like our probabilities of each class all add up to 1, so it is like Class 0
vs. Class 1
vs. Class 2
.
What if we wanted to create a logistic regression that has Class 0
vs. Class 1
& Class 2
or just Class 0
vs. Class 2
? We will cover that in a bit, but first more evaluation metrics.
Classification Reports/Matrix
Classification reports are another means of evauliation classification models and return a few metrics that are based on True Positives, False Positives and False Negatives.
Precision
"How many of the items selected are relevant."
Of the items placed into a class, how many of the are True Positives.
Recall
"How many of the relevant items are selected."
Of the items that were suppose to be placed into a class, how many did we accurately place.
F1-Score
F1 exists on a range of 0 - 1 where 0 is just aweful and 1 is perfection. F1 is considered a harmonic mean as it averages Precision and Recall. With classification models you often times have to chooise what kind of error you are willing to increase in order to reduce the other and thus you may want to optimize Precision or Recall accordingly. If you are uncertain which you should optimize, F1 score may be the metric of choice.
Support Number of true observations in given class. The count of possible true observations.
Intro to Ensembling
Earlier we talked about building models relative to class combinations. Distinguishing One class from all other classes or just One specific class from another specific class. These goals are possible with Logistic Regression.
Up until this point we have used one model, but there are also Machine Learning methods that involve combining several models to come to a more refined conclusion, commonly reffered to as Ensemble Methods.
One Vs. Rest Classification.
One vs. Rest Classification is a method that builds an individual model for each class to try to distingush said specific class from the rest of the classes. Since we are only focusing on one class, Class 1
these classfiers will group Class2
, Class3
, Class4
into a single class of Not Class 1
. Same all the way through for the rest of the classes.
1 - Class1 vs. Class2, Class3, Class4 2 - Class2 vs. Class1, Class3, Class4 3 - Class3 vs. Class1, Class2, Class4 4 - Class4 vs. Class1, Class2, Class3
One Vs. One Classification.
We train a model for every set of classes. As more classes are added this becomes more computationally expense.
1 - Class1 vs. Class2 2 - Class1 vs. Class3 3 - Class1 vs. Class4 4 - Class2 vs. Class3 5 - Class2 vs. Class4 6 - Class3 vs. Class4
One Vs. Rest Classifier
One Vs. One Classifier
One Vs. One/Rest Classifiers are not restricted to fitting using Logistic Regression. With SKLearn, any type of Classification model can be placed into the One Vs X classification ensemble.