Skip to main content

Project 01: Employee Attrition Prediction

1.8 - Frontend and Backend Application

It's time to use our model in web development. To use our model we have to export model to backend developer for that we used 'pickle' library.

✍️
Pickle library - used for serializing and deserializing Python objects. This is useful for saving the state of an object to a file or sending it over a network, and then later reconstructing the object.

To use our model in web development, lets serialize our model into file.

import pickle

with open("my_model_lr.pkl", "wb") as f:
    pickle.dump((model_lr, scaler, OrdinalEncoder), f)
✍️
Export or write our model to a file. Here we exported:
model_lr - Logistic model for prediciton
scaler - for scaling our new data features into single unit and
OrdinalEncoder - Exported this library to backend. So we need to make sure that our new data should also contains same number of columns to make our model work.

Used flask for creating both frontend application and backend.

Frontend Interface

Folder Structure of project using flask.

Folder Structure of my employee attrition project
Fig: Folder Structure of employee attrition project

In index.html, create a form consisting of - Age, Years at Company, Monthly Income, Work-Life Balance, Job Satisfaction etc.. features which is same as the features we represented in model.

Frontend Employee Attrition Application
Fig: Frontend Employee Attrition Predicition Application using Flask
Features and matrix (row, columns) should be similar to the one given in model, even order of features also should be same or else you will get an error saying "features order doesn't match".

Backend

Create a file app.py, and follow the code below

Import necessary libraries

import os
import pickle
import numpy as np
import pandas as pd
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS

Start the backend

# sart flask app
app = Flask(__name__)

# Cors - to remove cross-origin error request
CORS(app)

Load the model, scaler and ordinal-encoder

if os.path.isfile("D:/machine learning/basics/my_model_lr.pkl"):
    with open("D:/machine learning/basics/my_model_lr.pkl", "rb") as f:
        model_lr, scaler, OrdinalEncoder = pickle.load(f)
else:
    raise FileNotFoundError

Data preprocesing

1. ordinal encoding
columns_to_encode = ['Work-Life Balance', 'Job Satisfaction', 'Performance Rating', 'Education Level', 'Job Level', 'Company Size', 'Company Reputation', 'Employee Recognition']

categories=[
    ['Poor', 'Fair', 'Good', 'Excellent'],
    ['Low', 'Medium', 'High', 'Very High'],
    ['Low', 'Below Average', 'Average', 'High'],
    ["High School", "Bachelor’s Degree", "Master’s Degree", "Associate Degree", "PhD"],
    ['Entry', 'Mid', 'Senior'],
    ['Small', 'Medium', 'Large'],
    ['Poor', 'Fair', 'Good', 'Excellent'],
    ['Low', 'Medium', 'High', 'Very High'],
]
2. Numerical encoding
# define numerical encoder...
emp_bool_map = ['Overtime', 'Remote Work', 'Opportunities']

Define API route

Home page
@app.route('/')
def index():
    return render_template('index.html')
Predict Model page
@app.route('/predict', methods=['POST'])
def predict():
    # get requested json data from frontend
    data = request.get_json()

    # convert requested data to dataframe for further analysis
    X_new = pd.DataFrame([data])

    # apply encoding
    oe = OrdinalEncoder(categories=categories)
    X_new[columns_to_encode] = oe.fit_transform(X_new[columns_to_encode]).astype('int')

    for name in emp_bool_map:
        X_new[name] = X_new[name].map({'No': 0, 'Yes': 1})

    # Define the function to map income ranges to ordinal values
    def map_monthly_income(income):
        if 1200 <= income <= 5000:
            return 0
        elif 5001 <= income <= 10000:
            return 1
        elif 10001 <= income <= 15000:
            return 2
        elif 15001 <= income <= 20000:
            return 3
        elif income >= 20001:
            return 4
        else:
            return -1  # Handle any unexpected values

    X_new['Monthly Income'] = X_new['Monthly Income'].apply(map_monthly_income)

    # use standard scaler to scale the features
    features = scaler.transform(X_new)
    
    # predict the output
    y_pred = model_lr.predict(features)

    prediction = "Left" if y_pred[0] == 1 else "Stayed"

    return jsonify({'prediction': prediction})

Run the flask

if __name__=='__main__':
    app.run(debug=True)

To run the flask app type this command python app.py in command prompt.

Here is the outcome of the employee attrition application

Outcome of the Attrition
Fig: Outcome of the Attrition

To get this flask code, follow this link and for model code click this model-link.