Open In App

Django – Machine Learning Placement Prediction Project

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will create  Placement Prediction Project using the Django framework using Python. In this project, we are using logistic regression algorithm of Python machine learning to predict the Placement of students by observing the input values filled by students according to their study background.

Placement Prediction Project in Django

Basically, we will convert a Placement prediction using logistic regression code into a web system by using the Django framework Django is a Python framework that we use to convert a python project into a web system and we will use the Placement  Prediction dataset as the backend with the use of this system we can predict the student will be placed or not like in as I explained we convert a code into web form so we will create a form for fill the input values which we need to predict Placement and after fill the input values we need to click on predict button through which we can see the result which we display on the same page a flash massage.

Prerequisites

First, create a new folder and open that folder in vs studio and open the terminal and first run the following command for creating a virtual environment. Now we need to install Django which runs the command.

pip install django

and create a new project in Django by using the following command or running the following command in the terminal.

django-admin startproject Placement

File structure:

Macheine Learning Placement Prediction Project

File structure

Templates Folder 

After that, we will make a template folder in which we create two files predict.html and index.html for making the frontend part of our Placement prediction system.

index.html: 

In this file, we create a simple home page in which we write a simple heading message welcoming to the Placement prediction System and add one button for redirecting to the form page which is the second page of our Placement prediction, or redirecting one page to another page we are using a form action tag which we write the function name home which we create view.py file and we added a render_templetes for redirect to predict page.

HTML




{% load static %}
<!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>
        .gfg {
            margin-left: 23%;
            margin-top: 10%;
        }
  
        .btn {
            margin-left: 45%;
            padding: 10px 20px;
            color: white;
            background-color: orange;
            font-size: 20px;
            border-radius: 5px;
            font-weight: bolder;
            cursor: pointer;
  
        }
  
        .btn:hover {
            color: orange;
            background-color: white;
            cursor: pointer;
        }
  
        strong {
            color: green;
  
        }
    </style>
  
</head>
  
<body>
    <div class="container">
        <h1 class="gfg
         "> Welcome to <strong>GeeksForGeeks</strong> Student Placement Prediction Project!</h1>
        <br>
        <form action="predict">
            <button class="btn" type="submit">Click Here</button>
        </form>
    </div>
  
  
  
</body>
  
</html>


Output:

Macheine Learning Placement Prediction Project

Home.html

predict.html:

In this file, we created a simple form in which we need to fill in some input like study details, CGPA, stream, gender, backlog,  and many other details which is a form required for predicting the Placement of a student and also we added a predict button when we click on button it will show the result as a flash message on same display screen like You are placed or not flash massage for perform all operations in our prediction system we created a two function predict and result through which we connected all the files separated and display result.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF=8">
    <title> Prediction page</title>
    <link rel="shortcut icon" type="x-icon" href="https://i.ibb.co/VH7qsyW/Google-Chrome-icon-February-2022-svg.png">
    <style>
        * {
            border-collapse: separate;
        }
  
        .main {
            position: absolute;
            top: 130px;
            right: 15%;
            width: 600px;
            background-color: #e6f1e6;
            border-radius: 15px;
            align-items: center;
            height: 72vh;
            border: 1.5px solid blue;
  
        }
  
        .menu-bar .table {
            margin-left: -150px;
  
        }
  
        h1 {
            font-family: Times New Roman;
            margin: 20px, 0;
            color: rgb(18, 17, 17);
            font-size: 30px;
            font-weight: lighter;
            animation-delay: 3.5s;
            font-style: italic;
        }
  
        input[type=submit] {
            background-color: rgb(127, 216, 131);
            border: 1px solid black;
            color: rgb(23, 22, 22);
            padding: 10px 16px;
            cursor: pointer;
            margin-top: 15px;
            border-radius: 10px;
        }
  
        input {
            padding: 20px, 0;
            margin: 20px, 0;
        }
  
        input:hover {
            color: rgb(127, 216, 131);
            background-color: rgb(23, 22, 22);
            ;
        }
  
        p {
            color: #ffffff;
        }
  
        .row em {
            color: red;
            font-weight: bolder;
            font-style: italic;
        }
  
        .main .bold {
            font-style: italic;
        }
  
        .wrapper .predict {
            font-weight: bold;
  
        }
  
        .logo .title {
            font-size: 20px;
            margin-left: 30%;
  
        }
  
        .gfg {
            color: green;
            font-weight: bold;
        }
  
        .bold {
            font-size: 17px;
            font-weight: bold;
        }
    </style>
</head>
  
