A Deep Dive into LSTM Neural Network-based House Rent Prediction

Ata Amrullah 14 Jun, 2023
9 min read

Introduction

Long Short Term Memory (LSTM) is a type of deep learning system that anticipates property leases. By leveraging its unique architecture and memory retention capabilities, LSTM offers an innovative approach to understand the house rent prediction patterns with extraordinary precision.

Rental markets are influenced by diverse factors, and LSTM’s ability to capture and remember long-term dependencies enables the modeling of complex relationships and the extraction of meaningful patterns from historical rental data. Moreover, LSTM networks can adapt to changing market trends, making them suitable for capturing evolving rental patterns and fluctuations. Continual training with new data ensures the model’s relevancy and accuracy in dynamic rental markets, making LSTM a powerful tool for generating accurate predictions and providing valuable insights to both tenants and landlords.

Now, let’s make the prediction through the steps in the article.

Learning Objectives

  1. Make accurate estimates of house rental prices that are useful for both tenants and landlords.
  2. Gain insights into market trends and patterns to identify factors that influence house rental prices.
  3. Provide valuable information to support decision-making processes for tenants and landlords.
  4. Help overcome the information asymmetry between tenants and landlords to facilitate fair and informed rental transactions.

Based on these goals, we will use the LSTM Neural Network to develop a model that can estimate the house rental price prediction accurately based on number of bedroom, size of the house, area type, location, furnishing status, and number of bathroom. This article will guide you on predicting house rental price using LSTM.

This article was published as a part of the Data Science Blogathon.

Step 1: Import Library

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.graph_objects as go
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, LSTM

An explanation of the use of several libraries that will be used in the prediction task this time has been explained in a previous article, Food Delivery Time Prediction with LSTM. Therefore, in this article, we don’t need to discuss it anymore.

Step 2: Read the Data

To build a house rental prediction system, we need a dataset that contains the factors that affect the rental price of a residential property. I found a dataset on Kaggle that is perfect for this task. For convenience, I put the dataset on GitHub.

url = 'https://raw.githubusercontent.com/ataislucky/Data-Science/main/dataset/house_rent_price.csv'
data = pd.read_csv(url)
print(data.sample(5))
 Dataset Overview I
Dataset Overview I

In this dataset, we get information about the features contained in it as follows:

  1. BHK: Number of Bedrooms, Hall, and Kitchen
  2. Rent: Rent of the houses, apartments, and flats.
  3. Size: Size of the Houses, Apartments, and Flats in Square Feet.
  4. Floor: Houses/Apartments/Flats situated in which Floor and Total Number of Floors (Example: Ground out of 2, 3 out of 5, etc.).
  5. Area Type: The size of the houses, apartments, or flats is calculated on either the super area, carpet area, or build area.
  6. Area Locality: Locality of the Houses, Apartments, or Flats.
  7. City: City where the houses, apartments, and flats are located.
  8. Furnishing Status: The furnishing status of the houses, apartments, or flats is either furnished, semi-furnished, or unfurnished.
  9. Tenant Preferred: Type of Tenant Preferred by the Owner or Agent.
  10. Bathrooms: Number of Bathrooms.
  11. Point of Contact: Whom should you contact for more information regarding the houses, apartments, or flats.

Step 3: House Rent Analysis

Before doing the analysis, we need to check whether the dataset we have contains a null value or not because it will affect the effectiveness of the built model.

print(data.isnull().sum())
 Data Checking
Data Checking

Descriptive Statistics

Let’s look at the descriptive statistics of the data now. We can gain a better grasp of the data and discover possible concerns by using it. This data can be used to make decisions on how to process it.

print(data.describe())
 Data Description
Data Description

To make it easier for us to read the rental price data, we use code like the following:

print(f"Mean Rent: {data.Rent.mean()}")
print(f"Median Rent: {data.Rent.median()}")
print(f"Highest Rent: {data.Rent.max()}")
print(f"Lowest Rent: {data.Rent.min()}")
 Simple Data Description
Simple Data Description

We can gain a better grasp of the rent prices in the dataset by examining these metrics. This data may be used to make rental decisions, such as how much to spend on rent or what type of house to rent.

Rental Prices in Different Cities

Now let’s look at the rental prices in different cities according to the number of bedrooms, halls and kitchens.

