Hello,
I am (name) from LearnVern,
This tutorial is in continuation of our previous session.
So, let's see ahead.
So, we are going to see about SVR in today's session of Machine Learning.
We have always discussed SVM, which is a support vector machine.
So, in a support vector machine, if your output is categorical, then we call it an SVC, that is support vector classifier, because it will classify.
And, if the same output that we are getting is continuous, so in this case we will call it as SVR, which is a regressor.
So, let's move ahead, now I will introduce to you our today's dataset.
Our, today's dataset is about Position Salaries,
And here I uploaded the file, and from here I will copy path it's file path,
Now, let's import the libraries.
So, import pandas as pd, this is our first library,
Second, the library that we need is Numpy, so import numpy as np.
Third, the library that we will be needing is matplotlib dot pyplot as PLT.
So, we require these three libraries, apart from this whatever else we will need, we will import that during run time.
So, the first dataset is equal to pd dot read CSV, and here we will paste its path, so this way our dataset is loaded now.
So, by using dataset do head we will explore our dataset and understand, as to what we need to perform.
So, here you can see we have 'position', such as business analyst, junior consultant, senior consultant, manager, country manager.
After that, you can see we have 'level' as 1, 2, 3, 4, 5, and then after that we have ' 'salary', so here you can see that as the level is increasing so is the salary getting increased.
So, if somebody's level at the time of joining the company is one, then his salary will also be less that time, as here it is 45000, so as his experience will increase and his level also will increase, then his salary will also increase.
So, in this way our data is correlated among themselves.
So, if we want to take up an inference, change in levels lead to change in salary, that is with increase in level ,salary also increases.
So, let's proceed ahead and perform some more things in this, so we can determine our x and y here.
My input is of x, we will get our X from the data set, data set dot, here I will be use iloc function, your I will take all the records but for the column I will take only 'level' column, which is 1, so I will fetch this by putting one colon two.. and putting dot values, I will convert this into an array,
So, now if you will see through X dot shape, then you can see it has 10 values or records and one column.
So, let's move ahead and extract y also,
So y is equal to data set dot iloc, again I will take all the records and directly fetch the second column, and I will also like to have this in numpy format.
Now we will see y dot shape, so in this also we have 10 records, and this is a one dimensional array.
So in this way X and Y are determined.
So after we have determined X and y now we know that in data preprocessing we need to split the data for training and testing also.
Show this train and test, how can we perform the split in this?
For that,
From, SKlearn dot model selection, SK Learn will help us, from SK learn dot model selection , import from here we will import train test split.
And this train test split will help us in dividing the data.
Now we will have X train, after that we will have X test, and after this the two variables that we will have, they are y train and y test, is equal to train test split, and here we will pass our original data, with this we will pass test size as 0.2 percent, that is 2 percent, for now.
And, random state, we will initialise that also as zero.
So, our data is divided now, now to see, x underscore train,
And this is our x train.
And, in the same way, x underscore test comma, y underscore test, so this is our x test and y test.
So, you can see in this we get 2 records, and here also 2 records, rest 1 record has come under x train.
So, this way our data is also divided.
Now, we can scale or transform these values with the help of standard scalers.
So, we will do that also,
Now, we will scale this with the help of standard scaler, from sklearn dot preprocessing, as this is also a part of preprocessing, import standard scaler.
So, we will import standard scaler.
So, after importing,
First we will create an object for standard scaler X, s t a n d a r d(standard scaler).
In the same way we will create one standard scaler for y,
So, I created a second object, for whom? For y!
So for standard scaler X and for standard scaler y, for both of them we created one object for each.
Now, let's use them,
So, x is equal to sc underscore X dot fit transform, and in fit transform we will give X as input.
Now, our values for X are scaled.
Now, in the same way we will do for y,
Y is equal to sc underscore y,(this is an object for y) dot, so as we had used fit transform before, in the same way,we will use it here also, and in fit transform we will insert y. Let's execute and see!
So, let's see what are the changes made in our y.
So, here we will have to reshape our y.
So, we will reshape y so that we can transform it.
So, y dot reshape, with this y, will be reshaped, and we will get such values from y transform.
So, we will move ahead.
So, our values are scaled also, both x and y have been scaled by us.
So, on the basis of this x and y we will train our model.
So, we are going to implement SVR, support vector regressor,
So, here from sklearn dot SVM import...SVR'.
So, with SVM, what did we import? We imported SVR.
Now, here regressor, so we will create a Regressor, is equal to SVR, and in SVR.. we can pass the kernel, so what is the kernel that we will use?, So by default here it is RBF, so I will also be using RBF kernel only,
So our regressor model is prepared here.
Now, this model will help me in learning, because it only has to learn it.
regressor dot fit, and here x comma y, so this way we gave our input and output, for it to learn.
And, our SVR has learned this now, so now we can use this for predictions,
So, come let's use this for prediction.
So, y underscore pred, in this variable we will store our Predictions, so y underscore pred is equal to,
So, what do we have here?..
First…we will do prediction here, thereafter we will transform our data also.
So, for performing prediction, we will need a regressor dot predict, so to predict what is the value that we need to put? We will put the value of x test, before putting X test value, we should transform it, so sc underscore X, (with this we will transform this) dot transform, here we will put.. let's put some array over here.
So, here we will put Np dot a r r a y array, our array will be 6 point 5.
So, what is the prediction that it is giving on 6 point 5 value, let's first see that.
Y underscore pred…so its value has come as, minus 0.27.
So, this is our value, minus 0.27. okay?
But, now we will inverse transform this value, and find its actual Value.
So, y underscore pred is equal to sc underscore Y, and in this we will put dot inverse transform, and we will be able to transform this, here we will insert y underscore pred.
And we will see what the value of y underscore pred is, now.
So, we will reshape this also.
After reshaping, then we will get the value of y pred.
So, here we reshaped this also,
And now we will get the value of y pred.
So, y underscore pred, so we got the value as 1 7 0 3 7 0, that is 1 lakh 70 thousand 370.
So, this means if an individual has achieved 6.5 level, then his salary should be this.
So, in this way we use support vector regressor, with the help of kernel function's RBF that is radial basis function we applied this, and we are able to conduct predictions
Now, let's see how our function fits in between our data points, so here we will also see our regression line also.
So, now we will visualise and see,
So to visualise and see we will start with scatter plot, PLT dot scatter, and here we will have our x and y, after that we will have the colour that we want, so let's say red, now I will execute this and show you.
So, by executing this, you can see our data points plot formed here.
After this we will additionally add here PLY dot plot, here I will put x and Regressor.. dot here we will predict, so regressor dot predict.. and here what are we going to insert? here also we will insert x, so what is the prediction it will give for x?
And here for the colour we will take as blue, so that it looks a little different.
So, C is equal to let's take it as blue.
Now, we will execute this and see.
Now, you can see this is our regression line, it got fitted here, so this blue line is a regression line and the Red ones are the data points.
So, let us label it also.
Plt dot let's give it titles as truth and bluff, meaning correct or bluff, so t i t l e we will give it a title, I will name it as truth or b l u f f bluff , along with that here plt dot x label, so what is X label? It is position level, p o s i t i o n - position, l e v e l - level.
After position level, plt dot, what will be our y label? Our y label is our salary, so this is salary s a l a r y - salary.
Ok! So let's display this,
Plt dot show, so now you can see our chart has a title also, below it's showing us position level, and here it is showing salary.
So, in this way we are able to visualise also, as to How the line is getting fit in there, which is a Regression line on the data points.
I hope you must have enjoyed this.
So, keep learning and remain motivated.
Thank you very much.
So, we will see its other parts in the next session.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.