People Analytics with Attrition Predictions | by Natasha Sharma | Towards Data Science

Every year a lot of companies hire a number of employees. The companies invest time and money in training those employees, not just this but there are training programs within the companies for their existing employees as well. The aim of these programs is to increase the effectiveness of their employees. But where HR Analytics fit in this? and is it just about improving the performance of employees?
HR Analytics
Human resource analytics (HR analytics) is an area in the field of analytics that refers to applying analytic processes to the human resource department of an organization in the hope of improving employee performance and therefore getting a better return on investment. HR analytics does not just deal with gathering data on employee efficiency. Instead, it aims to provide insight into each process by gathering data and then using it to make relevant decisions about how to improve these processes.
Attrition in HR
Attrition in human resources refers to the gradual loss of employees over time. In general, relatively high attrition is problematic for companies. HR professionals often assume a leadership role in designing company compensation programs, work culture and motivation systems that help the organization retain top employees.
How does Attrition affect companies? and how does HR Analytics help in analyzing attrition? We will discuss the first question here and for the second question we will write the code and try to understand the process step by step.
Attrition affecting Companies
A major problem in high employee attrition is its cost to an organization. Job postings, hiring processes, paperwork and new hire training are some of the common expenses of losing employees and replacing them. Additionally, regular employee turnover prohibits your organization from increasing its collective knowledge base and experience over time. This is especially concerning if your business is customer facing, as customers often prefer to interact with familiar people. Errors and issues are more likely if you constantly have new workers.
Hope the basics made sense. Let’s move on to coding and try finding out how HR Analytics help in understanding attrition.
In order to start with exercise, I have used IBM HR Analytics Employee Attrition & Performance Dataset, which was downloaded from Kaggle. The dataset includes features like Age, Employee Role, Daily Rate, Job Satisfaction, Years At Company, Years In Current Role etc. For this exercise, we will try to study the factors that lead to employee attrition. This is a fictional data set created by IBM data scientists.
Let’s get start with the work.
#Load the datahr_data = pd.read_csv("HR-Employee-Attrition.csv")hr_data.head()
#Missing values checkhr_data.isnull().sum()
Luckily we don’t have any missing values and from the above HR data snapshot it also looks like we don’t need to format the data.
Let’s have a look at the data and see how features are contributing in the data and in attrition of employees. We need to first check the data type of the features, why? Because we can only see the distribution of numerical/continuous values in a dataset. In order to take a peak into categorical/object values we have to bind them with a numeric variable and then you will be able to see their relevance to the dataset or you can replace the categorical variable with dummies.
#Check the structure of datasethr_data.dtypes
For this exercise, our aim is to predict the employee attrition and it is important to see which variables are contributing the most in attrition. But before that we need to know if the variables are correlated if they are, we might want to avoid those in model building process.
There are many continuous variables, we can have a look at their distribution and create a grid of pair plots but that would be too much code to see the correlation as there are a lot variables. Rather, we can create a seaborn heatmap of numeric variables and see the correlation. The variables which are not poorly correlated(i.e correlation value tend towards 0), we will pick those variables and move forward with them and will leave the ones which are strongly correlated(i.e correlation value tend towards be 1).
From the above heat map we can now see which variables are poorly correlated and which ones are strongly correlated.
#Let's remove the strongly correlated variableshr_data_uc = hr_data_num[['Age','DailyRate','DistanceFromHome',                        'EnvironmentSatisfaction', 'HourlyRate',                                            'JobInvolvement', 'JobLevel',                       'JobSatisfaction',                        'RelationshipSatisfaction',                        'StockOptionLevel',                        'TrainingTimesLastYear']].copy()hr_data_uc.head()
Stop for a sec and think what we don’t have it here- two things, One, Categorical variable and any information about attrition. Let’s combined those with above dataframe.
#Copy categorical datahr_data_cat = hr_data[['Attrition', 'BusinessTravel','Department',                       'EducationField','Gender','JobRole',                       'MaritalStatus',                       'Over18', 'OverTime']].copy()hr_data_cat.head()
Let’s first replace Yes and No in Attrition with 1 and 0.
Num_val = {'Yes':1, 'No':0}hr_data_cat['Attrition'] = hr_data_cat["Attrition"].apply(lambda x: Num_val[x])hr_data_cat.head()
Now replace other categorical variables with dummy values.
hr_data_cat = pd.get_dummies(hr_data_cat)hr_data_cat.head()
Now that we have all the data in numerical format, we can now combine hr_data_num and hr_data_cat.
hr_data_final = pd.concat([hr_data_num, hr_data_cat], axis=1)hr_data_final.head()
We have our final dataset. We now have to start modelling- Predicting the Attrition. Wait? Are you also confused like me? We already have the Attrition data then what is it here to predict? Well most of the time in Regression and classification problem, you run your model with the available values and check the metrics like accuracy of the model by comparing observed values with true values. If you won’t have the true values how would you know that the predictions are correct. Now you will realize that, how important the training data phase is. We train the model in a way that it can predict(almost) correct results.
In this dataset, we don’t have any missing values for Attrition, we will split the data into train and test. We will train the model on training data and predict the results on test data.
For this particular exercise we will use Random Forest Classifier. Before jumping into code, let’s get a little background about the RF classifier.
Random Forest Classifier
Number of weak estimators when combined forms strong estimator.
The Random forest works on Bagging principle. It is an ensemble of Decision Trees. The bagging method is used to increases the overall results by combining weak models. How does it combine the results? In the case of Classification problem it takes the mode of the classes, predicted in the bagging process.
I bet, a lot of you must be thinking, why did we choose the Random Forest for this particular problem? Well, to answer that, first let’s write the code.
Model building
from sklearn.cross_validation import train_test_splitfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.metrics import accuracy_scoretarget = hr_data_final['Attrition']features = hr_data_final.drop('Attrition', axis = 1)#create the train/test splitX_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.4, random_state=10)#Create the model and trainmodel = RandomForestClassifier()model.fit(X_train,y_train)#predict the results for testtest_pred = model.predict(X_test)#test the accuracyaccuracy_score(y_test, test_pred)
The model gave accuracy score of 0.84, not too bad. The random forest works quite well even with the default parameters. That’s one of reason we used RF for this problem.Though this can be improved by tuning hyper parameters of Random Forest classifier. Random forest also doesn’t over fit easily because of its randomness feature.
One of the best feature Random forest model has- it provides the importance of variables/features in the data/model. For this HR Analytics problem, we are interested in knowing which feature/factor contribute the most in the Attrition and RF’s one function can give us this information. This is just another reason why we have used RF.
feat_importances = pd.Series(model.feature_importances_, index=features.columns)feat_importances = feat_importances.nlargest(20)feat_importances.plot(kind='barh')
Looks like monthly income contribute the most in Attrition of employees.
Throughout this post, we saw Data is important in Human Resource department(actually in most of places it is important). We saw how we can avoid using correlated values and why it is important not to use those while modelling. We used Random forest and learned how it can be very advantageous over other available machine learning algorithm. Most of all we found factors which are most important to employees and if are not fulfilled might lead to Attrition.
GitHub RepoKaggle DatasetHR AttritionHR Analytics