|
import streamlit as st |
|
import pickle |
|
import pandas as pd |
|
from catboost import CatBoostClassifier |
|
|
|
|
|
with open('model_and_key_components.pkl', 'rb') as file: |
|
saved_components = pickle.load(file) |
|
|
|
model = saved_components['model'] |
|
unique_values = saved_components['unique_values'] |
|
|
|
|
|
def main(): |
|
st.title("Employee Attrition Prediction App") |
|
st.sidebar.title("Model Settings") |
|
|
|
|
|
with st.sidebar.expander("View Unique Values"): |
|
st.write("Unique values for each feature:") |
|
for column, values in unique_values.items(): |
|
st.write(f"- {column}: {values}") |
|
|
|
|
|
st.write("Welcome to the Employee Attrition Prediction App!") |
|
st.write("This app helps HR practitioners predict employee attrition using a trained CatBoost model.") |
|
st.write("Please provide the following information to make a prediction:") |
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
|
|
with col1: |
|
age = st.number_input("Age", min_value=18, max_value=70) |
|
monthly_income = st.number_input("Monthly Income") |
|
num_companies_worked = st.number_input("Number of Companies Worked") |
|
percent_salary_hike = st.number_input("Percent Salary Hike", min_value=0, max_value=25) |
|
training_times_last_year = st.number_input("Training Times Last Year", min_value=0, max_value=6) |
|
|
|
|
|
with col2: |
|
department = st.selectbox("Department", ['Sales', 'Research & Development', 'Human Resources']) |
|
environment_satisfaction = st.selectbox("Environment Satisfaction", [1, 2, 3, 4]) |
|
job_role = st.selectbox("Job Role", ['Sales Executive', 'Research Scientist', 'Laboratory Technician', |
|
'Manufacturing Director', 'Healthcare Representative', 'Manager', |
|
'Sales Representative', 'Research Director', 'Human Resources']) |
|
job_satisfaction = st.selectbox("Job Satisfaction", [1, 2, 3, 4]) |
|
work_life_balance = st.selectbox("Work Life Balance", [1, 2, 3, 4]) |
|
|
|
|
|
with col3: |
|
over_time = st.checkbox("Over Time") |
|
relationship_satisfaction = st.selectbox("Relationship Satisfaction", [1, 2, 3, 4]) |
|
years_since_last_promotion = st.number_input("Years Since Last Promotion") |
|
years_with_curr_manager = st.number_input("Years With Current Manager") |
|
|
|
|
|
if st.button("Predict"): |
|
|
|
age = str(age) |
|
monthly_income = str(monthly_income) |
|
num_companies_worked = str(num_companies_worked) |
|
percent_salary_hike = str(percent_salary_hike) |
|
training_times_last_year = str(training_times_last_year) |
|
years_since_last_promotion = str(years_since_last_promotion) |
|
years_with_curr_manager = str(years_with_curr_manager) |
|
|
|
|
|
input_data = pd.DataFrame({ |
|
'Age': [age], |
|
'Department': [department], |
|
'EnvironmentSatisfaction': [environment_satisfaction], |
|
'JobRole': [job_role], |
|
'JobSatisfaction': [job_satisfaction], |
|
'MonthlyIncome': [monthly_income], |
|
'NumCompaniesWorked': [num_companies_worked], |
|
'OverTime': [over_time], |
|
'PercentSalaryHike': [percent_salary_hike], |
|
'RelationshipSatisfaction': [relationship_satisfaction], |
|
'TrainingTimesLastYear': [training_times_last_year], |
|
'WorkLifeBalance': [work_life_balance], |
|
'YearsSinceLastPromotion': [years_since_last_promotion], |
|
'YearsWithCurrManager': [years_with_curr_manager] |
|
}) |
|
|
|
|
|
input_data = input_data[['Age', 'Department', 'EnvironmentSatisfaction', 'JobRole', 'JobSatisfaction', |
|
'MonthlyIncome', 'NumCompaniesWorked', 'OverTime', 'PercentSalaryHike', |
|
'RelationshipSatisfaction', 'TrainingTimesLastYear', 'WorkLifeBalance', |
|
'YearsSinceLastPromotion', 'YearsWithCurrManager']] |
|
|
|
|
|
prediction = model.predict(input_data) |
|
probability = model.predict_proba(input_data)[:, 1] |
|
|
|
|
|
if prediction[0] == 0: |
|
st.success("Employee is predicted to stay (Attrition = No)") |
|
else: |
|
st.error("Employee is predicted to leave (Attrition = Yes)") |
|
|
|
st.subheader("Prediction Probability") |
|
st.write(f"The probability of the employee leaving is: {probability[0]*100:.2f}%") |
|
|
|
|
|
st.subheader("Suggestions for retaining the employee:") |
|
if job_role_satisfaction < 2: |
|
st.write("Enhance job satisfaction through job enrichment initiatives and regular assessments of job roles.") |
|
if years_since_last_promotion > 5: |
|
st.write("Implement a transparent promotion policy and provide opportunities for career advancement to employees who haven't been promoted recently.") |
|
if years_with_current_manager > 5: |
|
st.write("Offer opportunities for a change in reporting structure to prevent stagnation and promote growth.") |
|
if performance_rating < 3: |
|
st.write("Provide constructive feedback and support for employees with low performance ratings to improve their performance and morale.") |
|
if workload > 8: |
|
st.write("Implement workload balancing and stress management programs to support employee well-being and prevent burnout.") |
|
if career_development_opportunities == "Limited": |
|
st.write("Offer continuous learning and development opportunities to foster employee growth and engagement.") |
|
if feedback_mechanism == "Not Effective": |
|
st.write("Establish open channels for feedback and regular one-on-one meetings to address employee concerns and development needs.") |
|
if company_culture != "Aligned with Values": |
|
st.write("Foster a positive work environment aligned with organizational values to enhance employee retention.") |
|
if recognition_rewards == "Limited": |
|
st.write("Implement reward programs and appreciation initiatives to recognize and acknowledge employees' contributions.") |
|
|
|
|
|
st.write("Conduct exit interviews to gather feedback and identify areas for improvement in retention strategies.") |
|
|
|
if __name__ == "__main__": |
|
main() |