Hello everyone and welcome to support vector machines project solutions lecture and this lecture we're
going to be going over the solutions for the support vector machines project.
Let's go ahead and jump to notebook to get started.
OK so here I am at the Jupiter notebook.
Let's go ahead and get started with the solutions.
So scroll down to the first task and the first thing you wanted to do was to use seaborne to get the
iris data set by using iris sequels essence estopped load underscore dataset Iris.
Let me go ahead and import Seaborn or do this or say import Seaborn as Asinus and we'll go ahead and
copy that line.
Iris equals Asinus low data set and we pass an iris.
And that will load the dataset for us.
So just a quick note Seaborn has its own data sets and we've seen things such as the tips data set for
.
We'll go ahead and just use Seaborn to load the iris data set and we're going to import some libraries
we think we'll need.
And basically this is just hander's in pi and set map plot lived in line in order to actually see the
visualizations for the exploratory data analysis section where we're going to do is create a pair plot
of the data set.
And there's a question attached to this which flower species seems to be the most separable.
So I'm going to say Asinus pear plot passen Iris.
For my data frame and then I'm also going to specify the hue to be by the species columns.
If we actually take a look.
Our Iris looks like we have Sipple length sequel with Pedda length and petal with.
And then a species column.
And when we use that column actually color my data and I'm going to go ahead and change the default
palette to dark to just so it matches the solutions no book.
And if you go ahead and run that you'll notice that in most of the features pretty much all of them
.
It looks like this green class of species is really separable from the other two.
And that's going to be a Tosa class as the Tosa is highly separable pretty much any machine learning
algorithm should be able to separate this easily if it's a competent algorithm.
We're going to go ahead and also create a KDE plot of people link versus people with for the Seto's
a species of flowers.
So to do that I'm going to set a object called the Tosa and that's going to be the iris state of frame
or the species column was equal to.
So Tosa and then I'm going to say as soon as the Haiti plot and then I'm going to passen.
So Tosa we go ahead and check this out.
Pasan on the x axis Sipple with.
So we can go ahead and say so Tosa can actually is pass the whole column.
You want to see all underscore with for why we're going to go ahead and Passons the Tosa simple underscore
length and then set the see map to plasma.
So looks a little nicer and I will say shade is equal to true.
So it shades in let's go ahead and run that and let's not shade in the lowest value which is the start
purple in order to do that.
I can just do shade underscore lowest equals false and then I get a plot that looks just like what we
have in the solutions.
So kind of a tricky plot you had a separate First the data frame in order to use Seaborn to do this
.
But if you were having trouble with this don't worry too much about it doesn't have much of the project
.
It was kind of just something to practice your Seaborn skills with.
Now go ahead and move on to train split.
We'll go ahead and split our data into a training and testing set using sikat learn.
Or go ahead and say from S-K learn cross underscore validation in poor trainset split.
Run that and then train to split.
I'm going to go ahead and just grab that default example that we always use right here.
Then it's going to pass it in we'll go in and just use random state is equal to 42.
Not a big deal in this case since the data sets are pretty small.
Also test size equal 2.3.
And then I'll specify extreme features which is just everything minus the species column.
That's going to be along the x axis circle 1 and then WHY is going to be just the species column itself
.
What we just dropped is going to run that and now we have our train test split.
Let's go ahead and train a model.
We want to call the SPC model for SK learn and fit the model to the training data.
So we're going to say from Eskdale learn not as V.M. import as we see and then I'm going to go ahead
and say as we see model is an instance of SPC and I'll go ahead and fit that model if it's the faults
of the training data X train and white train.
You'd get a model of the default sequel's one Amma's the string auto.
OK now let's go ahead and grab predictions from the model and create a confusion matrix and a classification
report.
I will go ahead and say predictions is equal to s v c underscore model predict off of the X test data
.
So my test features.
And then I'm going to import from S-K learn metrics my classification report and my confusion matrix
.
Then I can just say print's confusion matrix comparing Whitehurst to my predictions and I get something
that looks like that.
And if it's different from the answers don't worry about it.
Probably because of a different random split in your data due to the train split.
And then I'm going to go ahead and call classification report on Whitehurst and my predictions.
OK.
And it looks like we're doing pretty good.
We've only mislabelled two of them.
We'll go out and see if we can improve this.
And this is going to be really hard.
It's a pretty small dataset and we can only work with two here and this may change depending on what
she used for.
The train has split as far as what your random value split was.
Let's go ahead and see if we can practice using grid search.
So the device is quite small you probably won't be satisfied of these results in real life but let's
go on and continue on.
Well go ahead and say from S-K learn helps us learn the grid search import.
In this case I want grid search C-v and we'll go out and create a dictionary called Parama grid and
fill out some parameters for sea and gamma.
I'll say Parama grid is equal to.
We have a dictionary in the past and some values for R C value.
Well go ahead and do one similar to what we did in lecture 0.1 one 10 and 100.
So just by powers of 10 there and then we will say gamma and and some values here we'll say one of 0.1
zero points.
0 1 and 0.00 1.
OK.
So we have a grid and we will go ahead and do is create a grid search C-v object and fit it to the training
data.
So this is going to be two lines here.
I will create a grid search object Hassen s b c as the model I want to use harams grid as my pram grid
and I'm going to go ahead and say verbose is equal to the number two.
So I see a little bit of output but not as much as three.
And then I'm going to go ahead and fit this to my training data.
Run that and it'll run pretty fast depending on how fast computer is.
But the data sets small enough that it shouldn't take that much time to run and then we'll go ahead
and do the exact same thing as we did before.
By creating grid predictions we have grid that predicts off my tests underscore a test.
And now let's go ahead and try that confusion matrix again will say confusion matrix passen Whitehurst
and my grade predictions.
And you'll notice we did a little better here than we did with the default parameters.
We were actually able to improve it by one classification.
So our confusion matrix going to be a little better and so should our classification report.
So we go ahead and pasan why underscore test and predictions and we can compare this classification
of poor to the default parameters up here for a classification report.
Overall both models did fairly well but it's definitely important to practice how to do a good grid
search using sikat learn especially when you don't when you're dealing with real world data.
OK.
Hope you enjoyed the project.
It's really just an exercise in making sure you learn the syntax for using grid search.
And as always calling a model from sikat learn if you have any questions feel free to post the Q&A forums
and the other students I will be happy to help you out.
Thanks everyone and I'll see you at the next lecture.