<jupyter_start><jupyter_text>Created By :- <NAME> Email :- <EMAIL>  Summer Analytics 2020 Capstone Project Predicting Employee Attrition in the Dawn of Recession               As the COVID-19 keeps unleashing its havoc, the world continues to get pushed into the crisis of the great economic recession, more and more companies start to cut down their underperforming employees. Companies firing hundreds and thousands of Employees is a typical headline today. Cutting down employees or reducing an employee salary is a tough decision to take. It needs to be taken with utmost care as imprecision in the identification of employees whose performance is attriting may lead to sabotaging of both employees' career and the company's reputation in the market. Aim of The Competition    To predict Employee Attrition by the given data about his/her past history.<jupyter_code># importing required librery

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

pd.pandas.set_option('display.max_columns',None)<jupyter_output><empty_output><jupyter_text>importing data<jupyter_code>data = pd.read_csv("train.csv")
data_y = data.Attrition
data.head()<jupyter_output><empty_output><jupyter_text>Data description  Education 1) 'Below College' 2) 'College' 3) 'Bachelor' 4) 'Master' 5) 'Doctor' EnvironmentSatisfaction 1 'Low' 2 'Medium' 3 'High' 4 'Very High' JobInvolvement 1 'Low' 2 'Medium' 3 'High' 4 'Very High' JobSatisfaction 1 'Low' 2 'Medium' 3 'High' 4 'Very High'  PerformanceRating 1 'Low' 2 'Good' 3 'Excellent' 4 'Outstanding' Behaviour 1 'Good' 2 'Bad' 3 'Not Rated' CommunicationSkill 1 'Bad' 2 'Average' 3 'Good' 4 'Better' 5 'Best'  StockOptionLevel 0 'No stocks' 1 'Less Stocks' 2 'Moderate Stocks' 3 'A lot of Stocks'  Exploratory Data Analysis  Attrition of employee in different department<jupyter_code>df_Department = data.groupby('Attrition').Department.value_counts().unstack(0)
df_Department
df_Department.plot.bar(title='Attrition of employee in different department')<jupyter_output><empty_output><jupyter_text>Attrition of male and female<jupyter_code>df_gender = data.groupby('Gender').Attrition.value_counts().unstack(0)
df_gender
df_gender.plot.pie(subplots=True,autopct='%1.0f%%',title='Attrition of male and female')<jupyter_output><empty_output><jupyter_text>Attrition of employee according to education field<jupyter_code>df_educationfield = data.groupby('Attrition').EducationField.value_counts().unstack(0)
df_educationfield
df_educationfield.plot.bar(title='Attrition of employee according to education field')<jupyter_output><empty_output><jupyter_text>Attrition of employee according to degree<jupyter_code>df_degree = data.groupby('Attrition').Education.value_counts().unstack(0)
df_degree
df_degree.plot.bar(title='Attrition of employee according to degree')
plt.xticks([0,1,2,3,4],['below college','college','bechelor','master','doctor'])<jupyter_output><empty_output><jupyter_text>Attrition of employee according to it's StockOptionLevel<jupyter_code>df_StockOptionLevel = data.groupby('Attrition').StockOptionLevel.value_counts().unstack(0)
df_StockOptionLevel
df_StockOptionLevel.plot.bar(title='Attrition of employee according to StockOptionLevel')
plt.xticks([0,1,2,3,4],['no stock','less stock','moderate stock','A lot of stock'])<jupyter_output><empty_output><jupyter_text>Attrition of employee according to it's JobRole<jupyter_code>df_JobRole = data.groupby('Attrition').JobRole.value_counts().unstack(0)
df_JobRole
df_JobRole.plot.bar(title='Attrition of employee according to jobrole')<jupyter_output><empty_output><jupyter_text>Data Preparation<jupyter_code># import librery for lable encoding

from sklearn.preprocessing import LabelEncoder
le_busnesstravel = LabelEncoder()
le_Department = LabelEncoder()
le_EducationField = LabelEncoder()
le_Gender = LabelEncoder()
le_JobRole = LabelEncoder()
le_MaritalStatus = LabelEncoder()
le_OverTime = LabelEncoder()

data['busnesstravel_n'] = le_busnesstravel.fit_transform(data['BusinessTravel'])
data['Department_n'] = le_busnesstravel.fit_transform(data['Department'])
data['EducationField_n'] = le_busnesstravel.fit_transform(data['EducationField'])
data['Gender_n'] = le_busnesstravel.fit_transform(data['Gender'])
data['JobRole_n'] = le_busnesstravel.fit_transform(data['JobRole'])
data['MaritalStatus_n'] = le_busnesstravel.fit_transform(data['MaritalStatus'])
data['OverTime_n'] = le_busnesstravel.fit_transform(data['OverTime'])

data = data.drop(['BusinessTravel','Department','EducationField','Gender','JobRole','MaritalStatus','OverTime'],axis='columns')
data.head()<jupyter_output><empty_output><jupyter_text>now we rempve column which has no meaning in prediction. for example Id and employee number etc....<jupyter_code>data = data.drop(['Id','Attrition','EmployeeNumber','Behaviour'],axis='columns')

# we remove attrition column because it is target and we store it in data_y dataframe
data.dtypes<jupyter_output><empty_output><jupyter_text>Create Mechine Learning Model     we have classiffication problem to solve. we need to classify employee in Attrition (1,0) based on given feature.         there are some machine learning model which we can use for example logistic regression classifier, SVM classifier, decission tree,random forest.         decission tree and random forest are doing good for categorical data. in our data mostly all feature are catagorical. so we use any one from those two. random forest work better then decission tree and it has less verience so we used random forest for this classification.  split data in trainning and testing set<jupyter_code># train test split

from sklearn.model_selection import train_test_split as tts
x_train,x_test,y_train,y_test = tts(data,data_y,test_size=0.33,random_state=4)
x_train.shape
x_test.shape<jupyter_output><empty_output><jupyter_text>Creat Random Forest model from sklearn librery<jupyter_code>from sklearn.ensemble import RandomForestClassifier as RFS
model = RFS(random_state=24,n_estimators=500,max_depth=50)
model.fit(x_train,y_train)
model.score(data,data_y)
y_predict = model.predict(x_test)
from sklearn.metrics import roc_auc_score

roc_auc_score(y_test,y_predict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0983974358974359, 'fraction_numerical': 0.011217948717948718, 'mean_word_length': 7.814971751412429, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '520381', 'license': 'MIT', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1891, 'n_words': 536, 'path': 'Predicting Employee Attrition in the Dawn of Recession.ipynb', 'repo_name': 'faldujay/Predicting-Employee-Attrition-in-Dawn-of-Recession-', 'size': '6258'}