Open In App

House Price Prediction using Linear Regression | Django

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we'll explore how to use a Python machine-learning algorithm called linear regression to estimate house prices. We'll do this by taking input data from users who want to predict the price of their home. To make things more accessible and interactive, we'll transform this house price prediction code into a web-based system using the Django framework. Django is a Python web framework that helps us turn a regular Python project into a web application.

House Price Prediction Project using Linear Regression in Django

For our data source, we'll be using the House Price Prediction dataset. With this system, users can easily predict a house's value by providing specific details. We've converted the original code into a user-friendly web form, allowing individuals to input the necessary information for predicting house prices. Once they've entered all the required details, users can click on the 'predict' button, and the result will be displayed on the same page in the form of a quick message.

Starting the Project Folder

Installation: To install Django follow these steps. To install the necessary modules follow this commands:

pip install pandas
pip install matplotlib
import os
pip install seaborn
pip install scikit-learn

To start the project use this command

django-admin startproject HOUSEPREDICTION
cd HOUSEPREDICTION

To start the app use this command

python manage.py startapp HousePrediction

Now add this app to the 'settings.py'

Now you can see the file structure like this which display on your vs studio.

h1
File Structure


setting.py: In this file, we import 'os' and connect templates to the folder by using the following 'os.path.join' code. To connect the 'templates' folder with the 'settings.py' file, add the following code line to the 'TEMPLATES' section of the 'settings.py' file, as shown in the image.

set
Setting.py


view.py: The dataset is read from the 'USA_Housing.csv' file, and the 'Address' column is dropped.The independent variables (X) and the dependent variable (Y) are separated.The data is split into training and testing sets using train_test_split.A Linear Regression model is created and fitted to the training data.User input values are obtained from the GET request.A prediction is made using the model.The prediction is rounded to an integer.A message containing the predicted rent is created.Finally, the 'predict.html' file is rendered with the prediction result.

Python
import os
from django.shortcuts import render
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import  metrics


def home(request):
    return render(request, 'home.html')
def predict(request):
    return render(request, 'predict.html')
def result(request):
    data = pd.read_csv(r"USA_Housing.csv")
    data = data.drop(['Address'], axis=1)
    X = data.drop('Price', axis=1)
    Y = data['Price']
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size= .30)

    model = LinearRegression()
    model.fit(X_train, Y_train)

    var1 = float(request.GET['n1'])
    var2 = float(request.GET['n2'])
    var3 = float(request.GET['n3'])
    var4 = float(request.GET['n4'])
    var5 = float(request.GET['n5'])
   

    pred = model.predict(np.array([var1, var2, var3, var4, var5]).reshape(1, -1))

    pred = round(pred[0])

    price = "The Predicted Rent Is Rs "+str(pred)
    return render(request, 'predict.html', {"result2":price}) 

Creating GUI

home.html: This HTML file is used to view the home page.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page</title>
    <style>
        * {
            background-color: black;
            color: white;
        }
        .hero-banner {
            text-align: center;
            margin-top: 50px;
        }
        .btn {
            color: black;
            background-color: green;
            padding: 10px 20px;
            font-weight: bold;
            font-size: 15px;
            border-radius: 5px;
        }
        img {
            width: 400px;
            display: block;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="hero-banner">
        <h1>Welcome to the House Price Prediction System!</h1>
        <p>Use this system to predict your house price or rent. Note: This is a prediction system with 85 to 90% accuracy.</p>
        <img src="https://media.geeksforgeeks.org/wp-content/uploads/20230228090229/house1.png" alt="House Image">
        <form action="predict">
            <button class="btn" type="submit">Predict House Price</button>
        </form>
    </div>
</body>
</html>

predict.html: This HTML file is used to print the results .

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Prediction Result</title>
    <style>
        * {
            background-color: black;
            color: white;
        }
        .result {
            text-align: center;
            margin-top: 50px;
        }
        .btn {
            color: black;
            background-color: green;
            padding: 10px 20px;
            font-weight: bold;
            font-size: 15px;
            border-radius: 5px;
            text-decoration: none;
        }
        .btn:hover {
            background-color: darkgreen;
        }
    </style>
</head>
<body>
    <div class="result">
        <h1>Prediction Result</h1>
        <p>{{ result2 }}</p>
        <a href="/" class="btn">Go Back</a>
    </div>
</body>
</html>

urls.py: This is the URL file which is used to define all the urls.

Python
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
    path('predict/', views.predict),
    path('predict/result', views.result),
]



Deployement of the Project

Run these commands to apply the migrations:

python3 manage.py makemigrations
python3 manage.py migrate

Run the server with the help of following command:

python3 manage.py runserver

Output


The accuracy of the prediction system is 80% according to the linear regression algorithm.


Article Tags :
Practice Tags :

Similar Reads