<jupyter_start><jupyter_text>Simple linear regression - Exercise You are given a real estate dataset. Real estate is one of those examples that every regression course goes through as it is extremely easy to understand and there is a (almost always) certain causal relationship to be found.The data is located in the file: 'real_estate_price_size.csv'. You are expected to create a simple linear regression (similar to the one in the lecture), using the new data. Apart from that, please:-  Create a scatter plot (with or without a regression line)-  Calculate the R-squared-  Display the intercept and coefficient(s)-  Using the model make a prediction about an apartment with size 750 sq.ft.Note: In this exercise, the dependent variable is 'price', while the independent variable is 'size'.Good luck!  Import the relevant libraries<jupyter_code># Import the relevant libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import os
import seaborn as sns
# We can override the default matplotlib styles with those of Seaborn
sns.set()<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code>df = pd.read_csv(os.path.join('real_estate_price_size.csv'))
df.head()<jupyter_output><empty_output><jupyter_text>Create the regression  Declare the dependent and the independent variables<jupyter_code># Define the dependent and independent variables
y = df['price'] # output/target
x = df['size'] # input/feature<jupyter_output><empty_output><jupyter_text>Explore the data<jupyter_code>print(x.shape, y.shape)
plt.scatter(x,y)
plt.xlabel('Size',fontsize=20)
plt.ylabel('Price',fontsize=20)
plt.show()<jupyter_output><empty_output><jupyter_text>Transform the inputs into a matrix (2D object)<jupyter_code>x_matrix = x.values.reshape(-1,1)
x_matrix.shape<jupyter_output><empty_output><jupyter_text>Regression itself<jupyter_code># Regresion itself
reg = LinearRegression()
reg.fit(x_matrix,y)<jupyter_output><empty_output><jupyter_text>Calculate the R-squared<jupyter_code>reg.score(x_matrix,y)<jupyter_output><empty_output><jupyter_text>Find the intercept<jupyter_code>reg.intercept_<jupyter_output><empty_output><jupyter_text>Find the coefficients<jupyter_code>reg.coef_<jupyter_output><empty_output><jupyter_text>Making predictionsYou find an apartment online with a size of 750 sq.ft.All else equal what should be its price according to the model?<jupyter_code>new_df = pd.DataFrame({'size': [750]}).values.reshape(-1,1)
new_df
reg.predict(new_df)
# plot the regression line
plt.scatter(x, y)
yhat = reg.coef_*x_matrix + reg.intercept_
# yhat = 0.0017*x1+0.275
fig = plt.plot(x, yhat, lw=4, c='orange', label='regression line')
plt.xlabel('Size', fontsize=20)
plt.ylabel('Price', fontsize=20)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09790705924086555, 'fraction_numerical': 0.011706278822277403, 'mean_word_length': 7.269794721407624, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '417806', 'license': 'MIT', 'n_tokens_mistral': 964, 'n_tokens_neox': 912, 'n_words': 264, 'path': 'references/exercises/Simple Linear Regression /Simple Linear Regression with sklearn - Exercise.ipynb', 'repo_name': 'Hesbon5600/data-science-basics', 'size': '2819'}