# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19"
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
# -

# EXPLORATORY DATA ANALYSIS

data_filepath="../input/ibm-hr-analytics-attrition-dataset/WA_Fn-UseC_-HR-Employee-Attrition.csv"
data=pd.read_csv(data_filepath)
data.head(10)

#checking missing value
data.isnull().any()
#output shows we have zero missing values

#check data types
data.dtypes

# +
include =['object', 'float', 'int']
  
data.describe(include = include)
# -

# **Testing Job Satisfaction**

data['JobSatisfaction'].value_counts()
#we see that 459 employees report a high rate of job satisfaction, while 289 report a low rate of job satisfaction

data.loc[data['JobSatisfaction']<=1,'LowJobSatisfaction'] = 'True'
#printing out the new column
print(data['LowJobSatisfaction'])

# From the crosstab below we can see a correlation between cases with a Low Job Satsifaction and monthly attrition

pd.crosstab(index=data['Attrition'], columns=data['LowJobSatisfaction'])

# We can plot this in a bar graph for easier visualization

# +
data_tab = pd.crosstab(index=data['Attrition'],columns = data['LowJobSatisfaction'])
data_tab.plot.bar()

#In the output we can see that low job satisfaction led to a number of employees leaving
# -

# Testing Job Satisfaction against other columns

jb =data.groupby('JobSatisfaction')
jb.mean()

# Employees with high jb have a higher average daily rate,higher average stock option level and less working hours on average
# These employees also have a higher job involvement

# COMPARISON WITH OTHER FACTORS
# Show a breakdown of distance from home by job role and attrition.

data_distance_dept = data[["Attrition", "Department","DistanceFromHome"]]
data_distance_dept

# +

#2. Compare average monthly income by education and attrition

data_education_income =data[["Attrition", "EducationField","MonthlyIncome"]]
data_education_income
# -

data.Attrition.value_counts()

data.EducationField.value_counts()

attrition_distance = sns.catplot(x="Attrition", y="JobInvolvement", data=data, kind="bar",palette="Set3")

# The output shows that employees with lower job involvement had a higher rate of attrition

g = sns.catplot(x="EducationField", y="MonthlyIncome", hue="Attrition", data=data, kind="bar",palette="Set3")
g.fig.set_size_inches(15,15)

# The marketing field had the highest rate of attrition

StarcoderdataJupyterScriptsDedupFiltered