/Employee_Attrition_Prediction

Machine Learning project - CMP2024 - Computer Engineering - Cairo University

Primary LanguageJupyter Notebook

Employee Attrition Prediction 🔮🏃‍♂️

🙇‍♂️ Problem definition and motivation

Employee attrition, or the rate at which employees leave a company, is a significant concern for organizations due to the high costs associated with hiring and training new employees. According to recent data, the average cost per hire rose to $4,700 in 2023. For specialized positions such as cybersecurity, engineering, or nursing, the cost per hire can be even higher, reaching up to $28,329 for executive positions. These costs, combined with factors such as ultra-low unemployment rates and an aging workforce, highlight the importance of predicting and mitigating employee attrition. (source).

By developing a machine learning model that can predict employee attrition, organizations can take proactive measures to retain valuable talent and reduce turnover costs. This project aims to leverage machine learning techniques to analyze factors such as job satisfaction, salary, work-life balance, etc., and predict which employees are most likely to leave the company. By doing so, organizations can optimize their hiring and retention strategies, ultimately reducing the financial burden of employee turnover.

🗂️ Dataset

We used the publicly available dataset on employee attrition: the IBM HR Analytics Employee Attrition & Performance dataset, which contains information about employees' demographics, job role, satisfaction levels, etc.
Link

❔ Evaluation metrics

The performance of our machine learning model will be evaluated using metrics such as accuracy, precision, recall, and F1-score.
Given that the classes are imbalanced, we will use weighted f1-score as our main metric of evaluation.

🧐 Data Exploring

(1) Dataset has 1470 rows and 35 columns.
(2) Plot histogram for each numeric variable/feature of the dataset. Histograms Image

(3) Check for nulls & duplicates Total number of duplicates : 0, Total number of missing values : 0
(4) Get the number of unique values for each column.
(5) Explore categorical featuresׂ Total number of categorical variable : 8
(6) Explore outliers Print number of outliers in each column.
(7) Show Correlation Between the target variables and each feature Correlation matrix Image (8) Confusion matrix:
Confusion matrix Image (9) Check data imbalance.
Attrition Balance Image

🔧 Preprocessing

1- Drop ('EmployeeCount', 'Over18', 'StandardHours') columns as they were found to have constant values for all 1470 rows. Also, 'EmployeeNumber' is a unique identifier for all 1470 rows.
2- Encode categorical variables.
3- Remove MonthlyIncome,TotalWorkingYears, YearsInCurrentRole and YearsWithCurrManager taking a cutoff of 0.7 correlation coefficient. This will retain JobLevel and YearsAtCompany and remove possibility of multicollinearity from the features.
4- Scale the date.
5- Split The Data into training, validation, and testing sets.
6- Resampling.
After resampling Image

🔩 Models

Zero-R model (baseline model):

Zero-R

AdaBoost

Performing grid search to get the best parameters:

Grid Search

Parameters of Best Model:

Best Model

Evaluation metrics on Validation Set:

evaluation_metrics

Classification report for validation set:

Classifiction report

Confusion Matrix for validation set:

Classifiction report

Learning Curve (Scores):

Classifiction report

Learning Curve (errors):

Classifiction report

AdaBoost Learning Curve Insights:

• The decreasing training score suggests that as more training examples are provided, the model is exposed to a wider variety of instances and is learning to generalize better.

• This is a positive sign as it indicates that the model is not memorizing the training data but rather learning meaningful patterns.

• The increasing validation score indicates that the model's performance on unseen data is improving as more training examples are provided.

• This suggests that the model is generalizing well to new instances and is not overfitting to the training data.

• The small gap between the training and validation scores suggests that the model is not suffering from significant overfitting.

• The fact that both scores are increasing with a small gap indicates that the model is learning to generalize well to unseen data without excessively fitting to the training data.

• The fact that both the training and validation scores are eventually increasing slowly that the model's performance is stabilizing as more data is provided.

• This is a desirable outcome, indicating that additional data may not significantly improve the model's performance further.

Training & Validation errors vs number of learning estimators:

Classifiction report

• Boosting is often robust to overfitting.

• Test set error decreases even after training error is almost zero

After Training over all the data (training + validation):

Classifiction report

Classification Report for Test Set:

Classifiction report

Confusion Matrix for Testset:

Classifiction report


Logistic Regression

