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.
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.
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.
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))
In this dataset, we get information about the features contained in it as follows:
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())
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())
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()}")
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.
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()
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()
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()
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()
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()
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()
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())
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()
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)
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))
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.
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:
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.
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.
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.
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.
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.