Open oldoc63 opened 1 year ago
We can also do this calculation using the .predict() method on the fitted model. To predict the weight of a 160 cm tall person, we need to first create a new dataset with height equal to 160 as shown below:
newdata = {"height":[160]}
print(results.predict(newdata))
Output:
0 58.33
dtype: float64
Note that we get the same result (58.33) as with the other methods; however, it is returned as a data frame.
.params
.
Suppose that we have a dataset of heights and weights for 100 adults. We fit a linear regression and print the coefficients:
This regression allow us to predict the weight of an adult if we know their height. To make a prediction, we need to plug in the intercept and slope to our equation for a line. The equation is:
$$ weight = 0.50 * height - 21.67 $$
To make a prediction we can plug in any height. For example, we can calculate that the expected weight for a 160 cm tall person is 58.33kg:
$$ weight = 0.50 * 160 - 21.67 = 58.33 $$
In Python, we can calculate this by plugging in values or by accessing the intercept and slope from results.params using their indices (0 and 1, respectively):