Locked lesson.
About this lesson
In this video, learn how to train a linear regression model and how easy it is to fit the model.
Exercise files
Download this lesson’s related exercise files.
Train a Linear Regression Model and Fit The Model.docx57 KB Train a Linear Regression Model and Fit The Model - Solution.docx
55.6 KB
Quick reference
Train a Linear Regression Model and Fit The Model
Now it's time to create an instance of our Linear Regression and fit the Training data to the model.
When to use
Do this whenever you want to create a Linear Regression Model.
Instructions
First create an instance of a Linear Regression model:
lin = LinearRegression()
Next fit our training data to the model:
lin.fit(X_train, y_train)
Hints & tips
- lin = LinearRegression()
- lin.fit(X_train, y_train)
- 00:05 Okay, so we've imported our data.
- 00:08 We've created a data frame of that data, we've created our x and our y here and
- 00:12 our x is are column headers, and
- 00:14 our y is the price data of our actual housing stuff in Boston.
- 00:18 And now we've defined what parts of our model,
- 00:21 what parts of our data we want to train against and what we want to test against.
- 00:26 Now we need to actually train our model, our linear regression model.
- 00:29 So earlier we imported our model that we want to
- 00:32 use sklearn.linear_model is going to be linear regression.
- 00:35 So now we just need to create our linear regression instance or
- 00:39 our linear regression object.
- 00:41 And then just sort of run it, so we could do this very simply,
- 00:45 I'm just going to create a linear regression object.
- 00:48 And I'm just going to call it lin, you can call it anything you want.
- 00:52 And this is just going to be our linear regression instance, right?
- 01:00 So now we can type lin., and
- 01:03 then Shift Tab to learn all kinds of stuff about this.
- 01:07 So linear regression fits a linear model with coefficients
- 01:11 to minimize the residual sum of squared between the observed targets.
- 01:14 Remember I think of scatter plot, of the distance between the points in our scatter
- 01:18 plot and the line that goes through it, right?
- 01:20 fit_intercept is normalize, and if you want to come through here and
- 01:24 just read all of this stuff, it's kind of interesting stuff.
- 01:28 We're going to go through a lot of these things later on after we run this.
- 01:32 And there's even actually some examples that you can go through in here.
- 01:37 Very interesting.
- 01:39 See, we've done this earlier.
- 01:41 Very cool.
- 01:43 So, now that we've created an instance of our linear regression model,
- 01:48 we need to actually just train our data and fit the data.
- 01:51 And to do this, it's incredibly easy.
- 01:53 We just call our lin instance here and we say .fit.
- 01:57 And now if we shift enter, we see it go on and fit the linear model.
- 02:00 If we expand this, it needs parameters.
- 02:03 It needs an x and a y.
- 02:05 And remember, the x is our training data and the y is a target values,
- 02:10 in our case, it's going to be prices.
- 02:12 And you could put some sample way but we don't really care about that right now.
- 02:15 So we want to fit based on not our actual data but
- 02:20 our trained data from when we define X_train, X_test, y_train and
- 02:24 y_test, right, and we split our data between training data and testing data.
- 02:29 So here we need X_train, and we need y_train.
- 02:36 Now Shift Enter to run this and we see this linear regression copy_X=True,
- 02:41 fit_intercept=True, n_jobs=None, normalize=False.
- 02:46 This basically tells us that we're done,
- 02:49 it tells us that our linear regression model has been trained.
- 02:53 And if you're interested in what all of this stuff is,
- 02:56 we can go X_train and just kind of look at, see what these things are.
- 03:00 Oops, train, right?
- 03:03 So it's 303 rows of our data.
- 03:05 Remember our data was 500 and something but
- 03:08 40% of that goes to test size with leftover is 303 of the actual data.
- 03:14 We can also look at y_train.
- 03:17 And we're going to see something similar.
- 03:18 Remember, those are prices.
- 03:20 And those are 303 training data points,
- 03:23 the rest have been turned into testing data.
- 03:26 So our linear regression has been fit onto our training data.
- 03:30 And we're pretty much ready to go.
- 03:32 Now we can start to make predictions.
- 03:33 This is also generated now coefficients and intercepts and
- 03:36 we'll talk about coefficients and intercepts in the next video.
Lesson notes are only available for subscribers.