<jupyter_start><jupyter_text>TakenMind Internship Project  Introduction I am <NAME>. This Notebook has been developed by me . Originally the Project Framework was PyCharm.  Problem Statement The data is for company X which is trying to control attrition. There are two sets of data: "Existing employees" and "Employees who have left". Following attributes are available for every employee:  Satisfaction Level  Last evaluation  Number of projects  Average monthly hours  Time spent at the company  Whether they have had a work accident  Whether they have had a promotion in the last 5 years  Departments (column sales)  Salary  Whether the employee has left    Objective What type of employees are leaving? Determine which employees are prone to leave next. Present your results with **accuracy scores** and with **various plots**.  Proof Of Concept  1.case study:  The dataset consist of 14999 observations of 11 attributes which are described below:  **Emp ID:** The employee ID given by the firm.  **satisfaction_level:** Relates to environment satisfaction level of the employee. Ranges from 0 to 1.  **last_evaluation:** Last evauation scores of the employee.  **number_project:** Number of projects done by the employee.  **average_montly_hours:** Working hours per month.  **time_spend_company:** how many companies he had work for.  **Work_accident:** If any work accident.  **promotion_last_5years:** Is there any promotion in last five years.  **Dept:** These are mainly sales, IT, technical, accounting, management, support, R&D, product_msg and marketing.   **Salary:** ranges from low, medium to high.  **Attrition:** whether the employee had left the company or not(No or yes).    2.Collecting and Data Analysing:  The data set was given in two different format:    **Employees existing** and **Employees left**  I merge up the two data set with adding an Attrition attribute to the set with the values yes or no.   I gone through the data to figure out how to build a model that will perfectly work to predict the attrition of employees leaving the firm.   I am using Logistic Regression for this Problem Statement.    3.Conclusions:  Employee attrition must be decreased for a firm as it increases the high training cost and the crucial business time of an organisation.  After going through the deep analysis I figure out some crucial conclusions.  That are given Below in the Data Analysis part.    Code Importing Libraries and data set.<jupyter_code>import pandas as pd
import numpy as np
import pylab as pl
import scipy.optimize as opt
from sklearn import preprocessing
import matplotlib.pyplot as plt
%matplotlib inline
# The code was removed by Watson Studio for sharing.
# The code was removed by Watson Studio for sharing.<jupyter_output><empty_output><jupyter_text>For using Logistic Regression I must convert category models to category codes.<jupyter_code>df['Attrition'] = df['Attrition'].astype('category')
df['dept'] = df['dept'].astype('category')
df['salary'] = df['salary'].astype('category')
df.info()

df['salary'] = df['salary'].cat.codes
df['dept'] = df['dept'].cat.codes
df['Attrition'] = df['Attrition'].cat.codes
df.head(20)<jupyter_output><empty_output><jupyter_text>Taking out necessary features and  describing them statistically.<jupyter_code>cdf = df[['satisfaction_level','last_evaluation','number_project','average_montly_hours','time_spend_company','Work_accident','promotion_last_5years','dept','salary','Attrition']]
cdf.head(9)
cdf.describe()<jupyter_output><empty_output><jupyter_text>Analysis of Features As already seen **satisfaction_level,last_evaluation,average_monthly_hours** are continous discrete values, and rest are integer or categorical values.  Therefore, I will analyse them using **Voilin plots** and rest with **Pie charts**.    Pie charts:   1.Salary<jupyter_code>df_bar=cdf[11429:]
df_bar.shape
df_salary = df_bar.groupby('salary', axis=0).sum()
#df_salary.head()
df_salary['Attrition'].plot(kind='pie',
                            figsize=(5, 6),
                            autopct='%1.1f%%', 
                            startangle=90,     
                            shadow=True,            
                            )

plt.title('EMPLOYEE ATTRITION W.R.T SALARY')
plt.axis('equal') # Sets the pie chart to look like a circle.

plt.show()
df_salary.head()<jupyter_output><empty_output><jupyter_text>categories:  0:low  1:medium  2:high  It can be seen that Employees with **medium salaries** are prone to leave next. While low and high salaried Employees are less in comparison.    2.Time spend in Company:<jupyter_code>df_time = df_bar.groupby('time_spend_company', axis=0).sum()
df_time['Attrition'].plot(kind='pie',
                            figsize=(5, 6),
                            autopct='%1.1f%%', 
                            startangle=90,     
                            shadow=True,            
                            )

plt.title('EMPLOYEE ATTRITION W.R.T TIME SPEND IN COMPANY')
plt.axis('equal')

plt.show()
df_time.head()<jupyter_output><empty_output><jupyter_text>categories:  2,3,4,5 and 6 years  It can be seen that Employees with **medieval durations i.e.3,4 and 5 years** are prone to leave next. While low and high durations Employees i.e 2 and 6 years are less in comparison.  3.Promotion in last 5 years:<jupyter_code>df_prom = df_bar.groupby('promotion_last_5years', axis=0).sum()
df_prom['Attrition'].plot(kind='pie',
                            figsize=(5, 6),
                            autopct='%1.1f%%', 
                            startangle=90,     
                            shadow=True,            
                            )

plt.title('EMPLOYEE ATTRITION W.R.T PROMOTION IN LAST 5 YEARS')
plt.axis('equal')

plt.show()
df_prom.head()<jupyter_output><empty_output><jupyter_text>99.5% Employees who had left the firm were given late promotions.  4.Work Accident:<jupyter_code>df_workacc = df_bar.groupby('Work_accident', axis=0).sum()
df_workacc['Attrition'].plot(kind='pie',
                            figsize=(5, 6),
                            autopct='%1.1f%%', 
                            startangle=90,     
                            shadow=True,            
                            )

plt.title('EMPLOYEE ATTRITION W.R.T WORK ACCIDENT')
plt.axis('equal')

plt.show()
df_workacc.head()<jupyter_output><empty_output><jupyter_text>The more the no. of work acccidents the more the Employees had left.  5.Given No. of Projects:<jupyter_code>df_proj = df_bar.groupby('number_project', axis=0).sum()
df_proj['Attrition'].plot(kind='pie',
                            figsize=(5, 6),
                            autopct='%1.1f%%', 
                            startangle=90,     
                            shadow=True,            
                            )

plt.title('EMPLOYEE ATTRITION W.R.T NO. OF PROJECTS ASSIGNED')
plt.axis('equal')

plt.show()
df_proj.head(6)<jupyter_output><empty_output><jupyter_text>Most of the Employees who had left the firm were given less no. of projects.  6.Department:<jupyter_code>df_dept = df_bar.groupby('dept', axis=0).sum()
df_dept['Attrition'].plot(kind='pie',
                            figsize=(5, 6),
                            autopct='%1.1f%%', 
                            startangle=90,     
                            shadow=True,            
                            )

plt.title('EMPLOYEE ATTRITION W.R.T DEPT')
plt.axis('equal')

plt.show()
df_dept.head(10)<jupyter_output><empty_output><jupyter_text>Most of the Employees in the sales and technical department are more prone to leave the firm.  Voilin Plots Importing seaborn for Voilin plots and comparison is shown with two case where Attrition is **YES** and other is **NO**.  1.Satisfaction level:   Attrition is **YES**<jupyter_code>#for who left
import seaborn as sns
df_vol1=df_bar[['satisfaction_level']]
sns.violinplot(df_vol1).get_figure().savefig('violin1.png')
df_vol1.head()<jupyter_output><empty_output><jupyter_text>Medium satisfaction level i.e. within 0.26-0.6 Employees are more prone to leave. Attrition is **NO**<jupyter_code>#for who do not left
df_barc=cdf[:11428]
df_vol2=df_barc[['satisfaction_level']]
sns.violinplot(df_vol2).get_figure().savefig('violin2.png')
df_vol2.head()<jupyter_output><empty_output><jupyter_text>As it can be clearly seen more no. of present employees have higher satisfaction level.  2.Last Evaluation: Attrition is **yes**<jupyter_code>df_vol12=df_bar[['last_evaluation']]
sns.violinplot(df_vol12).get_figure().savefig('violin3.png')
df_vol12.head()<jupyter_output><empty_output><jupyter_text>These Employees had lesser last evaluation score, comparitively. More no. of Employees are in lesser score region. Attriton is **YES**<jupyter_code>df_vol22=df_barc[['last_evaluation']]
sns.violinplot(df_vol22).get_figure().savefig('violin4.png')
df_vol22.head()<jupyter_output><empty_output><jupyter_text>Employees with last evaluation scores higher are still working for the firm.  3.Average monthly hours: Attrition is **YES**<jupyter_code>df_vol13=df_bar[['average_montly_hours']]
sns.violinplot(df_vol13).get_figure().savefig('violin5.png')
df_vol13.head()<jupyter_output><empty_output><jupyter_text>Employees are spending compartively less time per month. Attrition is **NO**<jupyter_code>df_vol23=df_barc[['average_montly_hours']]
sns.violinplot(df_vol23).get_figure().savefig('violin6.png')
df_vol23.head()<jupyter_output><empty_output><jupyter_text>Employees spending more time per month are still working in the firm.  Building the Model Shuffling the data.<jupyter_code>df = cdf.sample(frac=1).reset_index(drop=True)
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Separating **features** and **target**.<jupyter_code>X=df.values[:,0:9]
y=df.values[:,9]<jupyter_output><empty_output><jupyter_text>Normalising the Data for the Model. Importing **sci-kit learn**.<jupyter_code>from sklearn import preprocessing
X = preprocessing.StandardScaler().fit(X).transform(X)
X[0:9]<jupyter_output><empty_output><jupyter_text>Splitting the train and test data by a ratio of **0.15**.<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.15, random_state=4)
print ('Train set:', X_train.shape,  y_train.shape)
print ('Test set:', X_test.shape,  y_test.shape)<jupyter_output>Train set: (12749, 9) (12749,)
Test set: (2250, 9) (2250,)<jupyter_text>Building the **Logistic Rgression Model**<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
LR = LogisticRegression(C=0.01, solver='liblinear').fit(X_train,y_train)
LR<jupyter_output><empty_output><jupyter_text>Predicting the Attrition values for test data.<jupyter_code>yhat = LR.predict(X_test)
yhat
yhat_prob = LR.predict_proba(X_test)
yhat_prob<jupyter_output><empty_output><jupyter_text>Creating a **Confusion Matrix**.<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix
import itertools
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
print(confusion_matrix(y_test, yhat, labels=[1,0]))
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, yhat, labels=[1,0])
np.set_printoptions(precision=2)


# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['ATTRITION=1','ATTRITION=0'],normalize= False,  title='Confusion matrix')<jupyter_output>Confusion matrix, without normalization
[[ 127  404]
 [ 130 1589]]<jupyter_text>As it can be seen the Model predicted **1589** values correct for Attrition=0 and **127** for Attrition=1 which are leading to confusion i.e. whose probability values are very close to each other.  Printing the classification Report.<jupyter_code>print (classification_report(y_test, yhat))<jupyter_output>precision    recall  f1-score   support

         0.0       0.79      0.94      0.86      1693
         1.0       0.57      0.26      0.35       557

   micro avg       0.77      0.77      0.77      2250
   macro avg       0.68      0.60      0.61      2250
weighted avg       0.74      0.77      0.73      2250<jupyter_text>As it can be seen that F1-score of the model is 0.73 i.e. the accuracy of the model is **73%**.<jupyter_code>from sklearn.metrics import log_loss
log_loss(y_test, yhat_prob)<jupyter_output><empty_output><jupyter_text>Another metric Log-loss suggest the error values predicted which is **44%**.  This means i am getting 44% uncertainity in my model...<jupyter_code>from sklearn.metrics import jaccard_similarity_score
jaccard_similarity_score(y_test, yhat)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10771363934904765, 'fraction_numerical': 0.021382179084871927, 'mean_word_length': 3.696367374914325, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '339174', 'license': 'MIT', 'n_tokens_mistral': 4549, 'n_tokens_neox': 4262, 'n_words': 1189, 'path': 'Employee_Attrition.ipynb', 'repo_name': 'SHIVANGIJOSHI/TakenMind_Internship_Project-Assignments', 'size': '13711'}