Linear regression
This notebook follows the content of the lesson on linear regression. In it, you can try to solve the task of determining real estate prices based on square footage.
First, load the libraries that we will need in further work.
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(37)
Now execute the following cell to generate the dataset of real estate properties. The information about the square footage is represented by the variable x while the information about their prices is represented by the variable y.
x = np.array([43, 25, 66, 80, 105, 70, 40, 85, 84, 102])
y = np.array([60, 32.1, 88.4, 111.4, 120.32, 72.1, 46.3, 90.1, 99.6, 139.2])
Execute the following cell to get a graphical representation of the points in the dataset.
plt.title('Real Estate Prices')
plt.xlabel('Square Footage')
plt.ylabel('Price')
plt.grid('on')
plt.scatter(x, y)
plt.show()

In the next cell, there is a function that will draw the line you choose in the exercise. Right below it is a function that draws the line you choose and the lines that indicate your errors. It might be useful to you when looking for the best parameter values.следећој ћелији се налази функција која ће исцртавати праву коју одабереш у вежби. Одмах испод ње је и функција која исцртава праву коју одабереш и линијице које означавају твоје грешке. Можда ће ти бити од користи приликом тражења најбољих вредности параметара.
def draw_graph(beta_0, beta_1):
plt.title('Real Estate Prices')
plt.xlabel('Square Footage')
plt.ylabel('Price')
plt.grid('on')
plt.scatter(x, y)
model_x = np.linspace(20, 120, 100)
model_y = beta_0 + beta_1*model_x
plt.plot(model_x, model_y, color='red')
plt.show()
def draw_graph_with_errors(beta_0, beta_1):
plt.title('Real Estate Prices')
plt.xlabel('Square Footage')
plt.ylabel('Price')
plt.grid('on')
plt.scatter(x, y)
model_x = np.linspace(20, 120, 100)
model_y = beta_0 + beta_1*model_x
plt.plot(model_x, model_y, color='red')
for i in range(0, 10):
prediction = beta_0 + beta_1*x[i]
if prediction > y[i]:
ymin = y[i]
ymax = prediction
else:
ymin = prediction
ymax = y[i]
plt.vlines(x=x[i], ymin=ymin, ymax=ymax, colors='blue', linestyles='dotted')
plt.show()
beta_0 = 3.84 #@param {type:"slider", min:-1, max:15, step:0.01}
beta_1 = 0.62 #@param {type:"slider", min:-2, max:4, step:0.01}
print("Odabrani model: y = {beta_0} + {beta_1}*x".format(beta_0=beta_0, beta_1=beta_1))
draw_graph(beta_0, beta_1)
Odabrani model: y = 3.84 + 0.62*x

beta_0_selected = 3.84
beta_1_selected = 0.62
draw_graph_with_errors(beta_0_selected, beta_1_selected)

The error function associated with the linear regression model is called the mean squared error. The following function can help you calculate the mean squared error for your choice of parameters.
def calculate_mean_squared_error(beta_0, beta_1, x, y):
y_predictions = beta_0 + beta_1 * x
individual_errors = y - y_predictions
return np.average(individual_errors**2)
calculate_mean_squared_error(beta_0_selected, beta_1_selected, x, y)
Out[ ]:
np.float64(1810.21888)
def my_function_to_calculate_mean_squared_error(beta_0, beta_1, x, y):
pass
- Make a submission