<jupyter_start><jupyter_text>Linear Regression with one variableIn this example, we use Linear Regression as an exploratory data analysis tool. The objective is to understand how well can we explain the SalePrice of a house using real attributes individually. Github version:https://github.com/rodmsmendes/reinforcementlearning4fun/blob/master/Linear_Regression_with_one_variable.ipynbKaggle kernel version:https://www.kaggle.com/rodolfomendes/linear-regression-with-one-variable/notebook  1. Importing librariesFor this notebook we load some of the most common libraries for Data Science wiht Python:* NumPy* Pandas* Matplotlib* scikit-learn<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error<jupyter_output><empty_output><jupyter_text>2. Loading Data and Selecting AttributesThen we use Pandas to load data from house pricing competition, into a DataFrame.<jupyter_code>url = 'https://raw.githubusercontent.com/rodmsmendes/reinforcementlearning4fun/master/data/house_prices.csv'
df = pd.read_csv(url)<jupyter_output><empty_output><jupyter_text>For this litte example, we select only the attributes which represents measurements. So, for our new DataFrame we select only the attributes of the **float64** type. Next with print information about the attributes as well their basic statistics.<jupyter_code>df_float = df.select_dtypes(include=['float64']).copy()
df_float.info()
df_float.describe()<jupyter_output><empty_output><jupyter_text>3. Filling missing valuesFrom the DataFrame information printed above, we can observe the DataFrame has a total of 1460 rows. But looking at the attributes we observe that all of themhas less than 1460 non-null values each. So we need to handle this missing values in order to proceed. So, for each attribute we use the method *fillna()*to fill the missing values with the their respective mean value.<jupyter_code>df_float['LotFrontage'] = df['LotFrontage'].fillna(df['LotFrontage'].mean(), inplace=False)
df_float['MasVnrArea'] = df['MasVnrArea'].fillna(df['MasVnrArea'].mean(), inplace=False)
df_float['GarageYrBlt'] = df['GarageYrBlt'].fillna(df['GarageYrBlt'].mean(), inplace=False)<jupyter_output><empty_output><jupyter_text>After filling the missing values we can check that each attribute has 1460 non-null values each:<jupyter_code>df_float.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1460 entries, 0 to 1459
Data columns (total 3 columns):
LotFrontage    1460 non-null float64
MasVnrArea     1460 non-null float64
GarageYrBlt    1460 non-null float64
dtypes: float64(3)
memory usage: 34.3 KB<jupyter_text>4. Regression Analysis for LotFrontageOnce we handled the missing values for each attribute, we can proceed to the linear regression analysis. In this example we build a linear regression model to explain the SalePrice of a house depending on the LotFrontage measurement. As a first step we save the respective attributes into variables *lotFrontage* and *salePrice*. Instead of the traditional *X* e *y* variables we use the attribute name for clarity purposes.<jupyter_code>lotFrontage = df_float[['LotFrontage']]
salePrice = df['SalePrice']<jupyter_output><empty_output><jupyter_text>Next we create an instance of *LinearRegression* class from scikit-learn and call the method *fit()* passing the columns as parameters to create our model.<jupyter_code>lr1 = LinearRegression()
lr1.fit(lotFrontage, salePrice)<jupyter_output><empty_output><jupyter_text>We our model created, print the model coeficient, intercept and measure the mean squared error of the model.<jupyter_code>print(lr1.coef_)
print(lr1.intercept_)
print(mean_squared_error(salePrice, lr1.predict(lotFrontage)))<jupyter_output>[1208.0155492]
96299.75696070473
5599428130.969559<jupyter_text>5. Regression Analysis for MasVnrAre We the repeat the same steps of section 4 for the MasVnrAre attribute.<jupyter_code>masVnrAre = df_float[['MasVnrArea']]
salePrice = df['SalePrice']
lr2 = LinearRegression()
lr2.fit(masVnrAre, salePrice)
print(lr2.coef_)
print(lr2.intercept_)
print(mean_squared_error(salePrice, lr2.predict(masVnrAre)))<jupyter_output>[209.08536998]
159242.12458511663
4882373206.33175<jupyter_text>6. Regression Analysis for GarageYrBlt We the repeat the same steps of section 4 for the GarageYrBlt attribute.<jupyter_code>garageYrBlt = df_float[['GarageYrBlt']]
salePrice = df['SalePrice']
lr3 = LinearRegression()
lr3.fit(garageYrBlt, salePrice)
print(lr3.coef_)
print(lr3.intercept_)
print(mean_squared_error(salePrice, lr3.predict(garageYrBlt)))<jupyter_output>[1556.68774577]
-2898995.104363801
4912566774.814545<jupyter_text>7. ResultsThe results of ou regression analysis are presented in the charts below. For each analysed atrribute we use a scatter plot to represent the data points (in blue) userd for the analysis. Then we use the predicted values (the output of the model) to plot a line representing the linear model (in orange).<jupyter_code>f = plt.figure()
f, ax = plt.subplots(1, 3, figsize=(30, 8))

ax = plt.subplot(1, 3, 1)
plt.ylabel('SalePrice')
plt.xlabel('LotFrontage')
ax = plt.scatter(lotFrontage, salePrice)
ax = plt.plot(lotFrontage, lr1.predict(lotFrontage), linewidth=5.0, color='orange')

ax = plt.subplot(1, 3, 2)
plt.ylabel('SalePrice')
plt.xlabel('MasVnrArea')
ax = plt.scatter(masVnrAre, salePrice)
ax = plt.plot(masVnrAre, lr2.predict(masVnrAre), linewidth=5.0, color='orange')

ax = plt.subplot(1, 3, 3)
plt.ylabel('SalePrice')
plt.xlabel('GarageYrBlt')
ax = plt.scatter(garageYrBlt, salePrice)
ax = plt.plot(garageYrBlt, lr3.predict(garageYrBlt), linewidth=5.0, color='orange')

ax = plt.show()<jupyter_output><empty_output><jupyter_text>Finally, we use a bar chart to compare the mean squared error of each model.<jupyter_code>names = ['LotFrontage', 'MasVnrArea',	'GarageYrBlt']
heights = [
           mean_squared_error(salePrice, lr1.predict(lotFrontage)),
           mean_squared_error(salePrice, lr2.predict(masVnrAre)),
           mean_squared_error(salePrice, lr3.predict(garageYrBlt))]

f = plt.figure(figsize=(8,8))
ax = plt.bar(names, heights)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09661989795918367, 'fraction_numerical': 0.03730867346938776, 'mean_word_length': 6.84125, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '351151', 'license': 'Unlicense', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2071, 'n_words': 581, 'path': 'Linear_Regression_with_one_variable.ipynb', 'repo_name': 'rodmsmendes/reinforcermentlearning4fun', 'size': '6272'}