In this video we will learn how to use scikit-learn for linear regression. The
first thing we need to do is import. We will import a Pydataset to use in this
example and let's get panda's and numpy imported as well. Next, we import the
things we will need from scikit-learn so the linear regression comes from linear
underscore model and will also import from scikit-learn the train test split.
and lastly let's get matplotlib in there so we can visualize this model. So, first
of all let's get our data. We will be using the pima women dataset. This is
about Native American women living in Phoenix. What we're going to attempt to
do is use triceps skinfold measurements to predict BMI. So, first what we want
to see is if this data looks approximately linear, so we're gonna use
the pandas plotting capabilities to plot BMI against skin. So, here on the y axis
we have BMI and we have the skin fold measurement and that looks pretty linear
so we're gonna go ahead and proceed with our linear regression model.
Now we are going to do a test train split. We are doing supervised learning;
basically we create the model using only the training data and then we use the
model to see how well it predicts the testing data. It will choose a random
place to split the train test data; usually at about 70% train 30% tests. And
real quick let's go ahead and visualize what that train test split looks like.
We're gonna plot this in a scatter plot first. We'll do x_train and y_train
and we'll do plt dot scatter with X_test y_test.
and we'll make the training data read and the testing data green.
Alright, so let's plot that so you can see exactly what I mean. Everything in
the red here will be used to create our line or linear model, and that line will
then be tested on everything in the green. Okay, so now let's go ahead and
actually create our linear model. So, first we'll do LR is equal to a linear
regression object and then we'll do LR dot fit and we will plug in the X_train
and y_train and we'll have to reshape X_train because the input must be
two-dimensional so a reshape of -1 1 should be just fine.
And there it is. Now let's use this model to predict on our test data. We will plot
that against the scatter plot of the actual test data, so we'll do prediction
is equal to LR dot predict and we'll plug in our X_test... and of course what we
have to reshape that...
so this is what our model predicts the X_test or in other words the skinfold
BMI will be and then we'll do PLT dot plot and we'll plug in the X_test and
the prediction,
make that a blue line and then we'll do a scatter plot of the X_test against the
actual y_test
then we'll make that green again.
Okay, so there it is, it seems to do a decent job at following the overall
trend but there could be a few outliers.
Maybe this guy up here is an outlier?
Alright so there's our model, now suppose we want to see how the model will
predict a specific skinfold measurement... say 50. Let's plug that in and see what
our model predicts. So with the skinfold measurement of 50 this predicts that the
BMI of that woman will be 39 point 5 4 and you can see that in our model right
here. At 50 the line goes up to about 39 at that point and that's exactly what
this model is doing and how it is predicting.
Alright, now we will score the model using SciKit-Learn's built-in score
function. So, I'll just plug in X_test and Y_test and it will score how accurate
that is. You can see the output is about 0.399. Now, the max it could
get would be 1.0. I want you to think about what that score means. Is this a
good score? Is this a good model? Write me what you think in the comments below. So
there you have it, that is how you can use pythons scikit-learn to create a
linear regression model. Please check out some of my other Python videos and
please subscribe for more Python content. :D