|
import pickle |
|
import pandas as pd |
|
import shap |
|
from shap.plots._force_matplotlib import draw_additive_plot |
|
import gradio as gr |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
loaded_model = pickle.load(open("h22_xgb.pkl", 'rb')) |
|
|
|
|
|
explainer = shap.Explainer(loaded_model) |
|
|
|
|
|
def main_func(ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance): |
|
new_row = pd.DataFrame.from_dict({'ValueDiversity':ValueDiversity,'AdequateResources':AdequateResources, |
|
'Voice':Voice,'GrowthAdvancement':GrowthAdvancement,'Workload':Workload, |
|
'WorkLifeBalance':WorkLifeBalance}, orient = 'index').transpose() |
|
|
|
prob = loaded_model.predict_proba(new_row) |
|
|
|
shap_values = explainer(new_row) |
|
|
|
|
|
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False) |
|
|
|
plt.tight_layout() |
|
local_plot = plt.gcf() |
|
plt.close() |
|
|
|
return {"Leave": float(prob[0][0]), "Stay": 1-float(prob[0][0])}, local_plot |
|
|
|
|
|
title = "**Employee Turnover Predictor & Interpreter** 🪐" |
|
description1 = """ |
|
This app takes six inputs about employees' satisfaction with different aspects of their work (such as work-life balance, ...) and predicts whether the employee intends to stay with the employer or leave. There are two outputs from the app: 1- the predicted probability of stay or leave, 2- Shapley's force-plot which visualizes the extent to which each factor impacts the stay/ leave prediction.✨ |
|
""" |
|
|
|
description2 = """ |
|
To use the app, click on one of the examples, or adjust the values of the six employee satisfaction factors, and click on Analyze. 🤞 |
|
""" |
|
|
|
with gr.Blocks(title=title) as demo: |
|
gr.Markdown(f"## {title}") |
|
|
|
gr.Markdown(description1) |
|
gr.Markdown("""---""") |
|
gr.Markdown(description2) |
|
gr.Markdown("""---""") |
|
ValueDiversity = gr.Slider(label="ValueDiversity Score", minimum=1, maximum=5, value=4, step=1) |
|
AdequateResources = gr.Slider(label="AdequateResources Score", minimum=1, maximum=5, value=4, step=1) |
|
Voice = gr.Slider(label="Voice Score", minimum=1, maximum=5, value=4, step=1) |
|
GrowthAdvancement = gr.Slider(label="GrowthAdvancement Score", minimum=1, maximum=5, value=4, step=1) |
|
Workload = gr.Slider(label="Workload Score", minimum=1, maximum=5, value=4, step=1) |
|
WorkLifeBalance = gr.Slider(label="WorkLifeBalance Score", minimum=1, maximum=5, value=4, step=1) |
|
submit_btn = gr.Button("Analyze") |
|
|
|
with gr.Column(visible=True) as output_col: |
|
label = gr.Label(label = "Predicted Label") |
|
local_plot = gr.Plot(label = 'Shap:') |
|
|
|
submit_btn.click( |
|
main_func, |
|
[ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance], |
|
[label,local_plot], api_name="Employee_Turnover" |
|
) |
|
|
|
gr.Markdown("### Click on any of the examples below to see how it works:") |
|
gr.Examples([[4,4,4,4,5,5], [5,4,5,4,4,4]], [ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance], [label,local_plot], main_func, cache_examples=True) |
|
|
|
demo.launch() |