0
votes

I have just trained a linear regression model, getting my intercept and coefficient for house prices based on the "number_rooms" and "price". But I am just a bit unsure on how to plot my regression model using a scatter plot with a line of best fit.

Any help would be much appreciated on how to do this - thanks!

Here's my code:

rgr = linear_model.LinearRegression()
rgr.fit(X=sample['number_rooms'].values.reshape(-1,1), y=sample['price'].values)

print(rgr.intercept_, rgr.coef_[0])

predictions = rgr.predict(X=sample['number_rooms'].values.reshape(-1,1))
metrics.mean_squared_error(y_pred=predictions, y_true=sample['price'], squared=False)
3

3 Answers

1
votes

That's quite simple, try this once-

import matplotlib.pyplot as plt

# to plot predictions
plt.scatter(sample['number_rooms'].values, predictions.array.reshape(-1, 1), c='g', label='predicted prices')

# to plot the best fit line
min_rooms = 0 # or 1
max_rooms = 10 #say

rooms_range = range(min_rooms, max_rooms+1)
plt.plot(rooms_range, rgr.predict(X=rooms_range).array.reshape(-1, 1), c='r', label='best fit')
plt.legend()
0
votes

The other thing you can try is the following:

Given that now you know the slope and the intercept, find a reasonable range - a range that best describes the number of rooms and then

y = m*number_of_beds + intercept

then plot the y and x on a scatter plot

0
votes

Lets look into math of it

Linear regression fits a line for y given x which minimizes the error. This line is represented using: y = w*x +b

w = rgr.coef_[0]
bias = rgr.intercept_

number_rooms = sample['number_rooms'].values

plt.plot([min(number_rooms), max(number_rooms)], 
         [w*min(number_rooms)+bias, w*max(number_rooms)+bias])