figure = px.bar(data, x=data["City"], 
                y = data["Rent"], 
                color = data["BHK"],
            title="Rent in Different Cities According to BHK")
figure.show()
 rent in Bangalore BHK (3)
rent in Bangalore BHK (3)
 rent in Mumbai BHK (3)
rent in Mumbai BHK (3)

As we can see, in Bangalore, the highest rental price for a house with a BHK 3 is 3.5 million. In contrast to Mumbai, with a BHK of 3, the value of housing rents is lower by around 55 thousand.

Now let’s look at rented houses in various cities according to the type of area.

figure = px.bar(data, x=data["City"], 
                y = data["Rent"], 
                color = data["Area Type"],
            title="Rent in Different Cities According to Area Type")
figure.show()
 rent in Kolkata Carpet Area
rent in Kolkata Carpet Area
 rent in Delhi Carpet Area
rent in Delhi Carpet Area

Now let’s look at the price of renting houses in different cities according to the status of the furniture.

figure = px.bar(data, x=data["City"], 
                y = data["Rent"], 
                color = data["Furnishing Status"],
            title="Rent in Different Cities According to Furnishing Status")
figure.show()
 rent in Chennai unfurnished
rent in Chennai unfurnished
 rent in Hyderabad unfurnished
rent in Hyderabad unfurnished

Now let’s look at rented houses in different cities according to the size of the house.

figure = px.bar(data, x=data["City"], 
                y = data["Rent"], 
                color = data["Size"],
            title="Rent in Different Cities According to Size")
figure.show()
 rent based on house size in Chennai
rent based on house size in Chennai
 rent based on house size in Mumbai
rent based on house size in Mumbai

Let’s now look at the number of houses available for rent in different cities by data set.

cities = data["City"].value_counts()
label = cities.index
counts = cities.values
colors = ['gold','lightgreen']

fig = go.Figure(data=[go.Pie(labels=label, values=counts, hole=0.5)])
fig.update_layout(title_text='Number of Houses Available for Rent')
fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=30,
                  marker=dict(colors=colors, line=dict(color='black', width=3)))
fig.show()
 Number of Available Rent House
Number of Available Rent House

The most rental housing is available in Mumbai, with around 972 units, and the least in Kolkata, with 524 units. Let us now examine the quantity of dwellings accessible for different types of tenants.

tenant = data["Tenant Preferred"].value_counts()
label = tenant.index
counts = tenant.values
colors = ['gold','lightgreen']

fig = go.Figure(data=[go.Pie(labels=label, values=counts, hole=0.5)])
fig.update_layout(title_text='Preference of Tenant in India')
fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=30,
                  marker=dict(colors=colors, line=dict(color='black', width=3)))
fig.show()
 Available Rent House based On Tenant
Available Rent House based On Tenant

Step 4: Build an LSTM Model and Make Predictions

Before we create a learning model, we need to convert all categorical features to numeric features first.

data["Area Type"] = data["Area Type"].map({"Super Area": 1, 
                                           "Carpet Area": 2, 
                                           "Built Area": 3})
data["City"] = data["City"].map({"Mumbai": 4000, "Chennai": 6000, 
                                 "Bangalore": 5600, "Hyderabad": 5000, 
                                 "Delhi": 1100, "Kolkata": 7000})
data["Furnishing Status"] = data["Furnishing Status"].map({"Unfurnished": 0, 
                                                           "Semi-Furnished": 1, 
                                                           "Furnished": 2})
data["Tenant Preferred"] = data["Tenant Preferred"].map({"Bachelors/Family": 2, 
                                                         "Bachelors": 1, 
                                                         "Family": 3})
print(data.head())
 Data Overview II
Data Overview II

Now we will split the data into training and test sets.

#splitting data
x = np.array(data[["BHK", "Size", "Area Type", "City", 
                   "Furnishing Status", "Tenant Preferred", 
                   "Bathroom"]])
y = np.array(data[["Rent"]])

xtrain, xtest, ytrain, ytest = train_test_split(x, y, 
                                                test_size=0.10, 
                                                random_state=33)

Now, we need to train an LSTM neural network to predict the rent house price.

