Complete Implementation Of Perceptron In Deep Learning Using Python From Scratch

Krish Naik · Beginner ·📐 ML Fundamentals ·4y ago

Key Takeaways

Implements a Perceptron in deep learning using Python from scratch

Full Transcript

so hello ladies and gentlemen welcome to another session of introduction to deep learning in this session we will continue from where we left off so right now we are going to implement the perceptron algorithm again further so let me share my screen okay so that we are here so right now what i did is i have just kept the images for your references that we have already learned in the previous sessions so as you can see so we will we are actually trying to build these things only okay so let me just walk you through here so as you can see here in the perceptron so let me make it slightly bigger here so that you can watch it clearly and as you can see so perceptron y so we have like kept few things which is actually done in the previous session and then i was talking about the z.com the z outcome is actually this outcome that you can see on the diagram and then activation function that you can see so as for perceptron it was used as a step function so we were mentioning this here and then we will be implementing then similarly fit method is for the training purpose prediction is for the predict method that is means predict is nothing but its forward pass or whatever when the weights are getting trained and then once it is trained so when whenever you start passing the input values here and you give an outcome so it is nothing but it gives you a it's actually a forward pass only nothing that nothing more than that so that's the prediction that we are going to build here and then there is total total loss that means after passing all those data so what is the total loss or average loss that is we are going to calculate here then after that saving of the model that means whatever the model or the weights we have trained we can save it by using this method and if you want to load the metal again so you can also load the method by this method okay there is a load method and then create directory and return path is actually let's say if you want to save a method or save a model so we can save it in a certain directory so it will be taken care by this method so that's what we are going to build here in this section so let's try to work on this one so as you can see here so let's try to work upon the few important things so let's try to first of all go into the fit method itself okay and then we will gradually proceed further so in the fit method what will happen so let's say you have got the basic stuff that means you have got these like basic stuff ready okay so now lets say after that we want to implement the fit method so in the fit method what we have to do is we have to pass the your okay we need to pass the x value that is your okay just a second i guess yeah so we are actually passing the x value and then y value that is your label and x is your input data okay and we are going to pass this in this fit method so this will take these values and after that let's see what we can do so here let's define it self x is equal to x and self dot y as equals to y so we have got these two values now so after this what we have to do we have to create with we have to attach bias with them so x with bias is equals to we will be creating a concatenation operation here so np dot c underscore this will help us to concatenate so we want to concatenate concatenate itself x with the minus that is np once okay and then we have to provide the shape of it so shape should be as it should be equal into the input value that you are providing okay so let's try to do this so we are going to be length of self dot x okay and it's going to be your comma and then once it's going to be a column matrix of the length of psi self dot x okay so we have already done this calculation in the previous classes so we are just following the same thing here okay so just integrating the matrix of x with bias then after that if you want so you can also print this status so we can just simply say that x with bias x with bias is equals to or you can simply a new line character we can introduce and then we can mention is here that x with bias okay now let's close this so we are done so we have no it will be printing the x with the bias terms okay now for this what we have to do we have to run for the epochs so for epoch in range this will be equals to self.ebox okay so it will be the data will pass through this and then we will be uh printing the like which the pocket is so let's try to print that so it is going to be let's say for epoch and then we can mention in this form let's say it is going to be your epoch value so it will be printing the epochs okay you can just press tab also so it will give me suggestions here so this will be printing the epochs at which epoch it is okay then after that what we have to do is we will be creating the or we will be calculating the z value okay so z is equals to it's going to be self dot and this will be calculating with the help of the internal function that we have built that is z outcome or z outcome and we will take the x with bias okay and also it will take the self dot bits okay so we'll underline the weights with the biases okay actually uh that is x with the bias and these weights so this will give us the z value and then after that we will be passing this v hat is equals to that means actually y hat is equals to that is your predicted value is equal to self dot and this is going to be an activation function which will be passing through the activation function activation function will take this z value and it will return the outcome so now let's try to first of all build this this internal of z outcome so z outcome is actually nothing but it's going to be simply it is going to return me and also it will take the these values that we were expecting here so we will be passing so these values will be passed here so let's try to put it here in this section okay so this will take the self x with bias and then and so and after that it will try to return me the product of this so np dot okay and p dot or we can simply call this instead of calling this we can simply call them as x and w okay or simply weights weights and this is going to be input input with bias inputs okay so it is going to np dot and then inputs comma bits this will return me the product of this thing so cloud weights times the inputs okay that's what gives me the uh that this will give me the z outcome okay since it this input also include the biases so it will give me w x plus b okay that will be the final outcome that it will be giving and then after that that will be passed to your z value will be passed with the activation function so activation function will now provide me the product like predicted value so activation function will return what so let's try to build that so it will be returning me the like whether if it is if the terms or if the z outcome is negative so it will give me simply it will give me a zero if it is positive so it will give me one so np dot where we will be utilizing so we can use this or we can also use the if else condition so it's up to us so right now i'm using the this np dot where function so it says that if the z outcome that we are passing into this one if the z outcome is if it is greater than zero then provide me 1 and if it is not greater than 0 then provide me 0 okay so this is an a false condition that we have written here in the activation function this is nothing but a step function that we have written okay that is this section that we are trying to implement here and now after this what we will do is simply this will give me an activation function outcome that means the predicted value or the y hat value we will be getting so we can simply mention here also that printing the values like so f so we can say that um predicted value after forward pass is going to be this y underscore at so this is what we have we are getting we can keep it in the new line so that it can give looks clean and then after that we will be calculating what is left we have to calculate the error as well so self dot error is equals to self dot y minus y hat so itself dot y that's not number it's actually the actual value okay so here we can mention that okay what is the error so we can say that error as is equals to new line and then we can mention them setup.error okay so this will give the or which will print the errors and then we can simply say that okay self. weights okay weights is equals to that is going to be weight update rule now the self load weight that is the old weights that we have okay and then plus learning rate that is dot eta okay multiplied by we have got the simply that is np dot okay or we can simply use the again the z outcome or we can simply say but that is still so it's going to be simply x that is x i as if you can see cell dot error times this so x with bias dot transpose and then we need to pass the self dot error so this will give me the weight update okay so that is the weight of uh this is weight update rule that is w plus learning rate times error okay and times this x input or x i input so this will give me the error and or the updated weight so we can simply print these values so print and then we can mention that okay so updated updated waits after epoch so which epoch so let's mention that so we can mention that okay that it is these uh epoch out of how many epochs so we can also mention that itself epochs and then after that we can simply mention that what is the weight update so rate update is nothing but going to be this is your self dot weights so this will bring the weight outcome so this will give us the uh that is the every box okay so it will try to give me the outcome this so we have got the basic setup done now so after that we will be in the next session what we will do we will simply invent this predict method total loss and all these things okay so till then keep on exploring keep on reading and stay tuned with me thank you for attending this one so hello ladies and gentlemen welcome to another session of introduction to deep learning session and we are actually working on the implementation of the perceptron so this is going to be the third part of it so what we have done in the previous session so let's get started from there on onwards okay so we have actually created this init method then outcome activation and the fit method so we were actually left with the predict method total loss and these actually create directory return hidden uh you can say method and then save method and load method so once we are done with it then we will be able to utilize this class so from the previous one i have actually added some few extra lines here you can see this for the segregation purpose so that this will segregate the every epoch and the end of each epoch will also be segregated by these hash so has double hash timestamp that means it will be creating a 20 hash so it will look like there is a separation between these prints okay for every bob so that's why i did it now let's try to implement the predict method so in the predict method what you do you normally provide a input which can be let's say let's call this as a x as of now okay which is going to be your input now after that as you know that we have to first of all create we need to add a bias because that's what we have to do here that's what in the forward propagation has been done so x with y so what we can do is we can simply copy paste all those few other things right so here also if you can see here what we do is we create this x with bias okay so we can keep this here as it is only thing that we have to change is we need to remove this instead of self.x i will write x because it is just equal with the x value that you are passing here okay or otherwise if you are if you feel that this second is slightly confusing so what we can see we can also name it something else let's call this as some some sort of you can say um let's say test test input okay or you can say inputs for production so we can name it anything so let's keep it very simple i think you will be able to understand these things so x is nothing but here it is actually the input that you will be providing for prediction purpose so now we have created x with biases okay so after that we have to do what we have to calculate the z value okay the z value will be taking the what this is z is going to take your like input value okay so it will it can we can use here what we can simply use our self underscore z outcome here simply okay so it will take the x with bias that we have created here x with bias also it will take the updated weights because we are doing the prediction so weights these several orbits are nothing but the train weights that we have so this will give the z outcome and after that what we have to do we will simply call for the return method okay so return will do what return the self dot pass these values to the activation function okay so the z value that you have calculated will be passed to the activation function so this is all about your prediction part so after this prediction what we have to do is let's say you want to calculate the loss so obviously so for the last calculation what you have to do so you see we have to simply say total underscore loss is equals to it will take some of the np dot sum of self dot error that we have calculated while training so that's what we are trying to calculate here we will simply print that okay f so total loss okay is equals to you can simply mention total loss that we have calculated here okay so this will give the total loss here and then we can also segregate these steps so we can use backward slash and as a new line character on each of them and also if you want we can return this loss as well let's say if you want to utilize it somewhere so you can just have this return method as well here now for that what you have to do is let's try to save the your like whatever model you have created we will be saving it so in order to save that model what we have to do is first of all we need few things so what we need is where you want to save so we need to provide a file name here okay so let's provide file name and also we need to specify that model directory where let's say if you have not created or created a model directory where you want to save these models that you have created right now okay so we will be using this model directory path okay where we will be storing these models inside that so these are the two things that we need now since i have already passed default value as none so let's say so we can write a condition for that let's say if model underscore directory is not none okay that means if you have provided these things so in that case what we have to do is we will call model file path where we want to store model file path is equals to going to be self dot underscore create a direction so we will be utilizing that hidden function so it will take the model directory as it is and file name so based on this it will first of all try to wait on this model directory but it will try to create a folder and then it will attach the uh with the land then it will create a path for this one okay with model directory and filename and it will return this this function is that job so we can simply mention these things here so model directory and filename is it is going to take these two values and it will first of all it will take the it will make the directory first of all so make directories okay and then it will take the model directory and the it will first of all it will say that exist okay is equal to 2 so that it does not give me error if it is already existing so next thing is that return the path that you are going to create so os.path dot join and it will take the model directory and also the file name so this will so first of all before we write the model we need to have the directory parent directory so that's what it is doing here it is creating a directory here and then we are saving the file name inside that or actually the file which we have created so that will be saving it here so once we are done with this model.file path there then after that we can simply call this job lib we store them as a binary dump okay and then what we want to save we want to save the self that means whatever has been calculated these self values actually getting going to be saved as a binary and that's what we are going to use okay we are going to use the weights and all so that's what we are going to use so we are going to save our self okay then model directory or model file path we will be passing it here so that where we want to save this binary we want to save it as model file path next is about the else statement so statement is now model file path is equals to okay again self dot create return and let's say you did not give any any file name so in that case we will create a file name we will actually model directory so we will create in that case we will be creating the model directory for you so model directory will be created file name will be passed and restore things will remain same so job lib dot dump dump the self value and then let it sell for it let me self all the uh like these models like self dot x f dot error then everything will be saved here okay by using this job dot down in this model file path so this will actually save our model now let's say if you want to load the model so for the loading model what you need you simply need a file path that where the data is located or where the model is located so it will take this and it will return the i will read it by job leave dot load and it will take the file path okay so this will return me the and without this this will simply return the loaded uh you can say model that we have created so this creates our or this completes our perceptron class that we have built okay now in the next session what we will do we will try to utilize this hopefully you might uh like encounter some errors so we will be resolving on the way although it seems correct till now okay but sometimes it happens that we may submit something so it's okay that is the past part of the process okay so till then keep on exploring keep on reading and stay tuned with me thank you so hello ladies and gentlemen welcome to another session of introduction to deep learning in this section we will continue from where we have left off so we were actually working on the perceptron algorithm implementation using python so let's get started so as you can recall from the previous session you can see that we have implemented the perceptron class here okay so now let's get started let's try to utilize it let's see how it works okay so now in this section what we will do is first of all let's try to define a data set so we are actually working on the data set that is and gate okay so and gate will be actually we will be trying to make our perceptron model learn the and gate okay so we'll try to do that so first of all we need to define that okay what is our x1 so x1 is going to be a list okay so it is going to be a 0 comma 0 comma 1 comma 1 and then we have to specify the x2 okay x2 is equals to it's going to be 0 comma 1 0 comma 1 okay and then we have to specify the label so label data we are going to have is is equals to is going to be so since we are working for hand gate so and gate is actually it only gives one when you have all both the values are high so that's the outcome otherwise it will provide you a zero value okay so this is our and gate now let's try to convert them into pandas data frame so it is going to be pd dot data frame okay and we will pass this dictionary directly so this will create a data frame so let's try to check the data frame so as you can see x1 x2y and this is going to be your and gate data set okay so this is your and gate data set and now let's try to do what let's try to first of all or before that i think we should do one thing so let's try to make it uh let's give it a specific name so let's call this df and this so this will be data frame for and gate okay so this is done now let's try to first of all segregate these so that we can pass it the to the our model okay so for that what we have to do we can define one function which will simply do a preparation for us or preparation for the data frame so what will be this function so let's try to define this function just above this one cell okay so we will call this function or helpful function as prepare data okay so prepare data will just simply take the data frame what it will do and then this will first of all it will try to give you like df it will drop the column okay it will drop the column that is your label column and access if it's a label success is equals to one and then it will after dropping so it will provide a x value that is your features and then y will be simply is equals to df and then that is your target column so now after this it will try to return me x and y okay so to make it generic more generic so what we can do is we can also provide here one more axis or one more you can say target and let's call this as a target column okay so target column in our case it is y by default okay so let's say if you try to provide something else so we can make it more generic here so we can try to provide target column as something else okay so in this case we can replace it with this one and this thing so what it will do it will simply provide me and label which is why and the features out of the your data frame so this will do the preparation of the data okay so now let's try to utilize our perception class so first of all what we have to do is we have to get the data the data we have to prepare the data so prepare data and then we will be passing the data frame by default since it is taking y as a target column so we will leave as it is okay and we will provide df and as a data frame so this will give me x and y now let's try to define the learning rate so learning that we are trying to use is let's say 0.3 okay or 0.1 actually which lies between zero and one okay so this will be varying between zero and one and then after that we have to specify the epochs how many epochs we want to run so epox is equals to let's say let's call this as let's say 10 okay so 10 number of epochs we are going to clear and let's try to define model and so this is going to be perceptron okay class and then we need to provide the eta so eta is equal to that we have defined and apart from that we have epochs so we can also define the epochs which is going to be loopbox that we have defined okay so this will now create the model object and then after that we can simply call model.fit so this will be model and dot fit okay we'll just call fit the fit method on what on the x and y so we will be passing these things and then after that we will be creating a model dot also we will be calculating at the end that is model dot total loss okay so we can check the total loss at the end and why i'm using underscore because it's a dummy variable that means we are not going to use this so we're just calling it underscore otherwise you can skip this part as well so it's not in harm it will simply return the total loss because we have already done our total loss here in this case so now let's try to run this hopefully everything seems fine let's try to learn this so it takes like initial ways before training so it says a type error one takes personal number three were given so let's try to understand that what happened here okay so most of the time this may be possibility that you might come across this kind of issue okay so it seems like we have not yet started you can see from here actually we have defined from the large so what we can do is we can simply restart and run also let's try to run everything from the beginning so that it can take everything from the beginning itself otherwise what happened that sometimes let's see if i've defined in this cell and what will happen that if i have not run the cell again after let's say editing this cell so it will take the previous version okay of the previous values so that's why it was giving us an error but here if you see so it has started printing the outcomes so what you can do you can see that initial weights before training it was very low weights okay it was nearly zero and then after that we can see that that's with the bias term here okay so x with the bias is nothing but it's the same same that is zero zero zero one one zero and we have appended it with or you can say concatenate with your one that is your bias values and then for the epoch 0 we have rated value for the forward pass is going to be 0 0 1 so in the first pass itself it has given us a 0 error okay so you don't have to pass for the multiple errors actually but still we can just proceed further so you can see that luckily what we have got is that actually we have got error in the first chance itself is zero okay and you can see that since error is zero you will not see any weight update in any of the cases okay so this was actually a luck pure luck so what we can do is let's try to re-run it so hopefully we will see the differences here okay since it's a random one so we can see the differences and then it is zero let's try to see this so it actually is going to give us zero yeah here you can see now from here in this case it was not like that so in this case you can see the predicted value in the first forward pass was actually different so there was an error okay and uh after that it has in the second epoch you can see that there is one again one error was there okay then there was 30 again there was an error in the you can say fourth epoch you can see again there was an error and so on so in the fourth or you can say fifth epoch you will see that it was a zero error so after that you will see that it will be consistent the weights will be consistent it will getting utilized so you can see that now it is i can say at the end so we don't have to train actually to the 10th epoch as you can see here it was actually giving us total loss at the zero but right now you can see that it is 9 out of 10 it is running so what we can do is we can simply edit these things okay so what we are doing is actually we are just printing the exact epoch so we can simply say plus one here because epochs starts with zero okay if i give it range to ten so it will start with zero to nine so let's try to run this again and i think then we will be able to get the exact look okay so at the end you can see 10 out of 10 so this is now observatively so this time also let's see that okay whether you are able to get some errors or not yes there was few errors and it was able to solve properly so that means our perceptron algorithm is working fine so in the next session what we will do we will try to explore on different other types of gate like or gate target and we will say i will try to see that what kind of outcome we get okay and also we will try to plot these outcomes to verify our these things graphically so till then keep on exploding keep on learning and stay tuned with me thank you so hello ladies and welcome to another session of introduction to deep learning and in this session what we will do is we will simply explore further um in the perceptron algorithm so in the last section what we did is we have actually created and you can see we have already created a perceptron class we have also checked it for the end gate and so in this one we will try to explore for the or gate as well as the target so let's see that how it works okay so let's get back to work and here you can see that in the we have already created an and gate for you so now let's try to copy this and we will just try to explore for our this is for the or gate so let's call this as or gate so as you know that our guess actually gives you and okay if it is if both the either of the values are high so it will simply give you a it will simply give you a it will simply provide us a high value okay so let's try to look into this data set so as you can see the data set is actually let's say that either of them is i x minus two so it will provide you y as an output so this is the data set for our gate okay now let's try to work on the this part so what we have to do is we can simply copy paste this and instead of this we will be passing our or gate okay so let's call this r and here let's call this as model r okay so this will create actually the and this will actually just try to calculate the target as well now let's try to run this so as you can see that it is able to like get the errors in this one so it is actually getting the errors and so on so if we scroll over so you'll find out that okay after some time it will give start giving you a zero error in every case so at the end you will find out that okay total loss is exactly equal to zero so that means we were able to change this model as well and it was giving me the better results similarly what we can do is we can also explore for zargat let's try to experiment with the zargat resort it actually it's nothing but an exclusive or that means if either of them is high then it will provide you one but if both of them is high or both of them are low it will provide you with zero so let's try to explore the target as well okay so this is called as exclusive one and we can simply call for x here x here and then x here and it will provide you a zero in this case now let's try to run this so it will be this is the data set files or gate and now let's try to run our test here so we will be simply creating a model which is for the zor gate and this is going to be okay so this is let's try to see the results so as you can see uh if i just scroll over so let's see that whether it is able to reach to the zero or not so you can see that it is not it was not able to reach the minus and that is to zero so the error was actually still there now let's try to run it again so if you just see again the loss is exactly is equal to two if i run it again so you will find out that okay and the loss is not decreasing so what could be the reason behind this so we will be exploring this and the main reason as you can i can tell you from the theoretical understanding which we have already explored in the previous essence so it is that like the perceptron algorithm is actually no it's just it's a linear classifier it works for the linearly separable data set not for the non-linear data set and to experiment this we need to actually or to prove this we need to actually plot the graphs of these data sets that we have created so for that we will be exploring them in the next section but before that let's try to explore few more things here so as you can see in this section the perceptron class that we have implemented you can see also you have to find that okay there is one more way that is like if you want to save the model or load the model okay so that is also present so let's try to experiment with that so let's say you have created this and gate okay now you want to save him so what you have to do is simply call model and then add okay and then you need to provide few information here so as you can see by the definition here okay if i see the definition so it expects me the file name or model directory as well so if i try to get the model name here so model name okay so i'm trying to okay so it has to be model name model and then dot save method we need to call for and then if we specify here so file name is equals to since it's an and model so and we can say that dot model if you want or since it's typical files you can also say pkl as well so it's up to you the extension doesn't matter here because it's going to be binary file whether we write an extension here so it doesn't matter now after this we can specify that okay which is going to be my model directory that we can specify but let's say if we do not so what it will do it will simply create a modern directory and it will save it okay let's try to run this so you can see that it has actually run successfully now let's try to check it in the home page okay so if you check at the home page you will see there is a model uh because the directory is now created so and dot model is now present here so what we can do is apart from this now let's try to see if we can reload this model and understand that okay whether we can recall that uh you can say we can whether we can predict the method or not okay so let's try to reload reload and okay and we can simply recall it as let's say uh we can call for the perceptron algorithm a perception and now in this case we have to provide as you can see in this definition so if you can recall so by default it is none okay that epochs are done if you do not provide anything that means it will treat it as it will trade that it is now not training but if you provide that that means it is training so that's what we have written in this conditional statement so right now i am just initiating it and then after that we can simply call for load method so we are actually initiating or you can say again reinitializing the uh you can say that we are researching the perceptron class but in this time it is not training so we can simply call for load method okay and in this load method we can also call the path of this okay so let's call it as file path file path is equal to it is present in the model and model directory and the name of this is and model okay so if i try to do this so reload and so let's call for this one so you can see that it is now loaded now if i try to try and give me the if you try to prevent or you can say if you try to predict the math based on this one so we can simply call that row load reload and we can also call for the predict method okay and here we can simply specify x values equals to we need to provide it as let's say we can you it has to be a two dimensional matrix because that's how it is arranged the dataset is arranged if i provide 1 0 it should provide me the output as 0 okay so it is able to give me 0 because both of them are not one that is that means one of them is low and one of them is high so if i provide both of them high so it provides me one that means i was able to successfully reload the model and also i was able to predict the model as well okay that means this is how it works now let's try to do it for the uh this r model as well so we can simply copy i can simply save the model for the you can say uh for the for the r model as well so let's try to do it for the r model here since we have already created so we can simply save it here so model r dot save and let's call for our model so if i try to run this and this time what we can do is we can let's try to provide the directory structure or directory name model directory let's call it as a model or okay just for the simplicity now that means just to explain you that okay that this thing's works or not so if i try to do this we can observe here that okay this model r is actually created this folder is created now inside that our dot model is now saved so in this way we can also reload this model as well so it's our choice so we can give the full path of the model away from where we want to reload it and that's how you can save and reload the model so this is the method that we have created for those so as you can see in the perceptron class we have actually used all the methods that was already there now after this what we will do is we will simply try to plot all these graphs for them for the prediction of these output and then we will see that what kind of you can see the how we will try to explain that that y perceptron is actually applicable on the linear linearly separable data set or linear data set not on the non-linear date so that is actually a weak point or you can say a disadvantage of perceptron algorithm or you can say one perceptron so we will try to explore that in the next session till then keep on learning keep on exploring and stay tuned with me thank you for attending this one so hello ladies and welcome to another session of introduction to deep learning so in the last session what we did is we have already implemented the perceptron algorithm we have trained the model we have saved it we have loaded it to the model but as in this session what we will do is we will try to explore further we will try to justify that why it fails why perceptron fails for the zorg it why it survives for or why it gives the perfect results for and and or gate okay so the reason we will try to explore and actually we also know the reason theoretically that perceptron is actually uh works for the linear data set or linearly separable data set we can say so it only works for such kind of data set so that is the drawback of perceptron so we will try to justify this statement by doing certain experiments by plotting the graphs so let me show you one of the i can say this graphs here okay so right now i'm doing it by sketching it and i will write after that i will try to code it out as well so the objective here is as you can see on the screen so let me just explain it briefly so this is x1 axis and this is your x2 axis okay which is a vertical axis and it is actually pointing out these data sets that is present here so x1 and x2 we have got and y is the outcome so as you can see this is in since it is an and data so and it is actually it gives you one only when both the values are high so then only it will give you one and as you can see observe here so y is just giving us zero or one so that means the classes are zero or one now the zero classes are actually represented as a hollow circle and the one class is absorbed as a like it's a dark circle okay so as you can see on the screen so what i did is i have marked it so you can see at this location this is one this is two and so on minus one and minus one at the end and here it is one and here it is also two so if i try to plot all this graph zero zero zero one one zero one one so i have already named them as a b c d so you can see that a will be 0 b 0 1 1 0 and so on 1 1. so one one is it isn't it as a dark circle so that means that it is actually giving us the outcome as 1 as a class so if i try to classify this so what can be since it's a linearly classifier so we if it is linear so we have to draw a line here so our line will be actually be separating these two classes as you can see so as you can see there is multiple space here in between this okay and here as well that means there can be a multiple line possible to draw okay that will be able to separate these two classes okay so in order to do this and let me just make it slightly larger here so let's keep it from here to here okay so if i try to do this if i try to like first of all we instead of creating a line so let's say we already have a model okay so if i try to pass a model value okay let's say if i try to take a value from here let's say at this location whatever it may be the value if i try to pass this value so it will definitely give me either it is that either it is 0 or 1 that is the one thing that we will get so let's say we let's assign 0 as a let's say assigns for zero class let's say let's assign this as a this color this is your zero color and for the one it is representing by a green color so what will happen so this will give me a if i try to predict this so it will we can just show it as a uh blue color okay sky blocker similarly if you try to keep on pointing so in this way what will happen so it will try to keep on predicting and so on it will try to based on our algorithm that we have already trained or the model that we have all written it will try to fill all these values here okay which belongs to zero class okay these are all values this all values will be filled here okay so similarly if i try to like find out the value here or if i try to figure out that okay which class it belongs so it will try to give me a value which is green okay that means it belongs to one class similarly if i try to plot for every point here which is located here so this you will find out that okay this will be creating a green color here for every location in this section so it will try to provide me a green color here okay this will try to fill with the green color so let's slightly make it larger and slightly fill this up as quick as possible so you can see that okay this is going to give us a green color here okay for every such location similarly we can make it for the blue one let's make it larger here so you will find out that okay it is trying to fill up all this area with the fluid so that's what objective is that we will try to find out what will be each point representing okay so we will try to fill this up area so if i do this so it will appear that there is a certain line which will try to which is trying to separate these two things okay so separate these two uh these two classes so let's try to build this by writing a code let's try to justify this okay so i hope you got the understanding so let's try to justify this by like writing some code so let me minimize this and let's get back to our injured notebook so we will define a function which is called as save plot okay define we will call for save underscore plot which will try to first of all take the data frame and it will also try to take the file name because we want to save our file as well okay and also it will take the model so what model we are trying to plot okay so that will it will take so now let's try to keep this model here and let's try to define the uh the file name as a default some default value so let's say the file limit is equal to some callers plot plot.png okay let's say we are trying to save it as this one this is the default that we have given and this will try to save the plot that will be the agenda for this one so we will try to implement this function okay so now let's try to work on this one so the first thing that you should do is whenever you get the data you will be first of all implementing what we have to do is we will be segregating them so x comma y is equals to we will call for the prepare we already know that we prepared data is already available so it will take the data frame it will represent it will try to give us a x and y value then after that we can simply we can create an internal function here so which will create the base plot that means the representation of the x and y these coordinates that we have got okay x and x two coordinates and then the second function will do our decision function it will plot a decision function okay this is a region which is represented as i can recall so as a blue okay and green so that's what it is trying to represent here x and y model and then after that what we will do since once we have created these two like created the base plot and the plot that we have got so next after that we will define a plot directory let's say where we are going to save everything so let's call this as a lots okay and now it's better we can simply specify this as also an argument okay this will be much better so we can define this as an argument here that this is going to be a default plot okay plot directory so what will happen that after this we can simply call for os dot make directory and if this plot directory is not created so we will simply create this plot directory and it will say exists ok is equal to 2. so if it is already present so it will overwrite this and then after that we will try to find out the plot path okay so where we want to save it so let's try to clear this let's try to create it so os dot path dot join and we will simply take the model directory model or you can see sorry not modernization it's going to be plot directory and the file name okay very so this will create a total path to this uh function okay so the path to the plot that we have created and then after that we will simply save it save the plot by calling for plt dot save figure so save figure and it will take the plot path as a final path where we are showing to we are going to save the plots so this is going to be our function but we are yet to implement this create base plot first of all so what we have to do is we will simply define an internal function here so let's call this function and then function as a create plot this will be great base plot that will be taking the data frame and it will be creating the plot so let's call this let's pass this as of now okay and then after that we will be creating one more you can say plot decision regions and it will take the x and y that is features and labels and then the model that we are going to use it for plotting it this will let's call it as a pass but this is the function that we are need to implement these things so we will be implementing this internal function in the next session until then keep on exploring keep on learning and thank you for entering this one so see you in the next session thank you so hello ladies and gentlemen welcome to another session of introduction to deep learning so let's continue from where we left off in the last session so in the last session we were actually creating the save plot function and we have just created the skeleton of it internal functions are still left to be built okay so let's try to work on that in this particular session and let's do some observation about whatever model that we have created till now so we will be working on that we will try to explore and like we will try to justify them okay so why the certain results are coming in so let's try to observe it here so if i just open up here my notebooks as you can see this is what we have done in the last session so let's try to first of all open these or just create these plots okay so first thing is that let's try to define so we have got this internal function create base plot so it will be actually creating a scatter plot out of the uh data that is given into this to that to this particular uh you can say data frame okay so df data frame itself has a plot method okay so we will try to utilize that here in this case and we will specify the kind here the kind is going to be scatter plot okay and we can also specify the x values x is going to be x one since that is the you can say your uh that is the first column okay and then y is equals to here it is going to be actually x1 x2 okay so this is going to be your x2 and then the whatever is the c value that is the color value where c is equal to it is going to be y so that's what we have till now and then after that we have to also specify the diameter of the these circles so it is going to be 100 as of now we are just keeping it making it large and color map we are choosing here is as a cool warm okay so we are just keeping this as a color map now for that we can simply plot the horizontal axis and the vertical axis so a x h line okay and then we can specify that where we want to so we can just we are trying to draw we can say y is equals to 0 that is nothing but it is going to be a horizontal line okay so color is equal to we are specifying here as color is black okay and then we have the line style line style is equal to it's going to be dash okay iphone 5s and we have to specifying the line width as well so line width yes we are keeping it to the minimum so we are just keeping it as one so this is about the your x axis that we are trying to build or horizontal line now let's try to create a vertical line as well so in the vertical line we are just going to check this x is equal to zero and also we will be calling it as v line a x v line but this will create the horizontal and vertical axis for us and then we will simply plot these figures so pf bigger is equals to plt dot and gcf okay let's get current off pick their current figure and then after that we have to simply set the size of exhaust set okay and size size inches okay so it is going to be 10 comma 8 we are trying to keep it okay 10 comma 8 is the size that we are keeping it now after that we have to since this will create just a scatter plot but we need to also plot the decision boundary or design reasons so we let's try to create that so let's take some colored list okay let's double of colors so it's not necessary that we can take it we can take it any size okay but we can just like necessary using just two only so we can just use light green here and okay so these are going to be the colors and c map is equals to actually listed colors we already use and in this one we need to follow these colors okay colors that we have defined here okay now after that we have to simply call for x is equals to x dot values so that we can get the array values from them okay these are the values that we are going to so get as we just call this as an array and then after that we will be satisfying the x1 is equal to which is equal to x and it will take all the rows but 0th column and similarly we will have x2 as well which is going to be all the rows but first column so we'll call it as x2 now for that as you can recall from the pick so we have to create get the we have to extend the area so that we can plot it very well and it should look like that so as you can see here also i have extended it from one to two actually and also here so that it will create a graph like this so we need to do that for that we have to just get the minimum of x 1 and maximum of it and then we will in the minimum we will just subtract by 1 and in the maximum we will just add it by one so x 1 underscore min and x 1 underscore max okay so how it is going to be so let's call it as so x1 dot min then subtract it by 1 and then x1 dot max and then what you have to do add one to it similarly we will do it for your x2 as well okay so replace it with this with x2 let's do max min and x2 max so this will get us the max and min value now we have to find out the mesh

