Welcome back.
We finished the first step, which is preparing the data.
Now let's train the linear regression model.
We'll use the linear regression to fit a line.
Price equals to W times size plus b.
This is the linear regression relationship.
Linear regression is a model from sklearn.
Linearmodel.
Because we used here linear regression that tries to predict a numeric value like price based on one
or more input features like size.
It assumes that the relationship between the features and the target is linear.
For example, it can be approximated by a straight line in 2D or a flat plane hyperplane in higher dimensions.
So think about it as a linear line.
So if we go up here we have uh a straight line.
If you if you see it like it's a straight line.
So this is the linear.
This is the linear relationship between size and the price okay.
So this is the basic linear regression model.
At first we need to set the model and tell the Python interpreter.
With that we need to use this as a linear regression.
So model equals to linear regression.
Here we create an instance of the linear regression class.
At this point the model exists but hasn't learned anything yet.
It's like a blank sheet waiting to be trained.
Okay.
And this is from the library that we imported before.
The second step is to use this model to train it.
So here we are training the model.
We use model dot fit.
So I created an instance of the linear regression class and use this model instance to train the model.
Model.fit.
The fit method is used to train the model.
So fit.
I love to write notes down.
Please pay attention to those notes because they are very important.
Please write them down and because it's the revision and helps you understand the code better.
It looks like a very simple.
Yes.
So Model.fit is the function.
This is the method that trains the model.
It looks at the training data, exit train for sizes, white train for prices, and calculates the best
slope and intercept B that minimizes the difference between predicted and actual prices.
So here training the model involves many steps.
So this function gets xtrain and ytrain, which are the sizes and prices respectively, and calculates
the best slope and intercept b that minimizes the differences between predicted and actual prices.
After this step, the model knows the relationship between house size and the price.
Again, guys, I want from you to focus with me here.
We have a linear relationship between size and the price.
We have this straight line.
And as you know we have this relationship price equals to three size plus two.
This is normally a linear relationship between size and price.
It's in form of y equals ax plus b.
So A is the slope or w is a slope.
You can make it like w plus uh here like linear relationship y equals to w x plus b.
So the slope is called w and intercept is b.
Those are used by the model to minimize the difference between predicted and actual prices.
After this step the model knows the relationship between house size and price.
Okay.
The third step is the learned parameters.
Here we have the learned parameters w.
The model coefficient contains the weights or the slopes learned from this feature.
Since we have only one feature, which is the size, we take the first element with zero.
So model dot coefficient.
Since we have only one feature, we take only the zero from this array.
So since we have only one feature, B contains the bias or intercept the value of price when size equals
to zero.
Let's run this cell and here we go.
Model trained learned equation price equals to 2.9 times size plus 2.7.
So after training this model this model gets and learned the equation that price equals to 2.9 times
size plus 2.7.
So this is the magic.
This is how this model get to train.
How this model.
Get this equation and conclude this equation y equal 2.9 x plus 2.7.
So price equals to 2.9 times size plus 17.
So each 1000ft² increases in house size adds roughly um two $2,900 to the price starting from 2007
$2,170.
So in general, what we've done here, we created a linear regression model, training it on the training
data, extract the slope x and intercept b, print the learned linear equation.