<jupyter_start><jupyter_text>Employee-Retention-Prediction  Importing the Libraries<jupyter_code>import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns
import missingno

%matplotlib inline

#Reading the dataframe

df = pd.read_csv("HR_comma_sep.csv")
df.head()
#checking the shape of the data 

df.shape
#Checking Statistical Overview of Data

df.describe(include='all')<jupyter_output><empty_output><jupyter_text>**We can see that only columns departement and salary has unique values,So lets explore them**<jupyter_code># Checking Unique values of Department

df['Department'].unique()
# Checking Unique values of Salary

df['salary'].unique()
# Plot graphic of missing values

missingno.matrix(df, figsize = (30,10))
plt.show()
#Checking with isnull() function 

df.isnull().any()
#Counting Unique Values of Department.

df['Department'].value_counts()
#Analysing Department Column

df.groupby('Department').sum()
#Getting Unique values of the Left column

df.groupby('left').count()['satisfaction_level']
# Comparision of who left the company based on Salary and Department they are working in.

fig,axis=plt.subplots(nrows=2,ncols=1,figsize=(12,10))
sns.countplot(x='salary',hue='left',data=df,ax=axis[0])
sns.countplot(x='Department',hue='left',data=df,ax=axis[1])
plt.show()
# Comparision of who left based on Satisfaction level

plt.figure(figsize=(7,8))
sns.barplot(x='left',y='satisfaction_level',data = df)
plt.show()
#Checking the correlation between the features

corr = df.corr(method='pearson')
corr
#Checking the correlation between the features using Heatmap

sns.heatmap(corr, annot=True)
plt.show()
#CONVERTING CATEGORICAL FEATURES TO NUMERICAL ONE

from sklearn.preprocessing import LabelEncoder

le=LabelEncoder()

sal_num=le.fit_transform(df['Department'])
dept = le.fit_transform(df['salary'])
df.head()
df['salary_num'] = sal_num
df['dept'] = dept
df.head()
df.drop(['Department','salary'],axis=1,inplace=True)
df.head()<jupyter_output><empty_output><jupyter_text>MODEL SELECTION<jupyter_code>df.head()
y=df['left']
y.head()
X=df.drop(['left'],axis=1)
X.head()
#Importing Essential Machine Learning Algorithms to test the efficiency

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings('ignore')
from sklearn.model_selection import cross_val_score


from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC 


X_train, X_test, y_train, y_test = train_test_split(
     X.as_matrix(), y.as_matrix(), test_size=0.3, random_state=42)




from sklearn.model_selection import KFold
kf = KFold(n_splits=3)
kf
# Checking the shape of Train and Test dataset 

print("The X training set shape = ",X_train.shape)
print("The X testing set shape = ",X_test.shape)
# K Nearest Neighbors(KNN)

knn = KNeighborsClassifier()
knn.fit(X_test,y_test)
#Accuracy of KNN

(knn.score(X_test,y_test)*100)
cross_val_score(KNeighborsClassifier(), X, y.values.ravel(),cv=3).mean()*100
#Support Vector Machine(SVM)

sv = SVC()
sv.fit(X_test,y_test)
#Accuracy of SVM

sv.score(X_test,y_test)*100
cross_val_score(SVC(gamma='auto'), X, y.values.ravel(),cv=3).mean()*100
# Decision Tree Classifier

dt=DecisionTreeClassifier(max_depth=4)
dt.fit(X_test,y_test)
y_pred=dt.predict(X_test)
#Accuracy of Decision Tree Classifier

dt.score(X_test,y_test)*100
cross_val_score(DecisionTreeClassifier(max_depth=4),X,y,cv=3).mean()*100<jupyter_output><empty_output><jupyter_text>SCORES OF THE ALGORITHMS  | ALGORITHM      | SCORE | :---        |    :----:   | KNN     |      92.17 %| SVM |          94.51 %| DECISION TREE |96.49 %  🌲🌳 **DECISION TREE** PERFORMS WELL ON THIS EMPLOYEE RETENTION DATASET 🌲🌳  Model Evaluation<jupyter_code>from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,y_pred)
# TP = 3376
# TN = 974
# FP = 52 (also known as Type 1 Error) 
# FN = 98 (also know as Type 2 Error)
#Accuracy = No. of correct predictions / Total predictions.

# (TP+TN)/(TP+TN+FP+FN)

from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
# Recall = TP/(TP+FN)
from sklearn.metrics import recall_score
recall_score(y_test, y_pred, average=None)
# Precision = TP/(TP+FP)
from sklearn.metrics import precision_score
precision_score(y_test, y_pred, average=None)
# F1 score
from sklearn.metrics import f1_score
f1_score(y_test, y_pred, average=None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12260365581810076, 'fraction_numerical': 0.016049933125278644, 'mean_word_length': 6.416528925619835, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '361459', 'license': 'MIT', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1601, 'n_words': 379, 'path': 'Project Guidance/Employee Retention Prediction/EMPLOYEE RETENTION.ipynb', 'repo_name': 'prathimacode-hub/GSSoC-2022', 'size': '4486'}