Performing grid search to get the best parameters:

Grid Search

Parameters of Best Model:

Best Model

Evaluation metrics on Validation Set:

evaluation_metrics

Classification report for validation set:

Classifiction report

Confusion Matrix for validation set:

Classifiction report

Learning Curve (Scores):

Classifiction report

Learning Curve (errors):

Classifiction report

LogisticRegression Learning Curve Insights:

• The decreasing training score suggests that as more training examples are provided, the model is exposed to a wider variety of instances and is learning to generalize better. This is a positive sign as it indicates that the model is not memorizing the training data but rather learning meaningful patterns.

• The increasing validation score indicates that the model's performance on unseen data is improving as more training examples are provided. This suggests that the model is generalizing well to new instances and is not overfitting to the training data.

• The small gap between the training and validation scores suggests that the model is not suffering from significant overfitting. The fact that both scores are increasing with a small gap indicates that the model is learning to generalize well to unseen data without excessively fitting to the training data.

After Training over all the data (training + validation):

Classifiction report

Classification Report for Test Set:

Classifiction report

Confusion Matrix for Testset:

Classifiction report


SVM

Performing grid search to get the best parameters:

Grid Search

Parameters of Best Model:

Best Model

Evaluation metrics on Validation Set:

evaluation_metrics

Classification report for validation set:

Classifiction report

Gamma vs Error:

Classifiction report

Changing the gamma parameter does not significantly affect the model's performance on either the training or validation data.

C vs Error:

Classifiction report

  • Initial Decrease: At lower values of C, both the training and validation errors decrease. This indicates that the model is underfitting.

  • Optimal C Value: There is a point where the validation error reaches its minimum value, indicating the best regularization parameter (C) for the model where the model achieves the best balance between bias and variance, leading to optimal generalization to unseen data.

  • Increase after Optimal C: Beyond the optimal C value, both the training and validation errors start to increase. This is because higher values of C lead to overfitting, where the model becomes too complex.

Learning Curve(scores):

Classifiction report

Learning Curve(errors):

Classifiction report

  • The decreasing validation score indicates that the model's performance on unseen data is not improving as more training examples are provided.

  • This suggests that the model is not generalizing well to new instances and is not overfitting to the training data.

  • The gap between the training and validation scores is not small which suggests that the model is suffering from overfitting.

    Overall, the learning curve suggests that the model's performance improves with more training examples up to a certain point, after which further increases in the training set size may lead to overfitting. It highlights the importance of balancing model complexity and dataset size for optimal performance.

  • Because of this overfitting i started to perform regularization by choosing the optimal value for C.

We plotted the C vs Error again but this time we appended smaller values to the C list. Then We plotted the Learning curve again to ensure that the overfitting is solved.

C vs Error:

Classifiction report

Optimal C value=10

Learning Curve after regularization:

Classifiction report

Overfitting Solved.

Evaluation metrics After Regularization:

Classifiction report

Classification report for validation set after regularization:

Classifiction report

Confusion Matrix for validation set After Regularization:

Classifiction report

Metrics on test set:

Classifiction report

Classification Report on test set:

Classifiction report

Confusion Matrix for Test set:

Classifiction report


Linear SVM:

Performing grid search to get the best parameters:

Classifiction report

Parameters of Best Model:

Classifiction report

Evaluation metrics on validation sets:

Classifiction report

Classification report for validation set:

Classifiction report

Confusion Matrix for validation set:

Classifiction report

C vs Error:

Classifiction report

  • Initial Decrease: At lower values of C, both the training and validation errors decrease. This indicates that the model is underfitting.

  • Optimal C Value: There is a point where the validation error reaches its minimum value, indicating the best regularization parameter(C)for the model where the model achieves the best balance between bias and variance, leading to optimal generalization.

  • Increase after Optimal C: Both the training and validation errors start to increase. This is because higher values of C lead to overfitting, where the model becomes too complex.

Learning Curve:

Classifiction report

The model initially struggles to generalize well to the validation set, as indicated by the increasing training error and decreasing validation error. However, as the training set size increases, the model begins to generalize better, leading to both errors stabilizing. This indicates that the model has learned the underlying patterns in the data and is not overfitting.

Evaluation metrics on test sets:

Classifiction report

Classification report for test set:

Classifiction report

Confusion Matrix for test set:

Classifiction report

Models Results:

Classifiction report