Original Description

https://github.com/c17hawke/PERCEPTRON-implementation Part 1 : https://www.youtube.com/watch?v=lRcNvNduD2M 00:00 Implementation Of Perceptron Part 2 10:56 Implementation Of Perceptron Part 3 19:10 Implementation Of Perceptron Part 4 28:33 Implementation Of Perceptron Part 5 38:08 Implementation Of Perceptron Part 6 47:24 Implementation Of Perceptron Part 7 ⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I've been using Kite for a few months and I love it! https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=krishnaik&utm_content=description-only -------------------------------------------------------------------------------------------------------------------------- Subscribe my vlogging channel https://www.youtube.com/channel/UCjWY5hREA6FFYrthD0rZNIw Please donate if you want to support the channel through GPay UPID, Gpay: krishnaik06@okicici Telegram link: https://t.me/joinchat/N77M7xRvYUd403DgfE4TWw Please join as a member in my channel to get additional benefits like materials in Data Science, live streaming for Members and many more https://www.youtube.com/channel/UCNU_lfiiWBdtULKOw6X0Dig/join ----------------------------------------------------------------------------------------------------------------------- Connect with me here: Twitter: https://twitter.com/Krishnaik06 Facebook: https://www.facebook.com/krishnaik06 instagram: https://www.instagram.com/krishnaik06 -----------------------------------------------------------------------------------------------------------------------
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Krish Naik · Krish Naik · 0 of 60

