Hello,
I am (name) from LearnVern.
We had studied in its theory that naive (naa-eev) bayes works upon its probability, and in that works upon conditional probability and further conditional probability brings out the Bayes theorem, and from this the classifiers are formed, which are called naive bayes classifiers.
So, we will implement this and see how we can execute this.
We will use this also on iris dataset, because if we work on the same dataset, then it's easier for us to understand the different concept, However I have also performed some algorithm on different dataset as well which you can see, and if possible we will also use some more examples to demonstrate these algorithms.
Ok! So we will first begin with importing the libraries, before that I will click on this connect option, so that till I am importing it get connected also.
So, from S K learn dot datasets import load iris, so at first we need the iris dataset,
Then next, from S K learn dot model selection,
Ok, so now I will show you something new, meaning uptil now we have seen to split the data between training and testing, so now we will implement the same thing and see.
In our previous algorithm we didn't implement it till now.
So, from S K learn dot model selection we will import train test split, so this training and testing will divide our data, by just telling it the set amount required for training and testing individually.
We can say that by % that 80% this & 20% testing.
Next, from the S K learn dot package, the name itself is naive bayes, N A I V E so there are many inside naive Bayes, so we are going to use Gaussian Naive Bayes.
So, these are imported.
Now, I will initialise X and Y,
X comma Y is equal to load iris and in this I have a return function, so return X,Y and this I will make it as true.
Ok! So, now I have X and Y.
Now, let me show you.
So, this is our X and this is my target Y
Ok with the help of a train test split, I will split this.
So, split into training and testing
So, firstly I will have X underscore T R A I N train comma, next will come X underscore test, then Y underscore train and lastly Y underscore test.
Now, I will take the help of train test split, and in this I will pass X and Y. These are my original X and Y, along with that I will pass the test underscore size. I want to keep the test size very small, suppose I want to keep it only 10%. You have to enter how much you want.
So supposedly if you want you can keep 0.10 percent. Next initialise a random state, so random underscore S T A T E (state), and give that a value as zero.
So, this way I have divided my data.
Now, if you want to see what is there in X underscore train, then you can do that.
So, this is our X train. Now here it is not showing the total number of records, so for that here I will put 'shape', and then we can see.
So, there are 135 records.
And similarly for X underscore test, it will be less, let's see, so this is our record, obviously looking at it, seems very less, we will find the count through shape.
So, here we have 15 records.
So, out of 150, 10 percent of the record means 15 records have been splitted for testing.
So, this is how our data is split.
Similarly, y is also splitted in the same way between Y train and Y test.
Now, we will have to create an object for the model.
So, as we are using gaussian, so
GNB gaussian naive bayes is equal to GaussianNB, now we will execute this.
So, this is our object created.
After creating an object.
First I will have to train this.
So, in GNB dot we will use fit method, and in GNB dot fit, I will put training data, so X underscore train, Y underscore train. so we will pass X train and Y train, these two datas.
So, what will happen with this, my model will be prepared after training.
Now, by using GNB dot predict here I will put X underscore test.
Now, here you can see it has already given us the prediction.
Now, along with that I will show you print Y underscore test.
So, this is Y underscore test 2,1,0,2
Now, if we match it, then you can see that here in the last there is 1111.
But, here we have 2 also, which means there are some mistakes or errors due to wrong classifications, which are possible, that it can happen.
Now, this same thing you can also visualise and see.
For that we can import from matplotlib.. import pyplot as PLT .
Here, you can plot, so PLT dot scatter, here we will pass one variable, that should be done from X underscore test.
Before that let us add one more code and check the data first by displaying before putting it there.
So, X test off, so how many rows do we want? So we want all the columns but only one row, ok! So there is a mistake in the symbols, ok! We got all the columns and just one row,
So, basically we want the opposite of this data.
So, we want all the rows but only one column zeroth.
So, here we will take all the rows but only zeroth column, next again X underscore test in this we will take all the rows but only first column, so this is our X and Y,
Next, we will have to put C,
So, for that let me just utilise our prediction by adding Y pred over here, so that we can utilise.
So, we have put this.
Next, I will put this C in Y underscore pred.
In the similar way, again PLT dot scatter and here X underscore test all the rows but zeroth columns, and Y will be X underscore test colon comma 1, and the colour will be Y underscore test.
So, here we have given a Y underscore test, we need to put an underscore instead of a hyphen.
Now, you see both the diagrams.
By looking at them we can make out the difference between them.
So, this points over here if we check it is in yellow in actual data but in classification it is wrongly classified as blue.
So, this is the mistake of the algorithm.
So, in the next video we will also learn to check the accuracy of the data.
So, here we have understood to apply naive bayes algorithm, and after that to interpret it and see also through visualisation.
So, friends, let's conclude here for today, and it's further parts we will cover in the next session.
So, keep learning and remain motivated.
Thank you.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.