(base) PS C:\dev\admission> python build_model.py Accuracy with train data : 0.81 Accuracy with test data : 0.76 (base) PS C:\dev\admission>
# Import required libraries import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import r2_score # Read data from admission.csv df = pd.read_csv("admission.csv") # Consider only 3 features - Gre, Toefl, and Cgpa # Chance is label X = df[['Gre','Toefl','Cgpa']] y = df['Chance'] # Split data into train and test X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=0) # Fit or Train the Model lr_model = LinearRegression() lr_model.fit(X_train,y_train) score = lr_model.score(X_train,y_train) print(f"Accuracy with train data : {score:0.2f}") # Evaluate Model using test data y_pred =lr_model.predict(X_test) # Find out accuracy with test data r2score = r2_score(y_test,y_pred) print(f"Accuracy with test data : {r2score:0.2f}") # Pickle model pd.to_pickle(lr_model,'lr_model.pickle')
import pandas as pd # Unpickle model model = pd.read_pickle('lr_model.pickle') # Take input from user gre = int(input("Enter GRE : ")) tof = int(input("Enter TOEFL : ")) cgpa = float(input("Enter CGPA : ")) # Predict chances result = model.predict([[gre,tof,cgpa]]) # input must be 2D array print(f"Chances are : {result[0] * 100:.2f}%")
(base) PS C:\dev\admission> python predict.py Enter GRE : 320 Enter TOEFL : 120 Enter CGPA : 8.5 Chances are : 76.03% (base) PS C:\dev\admission>
c:\dev\admission>conda install django
c:\dev\admission>django-admin startproject web
c:\dev\admission\web>python manage.py startapp admission
INSTALLED_APPS = [ 'admission', # Our Application 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
from django.shortcuts import render from django.http import HttpResponse import pandas as pd def admission_client(request): return render(request, 'admission.html') def predict_chances(request): # Receive data from client gre = int(request.GET['gre']) toefl = int(request.GET['toefl']) cgpa = float(request.GET['cgpa']) model = pd.read_pickle(r"c:\dev\admission\lr_model.pickle") chances = model.predict([[gre, toefl, cgpa]]) return HttpResponse(f"{chances[0] * 100:.2f}%")
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Predict Admission Chances</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> function predict_chances() { url = "http://localhost:8000/predict?gre=" + $("#gre_score").val() + "&toefl=" + $("#toefl_score").val() + "&cgpa=" + $("#cgpa").val(); // Make AJAX request $.get(url, null, function(result) { $("#chances").text(result); } ); } </script> </head> <body> <h1>Predict Admission Chances</h1> GRE Score <br/> <input type="number" id="gre_score"/> <p></p> TOEFL Score <br/> <input type="number" id="toefl_score"/> <p></p> CGPA <br/> <input type="number" id="cgpa"/> <p></p> <button onclick="predict_chances()">Predict Chances</button> <h2 id="chances"></h2> </body> </html>
from django.contrib import admin from django.urls import path from admission import views urlpatterns = [ path('admission/', views.admission_client), path('predict/', views.predict_chances) ]
c:\dev\admission\web>python manage.py runserver
http://localhost:8000/admission