0% found this document useful (0 votes)
14 views18 pages

Housing Price Prediction with Python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Housing Price Prediction with Python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

Linear Regression with Python


Here neighbor is a real estate agent and wants some help predicting housing prices for
regions in the INDIA. It would be great if i could somehow create a model for her that allows
her to put in a few features of a house and returns back an estimate of what the house
would sell for.

She has asked me if i could help her out with your new data science skills. me say yes, and
decide that Linear Regression might be a good path to solve this problem!

My neighbor then gives you some information about a bunch of houses in regions of the
India,it is all in the data set: INDIA_Housing.csv.

The data contains the following columns:

'Avg. Area Income': Avg. Income of residents of the city house is located in. 'Avg. Area House
Age': Avg Age of Houses in same city 'Avg. Area Number of Rooms': Avg Number of Rooms
for Houses in same city 'Avg. Area Number of Bedrooms': Avg Number of Bedrooms for
Houses in same city 'Area Population': Population of city house is located in 'Price': Price that
the house sold at 'Address': Address for the house

Let's get started!

Check out the data


In [1]: ## Import Libraries

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
%matplotlib inline

In [2]: INDIAhousing = pd.read_csv("INDIA_Housing.csv")

[Link]()

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 1/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

Out[2]: Avg.
Avg. Avg. Area
Area
Avg. Area Area Number Area
Number Price Address
Income House of Population
of
Age Bedrooms
Rooms

208 Michael Ferry Apt.


0 79545.458574 5.682861 7.009188 4.09 23086.800503 1.059034e+06 674\nLaurabury, NE
3701..

188 Johnson Views


1 79248.642455 6.002900 6.730821 3.09 40173.072174 1.505891e+06 Suite 079\nLake
Kathleen, CA...

9127 Elizabeth
2 61287.067179 5.865890 8.512727 5.13 36882.159400 1.058988e+06 Stravenue\nDanieltown,
WI 06482..

USS Barnett\nFPO AP
3 63345.240046 7.188236 5.586729 3.26 34310.242831 1.260617e+06
44820

USNS Raymond\nFPO
4 59982.197226 5.040555 7.839388 4.23 26354.109472 6.309435e+05
AE 09386

In [3]: [Link]()

<class '[Link]'>
RangeIndex: 5000 entries, 0 to 4999
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Avg. Area Income 5000 non-null float64
1 Avg. Area House Age 5000 non-null float64
2 Avg. Area Number of Rooms 5000 non-null float64
3 Avg. Area Number of Bedrooms 5000 non-null float64
4 Area Population 5000 non-null float64
5 Price 5000 non-null float64
6 Address 5000 non-null object
dtypes: float64(6), object(1)
memory usage: 273.6+ KB

In [4]: [Link]()

Out[4]: Avg. Area Avg. Area


Avg. Area Avg. Area Area
Number of Number of Price
Income House Age Population
Rooms Bedrooms

count 5000.000000 5000.000000 5000.000000 5000.000000 5000.000000 5.000000e+03

mean 68583.108984 5.977222 6.987792 3.981330 36163.516039 1.232073e+06

std 10657.991214 0.991456 1.005833 1.234137 9925.650114 3.531176e+05

min 17796.631190 2.644304 3.236194 2.000000 172.610686 1.593866e+04

25% 61480.562388 5.322283 6.299250 3.140000 29403.928702 9.975771e+05

50% 68804.286404 5.970429 7.002902 4.050000 36199.406689 1.232669e+06

75% 75783.338666 6.650808 7.665871 4.490000 42861.290769 1.471210e+06

max 107701.748378 9.519088 10.759588 6.500000 69621.713378 2.469066e+06

In [5]: [Link]

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 2/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis
Index(['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
Out[5]:
'Avg. Area Number of Bedrooms', 'Area Population', 'Price', 'Address'],
dtype='object')

Exploratory Data Analysis for House Price


Prediction
In [6]: [Link](INDIAhousing)

<[Link] at 0x21385c9ef50>
Out[6]:

In [7]: [Link](INDIAhousing['Price'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Price'])

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 3/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis
<Axes: xlabel='Price', ylabel='Density'>
Out[7]:

In [8]: [Link](INDIAhousing['Area Population'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Area Population'])
<Axes: xlabel='Area Population', ylabel='Density'>
Out[8]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 4/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [9]: [Link](INDIAhousing['Avg. Area Income'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Avg. Area Income'])


<Axes: xlabel='Avg. Area Income', ylabel='Density'>
Out[9]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 5/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [10]: [Link](INDIAhousing['Avg. Area House Age'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Avg. Area House Age'])


<Axes: xlabel='Avg. Area House Age', ylabel='Density'>
Out[10]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 6/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [11]: [Link](INDIAhousing['Avg. Area Number of Rooms'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Avg. Area Number of Rooms'])


<Axes: xlabel='Avg. Area Number of Rooms', ylabel='Density'>
Out[11]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 7/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [12]: [Link](INDIAhousing['Avg. Area Number of Bedrooms'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Avg. Area Number of Bedrooms'])


<Axes: xlabel='Avg. Area Number of Bedrooms', ylabel='Density'>
Out[12]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 8/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [13]: [Link](INDIAhousing['Area Population'])

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link](INDIAhousing['Area Population'])
<Axes: xlabel='Area Population', ylabel='Density'>
Out[13]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 9/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [15]: [Link](INDIAhousing['Price'])

<Axes: xlabel='Price', ylabel='Count'>


Out[15]:

In [16]: [Link](INDIAhousing['Avg. Area Income'])

<Axes: xlabel='Avg. Area Income', ylabel='Count'>


Out[16]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 10/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [17]: [Link](INDIAhousing['Avg. Area House Age'])

<Axes: xlabel='Avg. Area House Age', ylabel='Count'>


Out[17]:

In [18]: [Link](INDIAhousing['Avg. Area Number of Rooms'])

<Axes: xlabel='Avg. Area Number of Rooms', ylabel='Count'>


Out[18]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 11/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [19]: [Link](INDIAhousing['Avg. Area Number of Bedrooms'])

<Axes: xlabel='Avg. Area Number of Bedrooms', ylabel='Count'>


Out[19]:

In [20]: [Link](INDIAhousing['Area Population'])

<Axes: xlabel='Area Population', ylabel='Count'>


Out[20]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 12/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [21]: INDIAhousing_numeric = [Link](columns=['Address'])

In [22]: [Link](INDIAhousing_numeric.corr(), annot=True)

<Axes: >
Out[22]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 13/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

Training a Linear Regression Model


In [23]: X = INDIAhousing[['Avg. Area Income','Avg. Area House Age','Avg. Area Number of Roo

Y = INDIAhousing['Price']

Split Data into Train ,Test


In [24]: from sklearn.model_selection import train_test_split

In [25]: X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.4, random_sta

Creating and Training the Linear Regression Model


In [26]: from sklearn.linear_model import LinearRegression

In [27]: lm = LinearRegression()

In [28]: [Link](X_train,Y_train)

Out[28]: ▾ LinearRegression

LinearRegression()

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 14/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

Linear Regression Model Evaluation


In [29]: print(lm.intercept_)

-2640159.79685267

In [30]: coeff_df = [Link](lm.coef_,[Link],columns=['Coefficient'])


coeff_df

Out[30]: Coefficient

Avg. Area Income 21.528276

Avg. Area House Age 164883.282027

Avg. Area Number of Rooms 122368.678027

Avg. Area Number of Bedrooms 2233.801864

Area Population 15.150420

Interpreting the coefficients:

Holding all other features fixed, a 1 unit increase in Avg. Area Income is associated with an
increase of $21.52

Holding all other features fixed, a 1 unit increase in Avg. Area House Age is associated with
an increase of $164883.28

Holding all other features fixed, a 1 unit increase in Avg. Area Number of Rooms is
associated with an increase of $122368.67

Holding all other features fixed, a 1 unit increase in Avg. Area Number of Bedrooms is
associated with an increase of $2233.80

Holding all other features fixed, a 1 unit increase in Area Population is associated with an
increase of $15.15

Does this make sense? Probably not because I made up this data. If you want real data to
repeat this sort of analysis, check out the boston dataset:

from [Link] import load_boston

boston = load_boston()

print([Link])

boston_df = [Link]

Predictions from our Model


Let's grab predictions off our test set and see how well it did!

In [31]: predictions = [Link](X_test)

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 15/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [32]: [Link](Y_test,predictions)

<[Link] at 0x2138b335750>
Out[32]:

Residual Histogram
In [33]: [Link]((Y_test-predictions),bins=50);

C:\Users\HP\AppData\Local\Temp\ipykernel_4772\[Link]: UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
[Link]

[Link]((Y_test-predictions),bins=50);

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 16/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [34]: [Link]((Y_test-predictions),bins=50);

Regression Evaluation Metrics


In [35]: from sklearn import metrics

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 17/18


4/18/24, 6:43 AM Linear Regression With Python - Housing data Analysis

In [36]: print('MAE:', metrics.mean_absolute_error(Y_test, predictions))


print('MSE:', metrics.mean_absolute_error(Y_test, predictions))
print('RMSE:', [Link](metrics.mean_squared_error(Y_test, predictions)))

MAE: 82288.22251914942
MSE: 82288.22251914942
RMSE: 102278.82922290884

Thank You
In [ ]:

localhost:8888/nbconvert/html/Linear Regression With Python - Housing data Analysis .ipynb?download=false 18/18

Common questions

Powered by AI

Using synthetic data in training a linear regression model might not capture real-world complexities and variabilities, potentially leading to biased or overly simplistic models that perform poorly on real data. Synthetic data might lack the intricate relationships seen in natural datasets, such as non-linear interactions or contextual socio-economic factors affecting housing prices. As claimed, the dataset was made up, thus interpretations or predictions might not hold against genuine trends or anomalies inherent in actual market conditions. This limitation underscores the importance of validating model outcomes with real-world data .

Using a Linear Regression model to predict housing prices allows for establishing relationships between the dependent variable (price) and several independent variables such as area income, house age, number of rooms, and population. It leverages the simplicity of linear relationships to make predictions, providing insights into how each feature contributes to house pricing. The linear model's coefficients indicate the level of influence each feature has on price, facilitating strategic decisions for pricing strategies. For instance, holding other features constant, an increase in 'Avg. Area Income' results in a price increase by the magnitude of its coefficient, allowing for a more targeted approach in influencing real estate decisions .

With the coefficient for 'Area Population' being 15.15, indicating a price increase for each unit rise in population, the small magnitude compared to other features could imply potential underestimation of population influence. Anomalies may arise if the dataset does not adequately represent variability in population densities or if there are significant non-linear relationships unaccounted for by the linear model. Further, the assumption of a constant relationship might overlook complex socio-economic dynamics influencing housing prices, suggesting careful examination is needed to ensure model accuracy and relevance .

Splitting the dataset into training and test sets is crucial for evaluating the model's performance on unseen data, which helps to avoid overfitting. The training set is used to train the model and learn the patterns between features and the target variable, while the test set provides an independent evaluation to assess the model's predictive accuracy and generalization ability. By withholding a portion of the data for testing, it ensures that the model's predictions are realistic and not overly adjusted to the dataset's specific idiosyncrasies .

The use of the `distplot` function in seaborn, which has been deprecated and will be removed in future versions, is a deprecated practice noted in the analysis. An alternative is to use the `displot` or `histplot` functions, which offer more flexibility and are aligned with recent updates in the seaborn library. Transitioning to these functions ensures the code remains compatible with library updates and leverages enhanced visualization capabilities, promoting best practices in data analytics .

Visualizations, such as pair plots and distribution plots, play a crucial role in supporting linear regression analysis by providing graphical insights into the data's distribution and relationships between variables. Pair plots help illustrate correlations and interactions between different features and the dependent variable, revealing potential linear relationships. Meanwhile, distribution plots offer a view of how each feature varies, indicating the normality or skewness that might affect the regression model's assumptions. These visual tools assist in identifying trends, outliers, and ensuring the model is appropriate for the data distribution .

The 'Avg. Area Income' impacts house pricing with a regression coefficient of 21.52. This indicates that for every one-unit increase in the average area income, while keeping all other variables constant, the house price is expected to increase by $21.52. This relationship highlights the positive correlation between area income and house value, suggesting that wealthier areas tend to have higher house prices .

The residual histogram visualizes the differences between observed and predicted values, helping in assessing the linear regression model's performance by highlighting the distribution of prediction errors. A well-performing model should exhibit residuals that are normally distributed around zero, indicating unbiased predictions. Any skewness or pattern in the histogram would suggest model shortcomings, such as systematic errors or unmet assumptions, guiding necessary adjustments or exploration of alternative models. Uniformity and mild spread of residuals in this histogram indicate reasonable model performance .

The 'Avg. Area Number of Rooms' is associated with a regression coefficient of 122368.67, meaning that for each additional room in the area, holding other features constant, the house price increases by $122,368.67. This is a significant impact, showing a strong positive correlation between the number of rooms and house value. Compared to other features like 'Avg. Area Income' or 'Area Population,' the number of rooms has a much larger coefficient, indicating a stronger influence on house prices, surpassed only by 'Avg. Area House Age,' which has an even higher impact .

The performance metrics—Mean Absolute Error (MAE), Mean Squared Error (MSE), and Root Mean Squared Error (RMSE)—indicate the magnitude of prediction errors. In this case, with MAE, MSE, and RMSE values, one can infer the average deviation of predictions from actual values. While these metrics provide a quantitative measure of error, lowering them requires steps like incorporating additional influential features, applying data transformations, using more sophisticated algorithms, or enhancing feature selection to improve the model's predictive accuracy, thereby reducing error rates .

You might also like