We will build a model that incorporates LSTM layers in a sequential neural network, aiming to exploit dependencies and temporal patterns in rental price data. The LSTM layer allows the model to learn from the sequential properties of data, capture long-term dependencies, and store information over time. Then we create the input data shape parameter to have the shape (xtrain.shape[1], 1), where xtrain is the training data. This means the model takes an input sequence with length xtrain.shape [1] and a single feature dimension.

The next assignment is return_sequences=true; the first LSTM layer ensures that subsequent layers receive all hidden state sequences, facilitating increased learning. The number of units or cells we set at 128 and 64 in each LSTM layer is enough to influence the ability of the model to capture complex patterns, with the necessary balance between complexity and computational resources. The dense layer after the LSTM layer processes the extracted information is 25 units, and the final dense layer with one unit outputs the predicted rent.

model = Sequential()
model.add(LSTM(128, return_sequences=True, 
               input_shape= (xtrain.shape[1], 1)))
model.add(LSTM(64, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
model.summary()
 Model Summary
Model Summary

Now let’s train the previously created model.

model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(xtrain, ytrain, batch_size=1, epochs=10)
 Model Fitting
Model Fitting

Finally, let’s test the model’s performance for predicting the rent house price given seven input parameters (BHK, size, area type, city pin code, furnishing status, tenant type, and number of bathroom).

print("Enter House Details to Predict Rent")
print("BHK = Bedrooms, Hall, and Kitchen\n")
a = int(input("Number of BHK (1 -- 6): "))
b = int(input("Size of the House (10 -- 8000): "))
c = int(input("Area Type (Super Area = 1, Carpet Area = 2, Built Area = 3): "))
print("\npincode = Mumbai: 4000, Chennai: 6000, Bangalore : 5600") 
print("pincode = Hyderabad: 5000, Delhi: 1100, Kolkata: 7000\n")
d = int(input("Pin Code of the City: "))
e = int(input("Furnishing Status of the House: "))
f = int(input("Tenant Type (Bachelors = 1, Bachelors/Family = 2, Only Family = 3): "))
g = int(input("Number of bathrooms (1 -- 10): "))
features = np.array([[a, b, c, d, e, f, g]])
print("Predicted House Price = ", model.predict(features))
 Prediction Result
Prediction Result

The output of the prediction is shown as “Predicted House Price = [[52324.74]],” which means that the model has estimated that the rent house price is approximately 52,3324.74 rupees.

Conclusion

This article starts by checking if there are blank values in the dataset and analyzing the data with descriptive statistics. Then look at the relationship between features and price variables before predicting the range of house rental prices using the LSTM. In short, we have discussed the following:

  • LSTM neural networks offer a powerful and effective approach for predicting rental housing prices.
  • By leveraging historical rental data, LSTM models can capture complex patterns and dependencies in the housing market.
  • LSTM models provide accurate predictions of rental prices, enabling tenants and landlords to make informed decisions.
  • Continuous monitoring and retraining of LSTM models using up-to-date data is crucial to maintaining their predictive performance in dynamic rental markets.

Overall, this article provides a comprehensive guide to predicting rental housing prices with an LSTM neural network using Python. If you have any questions or comments, please leave them below. The complete code can be seen here.

Frequently Asked Questions

Q1. What is house pricing prediction?

A. House pricing prediction refers to the task of estimating the value or selling price of a house based on various factors like location, size, amenities, and market trends. It uses machine learning or statistical models to analyze historical data and generate accurate price predictions for real estate purposes.

Q2. What is LSTM in neural network?

A. LSTM (Long Short-Term Memory) is a type of recurrent neural network (RNN) architecture widely used for sequential data analysis. It is designed to address the vanishing gradient problem in traditional RNNs, making it effective in capturing long-term dependencies and patterns in sequential data.

Q3. What algorithm does LSTM use?

A. LSTM utilizes a specific algorithm called the LSTM algorithm, which incorporates memory cells and gates to control the flow of information within the network. These gates regulate the information flow, allowing LSTMs to selectively retain or forget information from previous time steps.

Q4. Is LSTM a type of neural network?

A. Yes, LSTM (Long Short-Term Memory) is a type of neural network. Specifically, it is a variant of the recurrent neural network (RNN) architecture that addresses the limitations of traditional RNNs in handling long-term dependencies. LSTM networks have memory cells and gates that enable effective sequential data processing.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Ata Amrullah 14 Jun, 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers