Hello, everyone, and welcome to this new and exciting session in which we are going to see how to
solve the problem of class imbalance.
First of all, before trying to solve this problem, we have to first start by understanding what it
is first.
So here we have this data set or trained data set made of a 1525 data points for angry, 3019 for happy
2255 for set.
Now, just looking at this numbers, it's clear that any model you want to train on this data set will
see more of happy images.
And this automatically creates a bias in which the model now will be better or will do better on the
happy images as compared to the angry and the sad images.
And also, this model will do better on the sad images as compared to the angry images so we could put
them in this order.
One, two, three to confirm this hypothesis.
Let's take a look at this confusion matrix right here.
You'll see that this is the actual and this is the predicted then year we have correct predictions.
So your correct predictions.
Correct predictions and your correct predictions.
But one thing you will notice is that this is angry.
This is happy and this is sad.
We might just draw this year, actually.
So let's just put this out here.
Here we go.
And as we were saying, one thing you will notice is that the number of cases where the input image
is actually happy and the model predicts angry or sad, that is, 19 and year 59 is going to be fewer
than the number of cases where the model is or where the model is supposed to predict angry, but it
instead predicts happy and sad.
So here you have 58.
And 78.
And when you add this to up what you get, 136.
And so this means that the model tends to make more errors when shown angry images as compared to when
shown happy images.
We could also look at the example of the sad images here for the sad.
What we have here is 47 and 8147, 81.
If we add this to up, we have 128 mistakes.
And so on.
The section would use a technique known as class weighting to try to reduce the effect of this bias,
which has been created because of an uneven distribution in the number of samples per class in our dataset.
Before we go on to explain how class weighting can be used to solve this problem of the class imbalance,
what we want to make you understand is the very first thing you want to do is to build a balanced dataset.
So if you have three classes, for example, angry, happy and sad, then you should gather as much
cleaned data as possible from those three classes and also ensure that this class is are balanced.
So, for example, you want to create some much more sophisticated system.
We could gather 100,000 of this.
100,000.
We got a year.
100,000 and your 100,000.
So you should initially strive to have this balance nonetheless, in some cases or in some certain problems,
getting this kind of balance will be very difficult.
And so we'll just have to resort to a technique or one of the techniques that is class weighting in
order to understand class weighting.
Recall that you have the output level and then you have what the model outputs.
Let's call this Y Chapo.
So the model outputs this and this is what it's supposed to output.
Now to be able to update the weights here, what we do is we compute this difference or this loss here
between these two outputs, that's between the expected output and what the model outputs.
And we are trying to or the aim here is to minimize this difference between this expected and actually
predicted output.
Now, since we know that the model has a tendency of favoring the images or the happy images, what
we'll do is we are going to penalize the model more when it predicts a wrong output for an angry image.
And so we'll penalize more.
So how are we going to penalize this one?
The errors from your more than the errors from your.
And by doing so, the model's weights will try to be will be readjusted such that it doesn't favor another
or favor any one of the classes.
Now, that said, we are going to include the class weight here in this fit method.
So we'll just have this class weights and then we'll have a class weights dictionary.
So we're going to define this class with dictionary before this one year.
We have three classes, so we have class zero.
I will have his weight next would have class one and we would have his weights.
Next we would have class two and we would have his weights.
So here we have the angry, the happy and the sad class.
Now, to obtain those weights, we can use a Formula one divided by the number of samples.
And since here we have this number of samples for angry to be 1525 happy 301 nine set to 255, then
one we calculate this weights here would have one divided by this you'll see that if we take one year,
the weights will now be one divided by this number of samples.
Zero.
There we go.
And then here we have one divided by number of samples for class one and one divided by number of samples
for class two.
Now, once we have this, we could also normalize this value so that there is no so much great difference
or the values are in similar order of magnitude.
So let's add this code so and then print out the class weights.
We run this and we print out our class weights.
There we go.
We have this error number of samples, zero.
Let's run this here and run this.
Okay, So we have this.
We have our class weights.
You see this one, this and this.
Okay, Now we're going to multiply each and every value by the sum of all the total number of data points
we have.
So yeah, we're going to multiply this by 6769 790 96799 and you're six, 799.
There we go.
You're six, seven, nine, nine.
We have that.
We run this, get this class weights.
That's it.
4.45, 2.253.01.
So we see that your we have this class that the angry class has more weights compared to the happy and
the sad.
And this will now influence the model when updating its parameters since the loss will now punish the
model more for making mistakes with inputs from this sample as compared to input from the sample or
this other sample.
So that's it.
We have our class weights.
We can now go ahead and add this to our training.
So we have this.
We've added this already.
Even so, we just need to have this and then run this again.
Now we'll start this pre trained, we start this.
So we just from here we have our pre trained model, we'll recompile this, there we go.
And then we start training for over 60 E books.
So here we have trained and here are the results we get, We go ahead and evaluate the model.
We have 84% accuracy.
We test here, we have this different images which we pass.
We, we actually make here just one error.
So that's it.
We now get to the confusion matrix, which we have plotted.
And one thing we can notice straight away is the fact that the errors made by the model per class is
more evenly distributed.
So you could see here, let's have this here.
You could see here that the model makes around 118 errors for the angry images, while for the happy
makes about 100 and or rather 97 errors.
And then for this it makes 148 errors.
So although we have we still have this difference, which is inevitable now, this margin has been reduced
and that's it for this section, the solving the problem of class imbalance.
We have other techniques to.
Remedy or solve this problem like over sampling and under sampling.
In other sampling, what we have is we suppose now we have this initial data set, so let's just angry,
happy and sad.
And so what we are going to do here is we are going to randomly pick some elements from this angry sample
right here and added up to the available data we already have.
Your And so this all makes up for this remaining gap we have here and now.
It matches up with the number of happy samples.
And then for the sad samples, we again randomly pick some samples from here and add it up to this dataset
so that we now have this data set which has which matches up with a number of samples for the happy
now, whereas with down sampling, what we are instead going to do is we are going to cut this off here.
So we'll cut this off and we'll cut this part of.
So now what we're going to have is this year.
So we're going to take off some parts of our data set.
So now all of this match up instead with this angry class of our dataset.
So now here we have that, we have the happy C matches up and then we have the sad.
So that's our DOWNSAMPLING.
And since this part which is taken off, is taken off randomly, we call this random DOWNSAMPLING.
Nonetheless, it's not advisable to use over sampling or under sampling when solving real world problems
as either you're adding already existing data like this, which is not necessarily a great idea, or
you even removing data from your dataset, which is not also a great idea.
So if you have a problem of class imbalance, the very first thing which comes to your mind or we should
come to your mind should be that of trying to balance this data by gathering much more data from a particular
class.