Classical Machine Learning Classification
Transcript
Carlos Lizárraga | UA DSI DataLab: Okay, let's start today, session on classical machine learning. Carlos Lizárraga | UA DSI DataLab: Today, we're going to talk about classification algorithms didn't mean start sharing my screen on different topic. Carlos Lizárraga | UA DSI DataLab: So this is the starting page for our data lab workshops. That, you are already familiar. Carlos Lizárraga | UA DSI DataLab: And here you will find today's workshop. We're doing the classical machine learning. Carlos Lizárraga | UA DSI DataLab: And if you go there, I'm gonna posted Carlos Lizárraga | UA DSI DataLab: post a link in a while. So today we're talking about classification.
Carlos Lizárraga | UA DSI DataLab: And these are going to be the materials for today. So I'm going to share it in the chat Carlos Lizárraga | UA DSI DataLab: for you. Carlos Lizárraga | UA DSI DataLab: Yes, yes. Let me reshare again. Carlos Lizárraga | UA DSI DataLab: Okay, okay, share everything. Carlos Lizárraga | UA DSI DataLab: Okay. Now you should be seeing the the wiki. Carlos Lizárraga | UA DSI DataLab: Yes, please let me know.
Carlos Lizárraga | UA DSI DataLab: Okay, so I have shared Carlos Lizárraga | UA DSI DataLab: in the slack, I mean in the chat, the link for today's note. Carlos Lizárraga | UA DSI DataLab: And so last week we talked about supervised machine learning regression algorithms. Carlos Lizárraga | UA DSI DataLab: And remember that regression is when you build a model, and in base in that model, then you can Carlos Lizárraga | UA DSI DataLab: look for points that are not included, and Carlos Lizárraga | UA DSI DataLab: the value of that new point is going to be returned by the model.
Carlos Lizárraga | UA DSI DataLab: Today, we're going to be looking at classification. And Carlos Lizárraga | UA DSI DataLab: so I'm going to share the notebook. Carlos Lizárraga | UA DSI DataLab: There are many algorithms there for specific classification. We have the Carlos Lizárraga | UA DSI DataLab: logistic regression. Let me see. You go. Sorry classification. Yes.
Carlos Lizárraga | UA DSI DataLab: So this is the the notes for today. So there are many Carlos Lizárraga | UA DSI DataLab: algorithms there for doing classification. And I'm going to share a notebook. Carlos Lizárraga | UA DSI DataLab: So in in doing classification, we need to train our models. So we will have some initial data. Carlos Lizárraga | UA DSI DataLab: and then, after the data is already ready for the model, then we will split Carlos Lizárraga | UA DSI DataLab: the data into 2 parts, the train and and testing to train the model and test the model. And then, basically, is this Carlos Lizárraga | UA DSI DataLab: this figure over here? So we have our data, and we have to split our data. Carlos Lizárraga | UA DSI DataLab: Usually, it's this training data is if you have plenty of data, it's about 80% of your data, and then you leave 20% Carlos Lizárraga | UA DSI DataLab: for for testing your model. And then once you train your model, then you test it, and then you have to have a list of
Carlos Lizárraga | UA DSI DataLab: metrics to see what is the performance of your model. So that's that's the idea. Carlos Lizárraga | UA DSI DataLab: I'm going to go all the way Carlos Lizárraga | UA DSI DataLab: down, and I'm going to share this notebook. Carlos Lizárraga | UA DSI DataLab: So hope. Carlos Lizárraga | UA DSI DataLab: And we're going to follow the discussion from that notebook. Carlos Lizárraga | UA DSI DataLab: I'm going to post it in the chat again, and then the notebook for today peace as follows. Carlos Lizárraga | UA DSI DataLab: then this notebook will open in a github, and then you can select Carlos Lizárraga | UA DSI DataLab: the notebook and run it in your Carlos Lizárraga | UA DSI DataLab: Google collab account. You're using your Gmail account.
Carlos Lizárraga | UA DSI DataLab: So we're going to do that Carlos Lizárraga | UA DSI DataLab: and follow the discussion. And again, please let me know if I'm talking too fast, or if you have any questions, or you want to Carlos Lizárraga | UA DSI DataLab: stop me and and ask something that is not clear. Carlos Lizárraga | UA DSI DataLab: All right. So we're going to do the classification using the Carlos Lizárraga | UA DSI DataLab: Palmer's penguins data set. And that data set is one of the known data sets that contains Carlos Lizárraga | UA DSI DataLab: it contains data from 3 types of species of penguins that live on 3 different islands in the Antarctica. Carlos Lizárraga | UA DSI DataLab: So so we're going to work with that today, I'm going to open this notebook in collapse. Carlos Lizárraga | UA DSI DataLab: So I'll be running it.
Carlos Lizárraga | UA DSI DataLab: So now I'm in Google Collab. Carlos Lizárraga | UA DSI DataLab: This is the executable notebook and Carlos Lizárraga | UA DSI DataLab: and we're going to be using again scikit-learn. We we will be using scikit-learn library for all the classical machine learning tasks Carlos Lizárraga | UA DSI DataLab: that we're going to be doing. Carlos Lizárraga | UA DSI DataLab: And so the 1st cell Carlos Lizárraga | UA DSI DataLab: cold cell is always contains the Carlos Lizárraga | UA DSI DataLab: all the libraries will be using.
Carlos Lizárraga | UA DSI DataLab: So we are going to import the basic libraries numpy, and have the alias Mp. Carlos Lizárraga | UA DSI DataLab: Pandas, and alias Pd. Carlos Lizárraga | UA DSI DataLab: And the stats model. Api, S. Sm. Carlos Lizárraga | UA DSI DataLab: and then we will have to visualize some of the our data. So we're going to use map, load, leap by plot Carlos Lizárraga | UA DSI DataLab: and also Seabourne. I'm not going to use plotly express today. Carlos Lizárraga | UA DSI DataLab: and then we will be loading different
Carlos Lizárraga | UA DSI DataLab: classification libraries from scikit-learn, so we will use knaighbors classifier, a decision tree classifier and also a support vector classifier from support support vector machines. Carlos Lizárraga | UA DSI DataLab: And then we will use label encoder. Carlos Lizárraga | UA DSI DataLab: This is when you're going to do machine learning all your variables that you're going to be training your model on Carlos Lizárraga | UA DSI DataLab: must be numeric. So if your data set has categorical variables like, in this case, we have 3 but categorical variables. Carlos Lizárraga | UA DSI DataLab: and we are going to convert those categorical variables variables into numeric variables. Carlos Lizárraga | UA DSI DataLab: And then we're going to. All the numeric variables are going to be normalized just a second
Carlos Lizárraga | UA DSI DataLab: myself. Carlos Lizárraga | UA DSI DataLab: Okay? Sorry. Carlos Lizárraga | UA DSI DataLab: Okay. So I was talking when you're training a model. All your variables must be numeric. Carlos Lizárraga | UA DSI DataLab: and in this case we have several categorical variables. In the case of the penguins, the categorical variables are Carlos Lizárraga | UA DSI DataLab: the species. The island where they live, and also the gender.
Carlos Lizárraga | UA DSI DataLab: which is male and female. So we need to convert all those categorical variables into numeric, and we will be using the label encoder, and then all the numerical variables are going to be normalized, meaning that we will apply a transformation to convert Carlos Lizárraga | UA DSI DataLab: all the numerical variables into a normal distribution. Gaussian distribution with the Carlos Lizárraga | UA DSI DataLab: mean equals 0 and standard deviation equals one. So we are normalizing the the data. So we don't have Carlos Lizárraga | UA DSI DataLab: data that is Carlos Lizárraga | UA DSI DataLab: very high has very large values. And in in variables that have very low small values. So we're going to normalize them. Carlos Lizárraga | UA DSI DataLab: We're going to use the train test split function to split our data.
Carlos Lizárraga | UA DSI DataLab: And this is the standard scalar that's that's the one that converts into a Carlos Lizárraga | UA DSI DataLab: standard standard normalize probability distribution. Carlos Lizárraga | UA DSI DataLab: and then we will use several metrics like the confusion matrix, the accuracy score the Roc cure Carlos Lizárraga | UA DSI DataLab: and some other ones. We will see him in a while. Carlos Lizárraga | UA DSI DataLab: Alright. So let's start.
Carlos Lizárraga | UA DSI DataLab: Okay. So we're going to read this file name that I have Carlos Lizárraga | UA DSI DataLab: in Github, which is the penguins data set. I'm going to be using pandas. The read Csv function to read that file that is on Github. Carlos Lizárraga | UA DSI DataLab: And I'm going to define a data frame which is called dft or distribution data frame. Carlos Lizárraga | UA DSI DataLab: So I'm going to let me run first.st Load all the libraries. Carlos Lizárraga | UA DSI DataLab: Yes, runs, and loading all the libraries through the memory or working memory.
Carlos Lizárraga | UA DSI DataLab: And when it's done, then I can read Carlos Lizárraga | UA DSI DataLab: the data set from Github using pandas, using the function. Readcsb, okay, so, okay, still running. Carlos Lizárraga | UA DSI DataLab: So wait until we have the green check mark. Okay, so it's done. Carlos Lizárraga | UA DSI DataLab: And then we're going to read the data set. Carlos Lizárraga | UA DSI DataLab: And this is the the 1st lines of the data set.
Carlos Lizárraga | UA DSI DataLab: So these are the variables species. We have 3 types of species for the penguins. We have islands. We have 3 islands also, and then we have physical Carlos Lizárraga | UA DSI DataLab: characteristics of each species. So we have Coolman length Carlos Lizárraga | UA DSI DataLab: for every penguin. The Coolman depth, which is the peak. Carlos Lizárraga | UA DSI DataLab: and also the flipper, length, body, mass, and also let me. Carlos Lizárraga | UA DSI DataLab: We have the the gender, which is male or female, and we have some missing numbers, not a number NAN, Carlos Lizárraga | UA DSI DataLab: so we have some empty Carlos Lizárraga | UA DSI DataLab: values. We will have to use pandas to
Carlos Lizárraga | UA DSI DataLab: preprocess our data to make it, to make it clean. Carlos Lizárraga | UA DSI DataLab: By the way, Google, Collab offers you Carlos Lizárraga | UA DSI DataLab: some recommended plots and also generate codes to analyze your data set. Carlos Lizárraga | UA DSI DataLab: But I'm going to go ahead and and not accept that offer. Carlos Lizárraga | UA DSI DataLab: Okay, so we're going to do some data cleaning and manipulation, using the Pandas library. Carlos Lizárraga | UA DSI DataLab: So first, st we can use the function info, to see what is the distribution, the original distribution of the data frame. Carlos Lizárraga | UA DSI DataLab: And we have 344 rows. And remember, python counts from 0. So we have rows from 0 Carlos Lizárraga | UA DSI DataLab: 2, 3, 43, Carlos Lizárraga | UA DSI DataLab: and then we have 7 columns, the species, the island, the cooling length, the cooling depth, the flipper length.
Carlos Lizárraga | UA DSI DataLab: body, mass, and gender sex, and we have a different number of values. Carlos Lizárraga | UA DSI DataLab: We have some rows that are complete Carlos Lizárraga | UA DSI DataLab: and some rows that are missing some value. So we have from 344 Carlos Lizárraga | UA DSI DataLab: to 334. So we have some missing values. Okay, so we have to. We can use. The function is null Carlos Lizárraga | UA DSI DataLab: to tell me all the rows.
Carlos Lizárraga | UA DSI DataLab: the how many rows are empty. Carlos Lizárraga | UA DSI DataLab: So we have some missing values in cooling length, the cooling depth, the flipper length. Carlos Lizárraga | UA DSI DataLab: body, mass, and sex. Carlos Lizárraga | UA DSI DataLab: So we can drop all the rows that have empty values using the function drop. Na. Carlos Lizárraga | UA DSI DataLab: so I'm going to drop. Carlos Lizárraga | UA DSI DataLab: And then I inquire again, what's the status of my data set? Carlos Lizárraga | UA DSI DataLab: And this data set now has 334. So every every every column has 334 rows.
Carlos Lizárraga | UA DSI DataLab: and then, we're going to encode the categorical variables. As I was mentioning, using the function label encoder. Carlos Lizárraga | UA DSI DataLab: So we have 3 categorical variables, species, island and sex. Carlos Lizárraga | UA DSI DataLab: And then we can use the function value counts to count how many Carlos Lizárraga | UA DSI DataLab: different values we have of species. How many different values we have from island, and how many values we have for gender. Carlos Lizárraga | UA DSI DataLab: So I execute this cell. Carlos Lizárraga | UA DSI DataLab: And then we have Carlos Lizárraga | UA DSI DataLab: 146 species from Adelie, 1, 120 from Gentu, and 68 from Shinstra. Carlos Lizárraga | UA DSI DataLab: and we have from the different islands. We have 164 penguins from Bisco.
Carlos Lizárraga | UA DSI DataLab: 123 from Dream, and 47 from the Torgenson Island Carlos Lizárraga | UA DSI DataLab: and gender. We have 168 males, 165 females. Carlos Lizárraga | UA DSI DataLab: and there is a a row that has a dot Carlos Lizárraga | UA DSI DataLab: in the gender. So we need to Carlos Lizárraga | UA DSI DataLab: eliminate this if we don't have any more information. So I need to apply a filter to eliminate Carlos Lizárraga | UA DSI DataLab: the rows from the column 6. Carlos Lizárraga | UA DSI DataLab: So I'm going to keep only the rows, which are different. Carlos Lizárraga | UA DSI DataLab: Then dots, and then this will be my new data frame. Df, and so I will execute it.
Carlos Lizárraga | UA DSI DataLab: And now I have 168 males, 165 females and nothing else. Carlos Lizárraga | UA DSI DataLab: Okay, so now we can do the info function to see what is the status of my data data set or data frame. And we have 333 Carlos Lizárraga | UA DSI DataLab: rows for each column. So now we have a clean data set. Carlos Lizárraga | UA DSI DataLab: and we have used pandas to to preprocess and clean. And Carlos Lizárraga | UA DSI DataLab: our data data set and have a clean data frame. So we can start Carlos Lizárraga | UA DSI DataLab: alright. The next step is to encode all the categorical variables. So I'm going to transform Carlos Lizárraga | UA DSI DataLab: species, island and sex and declare a new variable called species encoded island encoded and sex encoded. So I do that.
Carlos Lizárraga | UA DSI DataLab: And now, I print the the 1st 5 lines of my data frame. Carlos Lizárraga | UA DSI DataLab: So these are the variable species, island, coolman, length, cooling, depth, flipper, length, body, mass, sex, and then the species encoded. So for the species encoded. Since I have 3 species, the the values will be 0 1 or 2. Carlos Lizárraga | UA DSI DataLab: We have 3 islands. The values will be 0 1 or 2. And we have 2 genders. Carlos Lizárraga | UA DSI DataLab: 0 or one.
Carlos Lizárraga | UA DSI DataLab: Okay, okay. Now, I have a numeric data frame. Carlos Lizárraga | UA DSI DataLab: Now, it would be good to check if we have any outlier values. Carlos Lizárraga | UA DSI DataLab: Can someone tell me, what's an outlier? Carlos Lizárraga | UA DSI DataLab: Are you familiar with that? Carlos Lizárraga | UA DSI DataLab: Yes, no.
Carlos Lizárraga | UA DSI DataLab: yes, those are extreme values. That Carlos Lizárraga | UA DSI DataLab: yes, that they are completely off. So sometimes Carlos Lizárraga | UA DSI DataLab: these outliers. Maybe just some error, or probably Carlos Lizárraga | UA DSI DataLab: it has a meaning. But then you have to find out what is what is the reason that you have Carlos Lizárraga | UA DSI DataLab: all these outliers good. Carlos Lizárraga | UA DSI DataLab: So Toki proposed a method for identifying outliers and an outlier by its definition, is
Carlos Lizárraga | UA DSI DataLab: you have all your distribute distributed data. Carlos Lizárraga | UA DSI DataLab: And you, the you separate your data by by quant quartiles. And then this will be the 1st quartile, which is 25% of your data. Carlos Lizárraga | UA DSI DataLab: And this is another quartile. So you have here half of the your data. Carlos Lizárraga | UA DSI DataLab: Lie in this in this part. Carlos Lizárraga | UA DSI DataLab: and then another 25% of your data rely here. So let's say.
Carlos Lizárraga | UA DSI DataLab: okay, so and then they defined these fences. Carlos Lizárraga | UA DSI DataLab: So this will be the an inner fence. And then we can. This is like 1.5, the interquartile range. Carlos Lizárraga | UA DSI DataLab: So this is the 1st quartile. q 1 minus 1.5 interquartile difference is going to be this boundary. Carlos Lizárraga | UA DSI DataLab: and then you can define an external fence Carlos Lizárraga | UA DSI DataLab: going. q, 1 minus 3 times the interquartile range. So okay, so that's the Carlos Lizárraga | UA DSI DataLab: the when you define it. This is the code for defining the all the different fences. Carlos Lizárraga | UA DSI DataLab: and you have the interference. The inner fence is just the q 1 minus 1.5 U interquartile range, or above Carlos Lizárraga | UA DSI DataLab: the the 3rd quartile plus
Carlos Lizárraga | UA DSI DataLab: then the plus 1.5, the interquartile range and the same thing 3 times that interquartile range. Carlos Lizárraga | UA DSI DataLab: So we we see we have any values. First, st we define the expenses, and then we Carlos Lizárraga | UA DSI DataLab: we read the file and see and classify. If we have outliers there. Carlos Lizárraga | UA DSI DataLab: There are no outliers in the Coolman Glen. Carlos Lizárraga | UA DSI DataLab: We check on the cooling depth. Also we don't find any outliers, and then on the flipper link. We don't find any outliers Carlos Lizárraga | UA DSI DataLab: and also on the body mass also there. There isn't any outlier, so we are. Carlos Lizárraga | UA DSI DataLab: Why? Carlos Lizárraga | UA DSI DataLab: And then, we can use matplotly pipeline and seaborne to see how your data is distributed. Carlos Lizárraga | UA DSI DataLab: So we have this data frame.
Carlos Lizárraga | UA DSI DataLab: So this is the data frame we're doing, and then we can use Seaborn his plot Carlos Lizárraga | UA DSI DataLab: to create some histograms for the cooling length and cooling depth. Carlos Lizárraga | UA DSI DataLab: I'm defining 25 bins. And also I'm going to plot the kernel density estimate. Carlos Lizárraga | UA DSI DataLab: and I'm going to make 2 plots in the same row. So I'm using the function subplots Carlos Lizárraga | UA DSI DataLab: and and these are the the first, st the axis for the 1st plot and the axis. For the second plot I will have a figure size of 12 units on the horizontal, and 8 units on the vertical, and I'm going to have 2 columns in in the in the plots Carlos Lizárraga | UA DSI DataLab: in a single row. So you will find this is the result and make it smaller. Okay.
Carlos Lizárraga | UA DSI DataLab: I do the same thing for the flipper length and the body mass. This is the distribution of the values. Carlos Lizárraga | UA DSI DataLab: and then I can use. I can take the advantage of the species and separate the the data by species Carlos Lizárraga | UA DSI DataLab: and do the same. So now I have Carlos Lizárraga | UA DSI DataLab: a distribution for each species of penguin for the cooling length the cool man depth Carlos Lizárraga | UA DSI DataLab: the same as the flipper length, and the body mass. Carlos Lizárraga | UA DSI DataLab: So I'm just using seaborne his plot. Carlos Lizárraga | UA DSI DataLab: and then I can do bar plots. Carlos Lizárraga | UA DSI DataLab: seaborne park plots to see what's how is the distribution of the cool main land for just specific Carlos Lizárraga | UA DSI DataLab: species, and
Carlos Lizárraga | UA DSI DataLab: these are these are the bar plots for the cooling length for the tree species, the same thing as the cooling depth. Carlos Lizárraga | UA DSI DataLab: And I do the same for the flipper length and the body mass. So everything Carlos Lizárraga | UA DSI DataLab: looks okay. I mean, that's the way the data is distributed. Carlos Lizárraga | UA DSI DataLab: So now I can do I'm going to go and train the model. Carlos Lizárraga | UA DSI DataLab: I'm going to train a model to, to detect or to. Carlos Lizárraga | UA DSI DataLab: Yeah, classify my data set based on numerical variables. Carlos Lizárraga | UA DSI DataLab: And I'm going to train it to identify any new data row Carlos Lizárraga | UA DSI DataLab: that hasn't. That doesn't have the the gender. So I'm going to create a model that classifies the penguins
Carlos Lizárraga | UA DSI DataLab: by gender. So just knowing what is the Coolman length, the Coolman depth the flipper length, the body mass. Carlos Lizárraga | UA DSI DataLab: and the species. I can determine if that a new row Carlos Lizárraga | UA DSI DataLab: the penguin has gender, male or female. So that's the model we're going to create. Carlos Lizárraga | UA DSI DataLab: So we're going to only handle the features, the variables, cooling length, cooling depth, flipper length, body, mass, island and the species. And I'm going to
Carlos Lizárraga | UA DSI DataLab: create a model that predicts, what's the gender? Okay? So Carlos Lizárraga | UA DSI DataLab: I'm going to create a new Carlos Lizárraga | UA DSI DataLab: data frame with these features. And I'm going to call it x. Carlos Lizárraga | UA DSI DataLab: and the other variable will be the gender. The sex encoded is going to be y. Carlos Lizárraga | UA DSI DataLab: and this is called the target variable. So I'm having Carlos Lizárraga | UA DSI DataLab: the variables or the values for the features. I can try to predict what's the sex. Carlos Lizárraga | UA DSI DataLab: So I'm going to Carlos Lizárraga | UA DSI DataLab: split my data, 80% for the training and 20% for the testing. So I I define the test size 20%. Then for Carlos Lizárraga | UA DSI DataLab: reproducibility purposes, I'm going to define my random state to 42.
Carlos Lizárraga | UA DSI DataLab: So if you want to get the same results over and over again, you have to Carlos Lizárraga | UA DSI DataLab: specify what is your random seed for your random or seldom random generator. Carlos Lizárraga | UA DSI DataLab: Okay, so I I do that. Carlos Lizárraga | UA DSI DataLab: And I have, for the training data, I have 266 rows and 6 columns Carlos Lizárraga | UA DSI DataLab: for the testing. I have 67, 67 rows, and 6 columns, and the the white train. I have 266 Carlos Lizárraga | UA DSI DataLab: rows for the gender and for the testing. I have 67. Okay. Carlos Lizárraga | UA DSI DataLab: this is how it looks. White Train. Carlos Lizárraga | UA DSI DataLab: So I have. I have the Carlos Lizárraga | UA DSI DataLab: the the species number and the sex encoded.
Carlos Lizárraga | UA DSI DataLab: And then I'm going to use the standard scalar function to to get like a normal distribution that has Carlos Lizárraga | UA DSI DataLab: average 0 and standard deviation one. Carlos Lizárraga | UA DSI DataLab: So it's the red one. Carlos Lizárraga | UA DSI DataLab: Okay? Carlos Lizárraga | UA DSI DataLab: So I apply it for the X-ray. So I will have the scaled X-train and the scaled XX test. Carlos Lizárraga | UA DSI DataLab: And then these are the the X trainer. Next test. Carlos Lizárraga | UA DSI DataLab: And this is the transformative variables which are Carlos Lizárraga | UA DSI DataLab: values for this Gaussian distribution of average 0 and standard deviation one. So it's just in order to avoid having large numbers, you can see the body mass is in the thousands, and the flipper length and the cooling depth
Carlos Lizárraga | UA DSI DataLab: are in the tens, and these are in the hundreds. So I I do a transformation to have everything Carlos Lizárraga | UA DSI DataLab: normalize between minus 4 and 4, let's say, or minus 3 and 3. Carlos Lizárraga | UA DSI DataLab: Okay, okay. So I have transformed my variables. Now, I can do. Carlos Lizárraga | UA DSI DataLab: There's something in the chat Carlos Lizárraga | UA DSI DataLab: what does Kd. Equals true do here? It just plots the kernel district density estimate. If you put it false, you won't. It won't draw it. It won't show it?
Carlos Lizárraga | UA DSI DataLab: And why do we use transform on test and fit, transform or train? Carlos Lizárraga | UA DSI DataLab: We transform all the numerical variables Carlos Lizárraga | UA DSI DataLab: for the, for the training and testing. So all the values will be between minus 3 and 3. Carlos Lizárraga | UA DSI DataLab: Let's say, okay, so that's why we transform everything. Carlos Lizárraga | UA DSI DataLab: So now, the training test in the new scale variables are going to be this. Carlos Lizárraga | UA DSI DataLab: And then I'm going to.
Carlos Lizárraga | UA DSI DataLab: I'm going to use 3 models for classifiers. I'm going to use the the k nearest neighbor classifier. I'm going to use the decision tree classifier and the support vector classifier Sbc. Carlos Lizárraga | UA DSI DataLab: they all are part of the scikit-learn classifier models, and there are more models. Carlos Lizárraga | UA DSI DataLab: So I'm just going to use 3 models to Carlos Lizárraga | UA DSI DataLab: to prove how these things work. Carlos Lizárraga | UA DSI DataLab: Okay, so now, I'm going to create a model model one Carlos Lizárraga | UA DSI DataLab: using Kna nearest neighbor classifier. And I'm going to use 3 neighbors. Carlos Lizárraga | UA DSI DataLab: So I'm going to create this model one. And then once I create the model one. Carlos Lizárraga | UA DSI DataLab: I'm going to fit the model one using the training scaled and the white train.
Carlos Lizárraga | UA DSI DataLab: The white train is just the target. So this is just the gender 0 or one I don't need to train to scale it Carlos Lizárraga | UA DSI DataLab: and then predict, once I have the model one fitted. Carlos Lizárraga | UA DSI DataLab: then I can use the model one to predict what would be Carlos Lizárraga | UA DSI DataLab: the outcome of the testing scaled. Carlos Lizárraga | UA DSI DataLab: And this will be my Y prediction. Carlos Lizárraga | UA DSI DataLab: So I'm I'm using my model to predict. Carlos Lizárraga | UA DSI DataLab: based on the testing values. And this will be my predicted value. Now, this predicted value can be Carlos Lizárraga | UA DSI DataLab: different than your real value.
Carlos Lizárraga | UA DSI DataLab: So I. I will later use the the metrics to see what is my error. Carlos Lizárraga | UA DSI DataLab: Okay? And then I'm going to use Carlos Lizárraga | UA DSI DataLab: want to create this report value. Carlos Lizárraga | UA DSI DataLab: So I'm going to see what is my testing value for Y, and then compare it with my prediction. Carlos Lizárraga | UA DSI DataLab: And I'm going to print a report the same thing. I'm creating a second model model 2 using the decision tree classifier. And I'm going to define a maximum depth of 3. So you have a tree that you just go 3 levels down.
Carlos Lizárraga | UA DSI DataLab: And this new model, too. I'm going to fit it using the training scales and the Y train. Carlos Lizárraga | UA DSI DataLab: And then, Carlos Lizárraga | UA DSI DataLab: I'm going to predict how well my model 2 works by inputting the testing data. And I will have a predicted value of y or what gender. And then I'm going to report about this difference between these 2, the same thing for the term model using the support vector classifier. I'm going to use a linear kernel and probability true. So you can read Carlos Lizárraga | UA DSI DataLab: about this functioning over Sitekit. Learn. And it will tell you all the different
Carlos Lizárraga | UA DSI DataLab: variables that you can play with, okay. Carlos Lizárraga | UA DSI DataLab: so start simple. And then you start adding things and and Carlos Lizárraga | UA DSI DataLab: and see how what is your result? So you can fine tune your model by by changing the the values of your parameters. Carlos Lizárraga | UA DSI DataLab: For every every every model, every algorithm has a way of of the of moving some parameters. Okay.
Carlos Lizárraga | UA DSI DataLab: alright. So now Carlos Lizárraga | UA DSI DataLab: these are my actual and predicted values. So this is my actual value was 0, and the predicted value was 0. Carlos Lizárraga | UA DSI DataLab: So it seems here there is a difference. My actual value was 0 like mail. Carlos Lizárraga | UA DSI DataLab: and the predicted value was the opposite gender. Okay? And the same thing happens along the different Carlos Lizárraga | UA DSI DataLab: rows. So I need to evaluate my model. So I need to also Carlos Lizárraga | UA DSI DataLab: bring new variables or new ways of measuring the errors. Carlos Lizárraga | UA DSI DataLab: And the 1 1 basic function that we will be using is the Carlos Lizárraga | UA DSI DataLab: confusion matrix. I don't know if you are familiar, but the confusion matrix is just Carlos Lizárraga | UA DSI DataLab: a comparison between the predicted condition and the actual condition. Carlos Lizárraga | UA DSI DataLab: And
Carlos Lizárraga | UA DSI DataLab: so this is the matrix. Your total population is the number of positives plus the number of negatives. So this is the this is your matrix. Carlos Lizárraga | UA DSI DataLab: So if something is positive and your model is identified as positive, then you have a true positive. Carlos Lizárraga | UA DSI DataLab: But if you're Carlos Lizárraga | UA DSI DataLab: Rho was positive, but the model predicted as negative, then you have Carlos Lizárraga | UA DSI DataLab: a false negative, and then, if something is negative Carlos Lizárraga | UA DSI DataLab: and detects as negative, you have a true negative, and if it was negative, but you detected as positive, then you have full positive. So this is Carlos Lizárraga | UA DSI DataLab: use very widely in in
Carlos Lizárraga | UA DSI DataLab: in doing any any clinical tests. Right? So you have true positives, true negatives. These are the real values. And when you missed Carlos Lizárraga | UA DSI DataLab: misinterpret, then you have a false negative Carlos Lizárraga | UA DSI DataLab: if you identify something negative, but it's positive. It's called false, negative, and Carlos Lizárraga | UA DSI DataLab: vice versa. If you have something negative and it's identified as positive. You have false positive in base of these quantities. Tp, true, positive, false, negative. Carlos Lizárraga | UA DSI DataLab: false, positive, and true negatives. They define a set of different Carlos Lizárraga | UA DSI DataLab: metrics. Okay. So you can see. For example, let me just Joe here.
Carlos Lizárraga | UA DSI DataLab: there is a okay. So nothing here. Sorry. Carlos Lizárraga | UA DSI DataLab: Okay, maybe later. Carlos Lizárraga | UA DSI DataLab: Alright. So I'm going to use the confusion matrix Carlos Lizárraga | UA DSI DataLab: and comparing the testing versus the prediction for each of the models Carlos Lizárraga | UA DSI DataLab: for model 1, 2, and 3, Carlos Lizárraga | UA DSI DataLab: and I get these numbers back. Okay, but Carlos Lizárraga | UA DSI DataLab: these are not very good for interpreting. So I'm going to use Seaborne Carlos Lizárraga | UA DSI DataLab: and Seaborn has a heatmap function. So I can represent this Carlos Lizárraga | UA DSI DataLab: boring a set of numbers into more Carlos Lizárraga | UA DSI DataLab: interesting ones. So this is my confusion matrix for the gay nearest neighbors.
Carlos Lizárraga | UA DSI DataLab: And these these are the actual actual values, and these are on the top are the predicted values. Right? So Carlos Lizárraga | UA DSI DataLab: this is positive. So of all the Carlos Lizárraga | UA DSI DataLab: I have 37, 37 rows for the testing set Carlos Lizárraga | UA DSI DataLab: identified 32 positive and 5 positives where I identify as negatives and identified 28 negatives were, which were negatives. But
Carlos Lizárraga | UA DSI DataLab: there were 2 false positives. So out of the 30 negative values or rows. Carlos Lizárraga | UA DSI DataLab: I only identified 28 as negative, and I missed on 2. So these are the misclassifications. These 5 were classified as negative Carlos Lizárraga | UA DSI DataLab: and were actually positive. And these 2 were positive, and they were actually identified as negatives. Carlos Lizárraga | UA DSI DataLab: And the second confusion. Matrix is something similar. 31 were put, were identified as positive, and they were positive. But 6 positives were identified as negatives. Carlos Lizárraga | UA DSI DataLab: and 25 of the negatives were identified as negatives, and 5 of the negatives were misclassified as positives. Carlos Lizárraga | UA DSI DataLab: The same thing for the 3rd model. Okay, 33, 4, 3, 27.
Carlos Lizárraga | UA DSI DataLab: Okay, so how do you decide which is a better model from the tree? Carlos Lizárraga | UA DSI DataLab: To identify that, then we have to go and take advantage of this following metrics. Carlos Lizárraga | UA DSI DataLab: So there is this function called classification report function. It will give you a set of metrics. Carlos Lizárraga | UA DSI DataLab: and in base of that Carlos Lizárraga | UA DSI DataLab: Then you can decide what are the different values of the different metrics Carlos Lizárraga | UA DSI DataLab: so precision and recall. I don't know if you are familiar, but it will just show you. Carlos Lizárraga | UA DSI DataLab: Let's see if I know.
Carlos Lizárraga | UA DSI DataLab: Sorry. Carlos Lizárraga | UA DSI DataLab: Okay. Carlos Lizárraga | UA DSI DataLab: okay, now I have it. Right? So this is precisionary. Recall. Carlos Lizárraga | UA DSI DataLab: The precision is just the true positives divided by the sum of the true positives plus false negatives. The recall is the true positives divided by Carlos Lizárraga | UA DSI DataLab: all the, the, the true positive plus the false negatives. Okay. Carlos Lizárraga | UA DSI DataLab: And these are the the formulas.
Carlos Lizárraga | UA DSI DataLab: And let's see if I have down here. Yes. Carlos Lizárraga | UA DSI DataLab: so down here you will find all the different Carlos Lizárraga | UA DSI DataLab: definitions for different variables. So every Carlos Lizárraga | UA DSI DataLab: every group of researchers have their Carlos Lizárraga | UA DSI DataLab: favorite metrics or depending what is the application? Maybe they have to talk about positive prediction or precision, or can talk about Carlos Lizárraga | UA DSI DataLab: fallout specificity specificity through negative rates. And so there are many, many. You have the f 1 score you have the Jacquard index. Carlos Lizárraga | UA DSI DataLab: So you will find that different groups use different definitions to measure the accuracy of their Carlos Lizárraga | UA DSI DataLab: confusion. Matrix. All right. So let me go back.
Carlos Lizárraga | UA DSI DataLab: And then there's the f 1 score and the accuracy. So if you're using Psyche, learn, then we can use the different Carlos Lizárraga | UA DSI DataLab: variables. So this function, the met. The classification report gives you back. Carlos Lizárraga | UA DSI DataLab: For example, when you're using the K neighbors, nearest neighbors algorithm, you have for this 0 and one values. You have precision point 9 4 on the positive values Carlos Lizárraga | UA DSI DataLab: and point 8 5 precision on the negative values. The recall is 0 point 8 6 and point 9 3 respectively. The f 1 score is point 9 0 and 0 point 8 9,
Carlos Lizárraga | UA DSI DataLab: and the support, the number of positive values were 37, or Carlos Lizárraga | UA DSI DataLab: I think, male gender and 30 females. Okay? So the the sum of the population is 67. The accuracy Carlos Lizárraga | UA DSI DataLab: is point 9 0, and then you can take an average, a macro average of each variable and the weighted average of each variable. This is for the K nearest neighbors. Carlos Lizárraga | UA DSI DataLab: and this is for the decision tree. All the precision. Recall f, 1 score support. You can compute other metrics Carlos Lizárraga | UA DSI DataLab: using scikit-learn. Carlos Lizárraga | UA DSI DataLab: And okay, so this set of numbers is very difficult to interpret. So now Carlos Lizárraga | UA DSI DataLab: what we can do is take advantage of the Roc curve Carlos Lizárraga | UA DSI DataLab: the Roc curve which is the receiver operating characteristic curves.
Carlos Lizárraga | UA DSI DataLab: and it shows the performance of binary classifiers. In our case our model is a binary classifier, so it tells me, if. Carlos Lizárraga | UA DSI DataLab: as an item or a species is male or female. So this is my binary classification classifier, and also, besides binary classifiers, you have multi-label classifiers. Let's say Carlos Lizárraga | UA DSI DataLab: you don't. You don't want to predict the gender, male or female. But you want to predict the species. Now you have 3 species, and then you're Carlos Lizárraga | UA DSI DataLab: confusion. Matrix is not going to be 2 by 2. Matrix is going to be 3 by 3 matrix. Carlos Lizárraga | UA DSI DataLab: So and and you can have a Carlos Lizárraga | UA DSI DataLab: a multi-level multi-label problem classification problem. Okay? So this is the our C plot.
Carlos Lizárraga | UA DSI DataLab: and it will show the area under the curve. So let me do it for the Carlos Lizárraga | UA DSI DataLab: K nearest neighbors Classifier. Carlos Lizárraga | UA DSI DataLab: So I have the model. I have the prediction. And then I'm going to use the Roc Carlos Lizárraga | UA DSI DataLab: core function. I give the training data, the training predicted, and I have some other parameters. Carlos Lizárraga | UA DSI DataLab: Okay? And then I draw it. So here, let me make it smaller. Carlos Lizárraga | UA DSI DataLab: So this is my, our Roc group. Carlos Lizárraga | UA DSI DataLab: and I. It gives you back the audio under the curb.
Carlos Lizárraga | UA DSI DataLab: So the dotted line is when your model is completely random. It's just classifying random female or male. Carlos Lizárraga | UA DSI DataLab: It doesn't have any precision. It's just it's just a random returning value. Carlos Lizárraga | UA DSI DataLab: But when your model is good it will start giving more true values compared with the Carlos Lizárraga | UA DSI DataLab: with a false positive. So this is this is my curve.
Carlos Lizárraga | UA DSI DataLab: True, positive rate versus true, false, positive rate. Carlos Lizárraga | UA DSI DataLab: and the true positive rate is the number of positive. The number of true positives divided by the number of positive values, and the false positive is the number of Carlos Lizárraga | UA DSI DataLab: false, positive, divided by the number of true negatives. Carlos Lizárraga | UA DSI DataLab: Okay, so this is the curve. And Carlos Lizárraga | UA DSI DataLab: if your model were 100% precise, always detecting the males as males and the females as females, then this curve will have an area of equals one.
Carlos Lizárraga | UA DSI DataLab: But if you're the area under the core is 0 point 5, that means your model is just random. It's not giving you any answer. It's just answering random. Carlos Lizárraga | UA DSI DataLab: So that's the curve for the for the K nearest neighbors. Carlos Lizárraga | UA DSI DataLab: The Roc core for the decision decision tree classifier is similar, but the area under the core is 0 point 9 3 compared to point 9 4, Carlos Lizárraga | UA DSI DataLab: and the 3rd model, which is the support vector machine classifier. Carlos Lizárraga | UA DSI DataLab: The area under the core is point 91. So what model would you select? Carlos Lizárraga | UA DSI DataLab: You select the model which has the highest roc curve and area. Carlos Lizárraga | UA DSI DataLab: So in our case, we have point 9 4 Carlos Lizárraga | UA DSI DataLab: point 9 3 and point 91. So our best model

Ask anything
We've read this whole study set, so questions about it get the best answers.