Update: If you are interested in getting a running start to machine learning and deep learning, I have created a course that I’m offering to my dedicated readers for just $9.99. Access it here on Udemy. If you are only here for Matlab, continue reading =]
This is the second in my series of implementing low-level machine learning algorithms in Matlab. We first did linear regression with gradient descent and now we’re working with the more popular naive bayes classifier. As is evident from the name, NB it is a classifier i.e. it sorts data points into classes based on some features. We’ll be writing code for NB using low-level matlab (meaning we won’t use matlab’s implementation of NB). Here’s the example we’ve taken (with a bit of modification) from here.
Consider the following vector:
(likes shortbread, likes lager, eats porridge, watched England play football, nationality)T
A vector would describe that a person likes shortbread, does not like lager, eats porridge, has not watched England play football and is a national of Scottland. The final point is the class that we want to predict and takes two values: 1 for Scottish, 0 for English.
Here’s the data we’re given:
X = [ 0 0 1 1 0 ;
1 0 1 0 0 ;
1 1 0 1 0 ;
1 1 0 0 0 ;
0 1 0 1 0 ;
0 0 1 0 0 ;
1 0 1 1 1 ;
1 1 0 1 1 ;
1 1 1 0 1 ;
1 1 1 0 1 ;
1 1 1 1 1 ;
1 0 1 0 1 ;
1 0 0 0 1 ];
Notice that usually when we represent data, we write features in columns, instances in rows. If this is the case, we need to get the data in proper orientation: features in rows, instances in columns. That’s the convention. Also, we need to separate the class from the feature set:
Y = X(:,5); X = X(:,1:4)'; % X in proper format now.
Alright. Now, that we have the data, let’s hear some theory. As always, this isn’t a tutorial on statistics. Go read about the theory somewhere else. This is just a refresher:
In order to predict the class from a feature set, we need to find out the probability of Y given X (where
with n being the number of features. We denote the number of instances given to us as m. In our example, n = 4, m = 13. The probability of Y given X is:
Which is called the Bayes rule. Now, we make the NB assumption: All features in the feature set are independant of each other! Strong assumption but usually works. Given this assumption, we need to find .
(The weird braces notation that follows is the indicator notation. means use 1 only if condition v holds, 0 otherwise.)
To find , you just have to find for all features and multiply them together. This is where the assumption comes in. You need the assumption of independence here for this.
This equation basically means count the number of instances for which both x_i and Y are 1 and divide by the count of Y being 1. That’s the probability of x_i appearing with Y. Fairly straight forward if you think about it.
Same as above. Count the ratio of Y=1 with the total number of Ys. Notice that we need to calculate all these for both Y=0 and Y=1 because we need both in the first equation. Let’s begin from the bottom up. For all of below, consider E as 0 and S as 1 since we consider being Scottish as being in class 1 (positive example).
pS = sum (Y)/size(Y,1); % all rows with Y = 1 pE = sum(1 - Y)/size(Y,1); % all rows with Y = 0
phiS = X * Y / sum(Y); % all instances for which attrib phi(i) and Y are both 1 % meaning all Scotts with attribute phi(i) = 1 phiE = X * (1-Y) / sum(1-Y) ; % all instances for which attrib phi(i) = 1 and Y =0 % meaning all English with attribute phi(i) = 1
PhiS and PhiE are vectors that store the probabilities for all attributes. Now that we have the probabilities, we’re ready to make a prediction. Let’s get a test datapoint:
x=[1 0 1 0]'; % test point
And calculate the probabilities P(X|Y=1) and P(X|Y=0)
pxS = prod(phiS.^x.*(1-phiS).^(1-x)); pxE = prod(phiE.^x.*(1-phiE).^(1-x));
And finally, the probabilities of P(Y=1|X) and P(Y=0|X)
pxSF = (pxS * pS ) / (pxS + pxE) pxEF = (pxE * pS ) / (pxS + pxE)
They should add upto 1 since there are only two classes. Now you can define a threshold for deciding whether the class should be considered 1 or 0 based on these probabilities. In this case, we can consider this test point to belong to class 1 since the probability pxSF > 0.5.
And there you have it!