<body>
  
    <div class="wrapper">
        <div class="top-bar">
            <div class="logo">
                <br><br><br> <br><br><br><br><br>
  
                <span class="title"><strong> NOTE :- </strong> Gender</span><br>
                <span class="title"> Male: 0</span><br>
                <span class="title"> Female: 1</span><br></br>
                <br>
                <span class="title"><strong>NOTE :- </strong> Stream</span><br>
                <span class="title"> Electrical And Communication: 0</span><br>
                <span class="title"> Computer Science: 1</span><br>
                <span class="title"> Information Technology: 2</span><br>
                <span class="title"> Mechanical: 3</span><br>
                <span class="title"> Electrical: 4</span><br>
                <span class="title"> Civil: 5</span><br> </br> <br>
                <span class="title"> <strong> NOTE 1 :-</strong> </span><br>
                <span class="title">Hostel Live: 1</span><br>
                <span class="title">Hostel Not Live: 0</span><br>
  
            </div>
            <div class="menu-bar">
  
            </div>
  
  
        </div>
  
        <p><em>WELCOME TO THE PREDICT PAGE</em></P>
        <div align='center' class="main">
            <h1 class="gfg">GeeksForGeeks</h1>
            <h1>It's just a prediction; work hard ,success will be yours.????</h1> <br />
            <form action="result">
                <table>
                    <tr>
                        <td align="right"><span class="bold">Age:</span></td>
                        <td align="left"><input type="text" name="n1"></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="bold">Gender:</span></td>
                        <td align="left"><input type="text" name="n2"></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="bold">Stream:</span></td>
                        <td align="left"><input type="text" name="n3"></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="bold">Internships:</span></td>
                        <td align="left"><input type="text" name="n4"></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="bold">CGPA:</span></td>
                        <td align="left"><input type="text" name="n5"></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="bold">Hostal:</span></td>
                        <td align="left"><input type="text" name="n6"></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="bold">Histroy Of Backlog:</span></td>
                        <td align="left"><input type="text" name="n7"></td>
                    </tr>
  
                </table>
                <br>
                <input class="predict" type="submit" value="PREDICT">
            </form>
            <div class="row">
                <h2><em>{{result2}}</em></h2>
            </div>
        </div>
</body>
  
</html>


Output:

Macheine Learning Placement Prediction Project

Predict.html

urls.py

In this file, we need to add all paths and import the py file by Django and we call every template one by one using URLs functionality and connect all files using the path method.

Python3




# code
from django.contrib import admin
from django.urls import path
from . import views
  
urlpatterns = [
    path('admin/', admin.site.urls),
    path("", views.index),
    path("predict/", views.predict),
    path("predict/result", views.result),
]


setting .py 

In this file, we import os and connect templates to the folder by using the following type of path join code. For connecting the templates folder with the setting, Use this one code line in the templates section of the setting.py file like image, and import os by using the import os command.

‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)]

Setting.py file 

views.py

This is the main file of our project in which we will use an ML logistic regression algorithm to predict student is placed or not for which first we import some libraries, use sklearn library to train our data, and use logistic regression we also imported logistic regression and also the accuracy_score.

#import all library
from django.shortcuts import render
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

Now we create three functions first for the home page, second for the display input form page, and third for the display result in which first we read our dataset by using pd.read, and then we train our dataset using train_ and train_Y and then we model our dataset and after that, we created 7 input using float and for fill student details and after that, we used an if and else condition to predict the Placement of students by observing the input values filled by students according to their study background.

Python3




from django.shortcuts import render
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import pandas as pd
  
  
def index(request):
    return render(request, "index.html")
  
  
def predict(request):
    return render(request, "predict.html")
  
  
def result(request):
    dataframe = pd.read_csv("collegePlace.csv")
    dataframe['Gender'].replace({'Male': 0,
                                 'Female': 1}, inplace=True)
    dataframe['Stream'].replace(
        {'Electronics And Communication': 0,
         'Computer Science': 1,
         'Information Technology': 2,
         'Mechanical': 3, 'Electrical': 4,
         'Civil': 5}, inplace=True)
  
    Y = dataframe["PlacedOrNot"]
    X = dataframe.drop(["PlacedOrNot"], axis=1)
  
    X_train, X_test, Y_train,
    Y_test = train_test_split(X, Y, test_size=0.1)
  
    model = LogisticRegression()
    model.fit(X_train, Y_train)
  
    val1 = float(request.GET['n1'])
    val2 = float(request.GET['n2'])
    val3 = float(request.GET['n3'])
    val4 = float(request.GET['n4'])
    val5 = float(request.GET['n5'])
    val6 = float(request.GET['n6'])
    val7 = float(request.GET['n7'])
  
    pred = model.predict([[val1, val2, val3,
                           val4, val5, val6, val7]])
  
    result1 = ""
    if pred == [0]:
        result1 = "You are not placed !????."
    else:
        result1 = "You are placed !????."
  
    return render(request, "predict.html",
                  {"result2": result1})


Output:

To run Django write the following command in the terminal then your prediction system will automatically display on your display screen.

python main.py runserver

Macheine Learning Placement Prediction Project

Placement Prediction Project

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads