‫Hello.
‫in this video
‫We will create a very simple single Perceptron model to classify flower species depending on their petal
‫length and petal width.
‫We will be using a sklearn to create this single Perceptron model.
‫In the later part of this course, we will use Keras and TensorFlow to create multi-level Perceptron
‫models, Sklearn is a very popular machine learning library for Python.
‫It is the go to library to create regression classification decision trees or SVM models, we
‫have separate lectures on all this machine learning models.
‫So if you are interested in learning any one of these, you can go ahead and check our courses
‫on regression classification, decision trees and SVM.
‫So let's start first.
‫We will import numpy and pandas.
‫Now, if you have installed Anaconda.
‫There is no need to install a sklearn separately.
‫You just have to import sklearn.
‫But in case you are facing any error in importing a sklearn, you can install it using pip or conda
‫install.
‫So just run this
‫Command, Pip, install sklearn in your command prompt.
‫You can open your command prompt by pressing Windows R and write cmd in the run command and hit
‫enter there in the command prompt
‫You can write this code and execute it to install sklearn.
‫Or else you can directly run this code
‫in Jupyter notebook and this will also install sklearn for you.
‫So if you have installed a sklearn.
‫First, we will load iris data.
‫There are various csv files out there.
‫You can also import those csv file to load this data.
‫But as Skillern have some predefined datasets and we will load our iris data from there.
‫So just write from sklearn.datasets.
‫Import load
‫Underscore iris.
‫And then we are saving our iris data into this variable iris.
‫So just run this.
‫Now, let's take a look at our data, you can see there are four columns.
‫the columns are sepal length, sepal width, petal length and petal width, as i have said earlier this is a
‫data of different types of flowers.
‫There are three different types of flowers, Setosa.
‫versicolour, virginica
‫And for each of these flowers, we have their sepal length, sepal width
‫petal length and petal width
‫So here we have this four variables and that category is stored in Iris.Target.
‫So in our example, we want to create a Perceptron model which would identify whether the flower is
‫Setosa or not using petal width and petal length as the independent variables.
‫If you want, you can take all the four variables.
‫But for our example, we are just taking these two variables to predict where the flower is setosa or not.
‫So we want our independent variable to be this petal length and petal width.
‫We will store this information in another variable which we are calling X.
‫We are defining X as iris.data
‫And here we just want the third and fourth column.
‫That's why we have written 2,3.
‫If you remember, the indexing starts with zero.
‫So the last two columns are two and three.
‫Just run this.
‫So our X variable is now ready.
‫Now let's just look at the target variable.
‫Here you can see we have different categories, zero one and two zero stand for Setosa.
‫One stands for versicolor and two stands for Virginica.
‫Now, if you have some machine learning knowledge, you may know that to create classification more
‫than what y variable should be in the form of zero and one.
‫So ideally we want one in all these records where the flower is serosa zero in all this records
‫where the flower is versicolor or virginica.
‫So let's just convert this target variable using some basic operations first.
‫Let's convert this target variable in the form of true and false.
‫We want true where the flower is setosa.
‫That is the value of target is zero.
‫And we want false where the flower is Virginica or versicolor.
‫That is the numerical value is one or two.
‫So let's just run this
‫Command.
‫We are just checking whether the target is equal to zero or not.
‫If the target is zero, we will get true.
‫And if the target is not equal to zero, we will get false.
‫Let's run this.
‫Now let's look at our y variable.
‫You can see we have converted zeros to true and one or two to false.
‫Now, in the next step, we want to convert this true and falls to one zero.
‫We want one in place of true.
‫And we want zero in place of false.
‫So we will just use as type method and we will convert it to int.
‫And so just run and look at our y variable.
‫So as you can see now, our y variable is an array with one and zero.
‫One means setosa and zero means not setosa.
‫Now let's look at our X variable before creating a Perceptron model.
‫You can see our X variable is a two dimensional array with petal length and petal width.
‫No our x and y variables are ready.
‫We just have to create our Perceptron model and train that model using this X and Y variables.
‫We can create a single Perceptron model using sklearn, but to create MLP on multi level perceptron
‫model, we have to use Keras and TensorFlow
‫We will be looking at mlp in the later part of this course.
‫But now we will create this perceptron model using sklearn only.
‫Now first we need to import Perceptron from sklearn.linear model
‫Then let's just import it.
‫And you can also look at the documentation using this link.
‫This Jupyter notebook is also shared in the resources section of this video.
‫So you can download it and run it if you want to practice.
‫So this is the official document of Perceptron in sklearn.
‫You can look at all the parameters which we can give, first one is the penalty.
‫If you remember, in linear regression, there are regularization terms, L1, L2 also known as
‫LASO and Ridge.
‫You can also give alpha, that is the variable we use for regularization.
‫Then there are different other hyper parameters.
‫We will just stick to basic default hyper parameters.
‫But you can look at it if you want.
‫These are just some very basic hyper parameters that we get with most of the machine learning algorithms.
‫Now, as with any other sklearn machine learning model, we first have to create an object of that algorithm,
‫then we have to fit our X and Y variables into that object and then we can use that object to predict
‫the future values of y.
‫So first, let's create an object that is Perceptron classifier.
‫We are naming our variable is per_clf and then we are using Perceptron that we have
‫just imported and we are giving only one hyper parameter.
‫That is a random state equal to 42.
‫This is basically to reproduce the same result whenever we run this model.
‫So if you give random state, you will always going to get the same result.
‫Giving this hyper parameter is not mandatory.
‫You can skip that also if you want.
‫And in the next line, we are fitting our X and Y variables into this object.
‫So let's run this.
‫So our model is trained
‫You can get the values of default hyperparameter
‫here if you want.
‫Now, let's predict the value using our classifier object.
‫We can just use dot predict method.
‫So this is our object and we are using dot predict and we are giving our independent and variable,
‫which is X as an input.
‫Let's do that.
‫You can see this are the predicted values.
‫We can compare this predicted values with the actual values.
‫There is no need to manually do that.
‫That is an another function.
‫That is accuracy score available in sklearn, which will give us the accuracy of our prediction. Accuracy
‫score varies between zero and one. Zero means zero percent accuracy, which means all the predictions are
‫wrong.
‫And one means hundred percent accuracy, which mean all the predictions are right.
‫So let's just first import the accuracy score from sklearn metrics
‫And then we are going to use accuracy score.
‫And here there are two arguments.
‫First, we have to provide the actual values.
‫And in the next argument, we have to provide the predicted values.
‫So our actual values are stored in just y variable
‫And the predicted values
‫We have restored in y_pred
‫So let's get the accuracy score of our predictions.
‫So the accuracy here is one that is hundred percent accuracy. Our perceptron
‫was able to identify the speci of the floert with 100 percent accuracy.
‫Now, this is a very simple model.
‫Usually we don't use Perceptron for any regression or classification tasks.
‫We usually go for machine learning techniques where the data is not following
‫any complex pattern and we go for nlp
‫where
‫The data is following a very complex pattern.
‫Usually you will never find yourself using Perceptron in business settings.
‫But this is just an introduction.
‫And we want you to give just some basic idea about running Perceptron models using sklearn.
‫Now, after training your model, you will get these two attributes.
‫That is the coefficient.
‫And the intercepts.
‫Basically our Perceptron is dividing this space using a linear regression line.
‫So this is the coefficient.
‫And this is the intercept of that line.
‫So your equation of that line will be four minus one point four times petal length minus two point
‫two times.
‫petal width.
‫So if you want to create that line, you can use coefficient and intercept values to do that, and you
‫can also see the impact of our different variables on the Y variable.
‫So this coefficient is giving you the impact of each of those variables.
‫This is the impact of petal length and this the impact of petal width.
‫That's all for this lecture.
‫Next, we will look at tensor flow and Keras to create our MLP model.
‫Thank you.