← Previous Next →
1 Natural Language Processing|Stemming
Natural Language Processing|Stemming
Krish Naik
2 Natural Language Processing|BagofWords
Natural Language Processing|BagofWords
Krish Naik
3 Gaussian distribution or Normal Distribution in statisctics
Gaussian distribution or Normal Distribution in statisctics
Krish Naik
4 Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Krish Naik
5 Log Normal Distribution in Statistics
Log Normal Distribution in Statistics
Krish Naik
6 Covariance in Statistics
Covariance in Statistics
Krish Naik
7 Confusion matrix, Precision, Recall| Data Science Interview questions
Confusion matrix, Precision, Recall| Data Science Interview questions
Krish Naik
8 Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Krish Naik
9 Implementing a Spam classifier in python| Natural Language Processing
Implementing a Spam classifier in python| Natural Language Processing
Krish Naik
10 Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Krish Naik
11 Face Recognition using open CV and VGG 16 Transfer Learning
Face Recognition using open CV and VGG 16 Transfer Learning
Krish Naik
12 Pedestrian Detection using OpenCV from Videos
Pedestrian Detection using OpenCV from Videos
Krish Naik
13 Face and Eye Detection from Videos using HAAR Cascade Classifier
Face and Eye Detection from Videos using HAAR Cascade Classifier
Krish Naik
14 Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Krish Naik
15 OpenCV Installation | OpenCV tutorial
OpenCV Installation | OpenCV tutorial
Krish Naik
16 Face and Eye Detection from Images using HAAR Cascade Classifier
Face and Eye Detection from Images using HAAR Cascade Classifier
Krish Naik
17 Car Detection using HAAR Cascade and Opencv from Videos.
Car Detection using HAAR Cascade and Opencv from Videos.
Krish Naik
18 Using OpenFace for Face recognition in Keras
Using OpenFace for Face recognition in Keras
Krish Naik
19 OpenPose Tutorial with Tensorflow
OpenPose Tutorial with Tensorflow
Krish Naik
20 Multiple Linear Regression using python and sklearn
Multiple Linear Regression using python and sklearn
Krish Naik
21 Dimensional Reduction| Principal Component Analysis
Dimensional Reduction| Principal Component Analysis
Krish Naik
22 Movie Recommender System using Python
Movie Recommender System using Python
Krish Naik
23 TPR,FPR,FNR,TNR, Confusion Matrix
TPR,FPR,FNR,TNR, Confusion Matrix
Krish Naik
24 Precision, Recall and F1-Score
Precision, Recall and F1-Score
Krish Naik
25 Artificial Neural Network for Customer's Exit Prediction from Bank
Artificial Neural Network for Customer's Exit Prediction from Bank
Krish Naik
26 GridSearchCV- Select the best hyperparameter for any Classification Model
GridSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
27 RandomizedSearchCV- Select the best hyperparameter for any Classification Model
RandomizedSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
28 K Nearest Neighbor classification with Intuition and practical solution
K Nearest Neighbor classification with Intuition and practical solution
Krish Naik
29 K Means Clustering Intuition
K Means Clustering Intuition
Krish Naik
30 Create custom Alexa Skill- Lambda function- Part2
Create custom Alexa Skill- Lambda function- Part2
Krish Naik
31 Hierarchical Clustering intuition
Hierarchical Clustering intuition
Krish Naik
32 Implement Transfer Learning with a generic Code Template
Implement Transfer Learning with a generic Code Template
Krish Naik
33 Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Krish Naik
34 Unlock Your Application With Your Face using OpenCV
Unlock Your Application With Your Face using OpenCV
Krish Naik
35 Draw rectangle from webcam and sketch process it on a live feed
Draw rectangle from webcam and sketch process it on a live feed
Krish Naik
36 Complete Life Cycle of a Data Science Project
Complete Life Cycle of a Data Science Project
Krish Naik
37 How we can apply Machine Learning in Finance
How we can apply Machine Learning in Finance
Krish Naik
38 Deep Learning in Medical Science
Deep Learning in Medical Science
Krish Naik
39 How to switch your career to Data Science.
How to switch your career to Data Science.
Krish Naik
40 Linear Regression Mathematical Intuition
Linear Regression Mathematical Intuition
Krish Naik
41 Handle Categorical features using Python
Handle Categorical features using Python
Krish Naik
42 Machine Learning Algorithm- Which one to choose for your Problem?
Machine Learning Algorithm- Which one to choose for your Problem?
Krish Naik
43 DBSCAN Clustering Easily Explained with Implementation
DBSCAN Clustering Easily Explained with Implementation
Krish Naik
44 Curse of Dimensionality Easily explained| Machine Learning
Curse of Dimensionality Easily explained| Machine Learning
Krish Naik
45 Feature Selection Techniques Easily Explained | Machine Learning
Feature Selection Techniques Easily Explained | Machine Learning
Krish Naik
46 Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Krish Naik
47 Cross Validation using sklearn and python | Machine Learning
Cross Validation using sklearn and python | Machine Learning
Krish Naik
48 Handling Missing Data Easily Explained| Machine Learning
Handling Missing Data Easily Explained| Machine Learning
Krish Naik
49 Deploy Machine Learning Model using Flask
Deploy Machine Learning Model using Flask
Krish Naik
50 Deployment of Deep Learning Model using Flask
Deployment of Deep Learning Model using Flask
Krish Naik
51 How to Visualize Multiple Linear Regression in python
How to Visualize Multiple Linear Regression in python
Krish Naik
52 K Nearest Neighbour Easily Explained with Implementation
K Nearest Neighbour Easily Explained with Implementation
Krish Naik
53 Predicting Heart Disease using Machine Learning
Predicting Heart Disease using Machine Learning
Krish Naik
54 Predicting Lungs Disease using Deep Learning
Predicting Lungs Disease using Deep Learning
Krish Naik
55 Stock Sentiment Analysis using News Headlines
Stock Sentiment Analysis using News Headlines
Krish Naik
56 Random Forest(Bootstrap Aggregation) Easily Explained
Random Forest(Bootstrap Aggregation) Easily Explained
Krish Naik
57 Voting Classifier(Hard Voting and Soft Voting Classifier)
Voting Classifier(Hard Voting and Soft Voting Classifier)
Krish Naik
58 Credit Card Fraud Detection using Machine Learning from Kaggle
Credit Card Fraud Detection using Machine Learning from Kaggle
Krish Naik
59 Hyperparameter Optimization for Xgboost
Hyperparameter Optimization for Xgboost
Krish Naik
60 Tutorial 45-Handling imbalanced Dataset  using python- Part 1
Tutorial 45-Handling imbalanced Dataset using python- Part 1
Krish Naik

Related Reads

Chapters (6)

Implementation Of Perceptron Part 2
10:56 Implementation Of Perceptron Part 3
19:10 Implementation Of Perceptron Part 4
28:33 Implementation Of Perceptron Part 5
38:08 Implementation Of Perceptron Part 6
47:24 Implementation Of Perceptron Part 7
Up next
Part 2 | MLOps On GitHub | Deploy and Automate ML Workflow |Using GitHub Actions and CML for CI & CD
Abonia Sojasingarayar
Watch →