import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
train_data= pd.read_csv('../input/hr-analytics-analytics-vidya/train.csv')
test_data=pd.read_csv('../input/hr-analytics-analytics-vidya/test.csv')
train_data.head()
train_data.shape
test_data.shape
train_data.describe(include='all')
train_data.info()
#education and previous year rating has null values
train_data.isnull().any()
# Univariate Analysis
plt.rcParams['figure.figsize'] = (20, 10)
sns.countplot(train_data['department'], color='pink')
plt.title('Different departments in the company', fontsize=30)
plt.xlabel('Department names', fontsize=18)
plt.ylabel('Count', fontsize=18)
plt.show()
"""
department with max people is sales and marketing
"""
plt.rcParams['figure.figsize'] = (20, 10)
sns.countplot(train_data['region'], color = 'pink')
plt.title('Different Regions in the company', fontsize = 30)
plt.xticks(rotation = 60)
plt.xlabel('Region Code', fontsize = 15)
plt.ylabel('count', fontsize = 15)
plt.show()
"""
Highest number of people are from region 2
"""
plt.rcParams['figure.figsize'] = (10, 5)
sns.countplot(train_data['education'], color = 'pink')
plt.rcParams['figure.figsize'] = (10, 5)
sns.countplot(train_data['gender'], color = 'pink')
plt.rcParams['figure.figsize']=(10,7)
sns.countplot(train_data['recruitment_channel'],color='pink')
plt.title('Distribution of employees based on recruitment channel')
plt.xlabel('Source of recruitment')
plt.ylabel('Count')
plt.show()
train_data['no_of_trainings'].value_counts()
plt.rcParams['figure.figsize']=(20,10)
sns.countplot(train_data['no_of_trainings'],color='pink')
plt.title('Distribution of employees based on trainings they have done')
plt.xlabel('No. of Training')
plt.ylabel('Count')
plt.show()
sns.distplot(train_data['age'],color='red')
plt.title('Distribution of employees based on their age')
plt.xlabel('Age',fontsize=15)
plt.show()
plt.rcParams['figure.figsize']=(10,7)
sns.countplot(train_data['previous_year_rating'],color='pink')
plt.title('Distribution of Previous year rating of the Employees')
plt.xlabel('Rating')
plt.ylabel('Count')
plt.show()
sns.distplot(train_data['length_of_service'],color='red')
plt.title('Distribution of employees based on their service length')
plt.xlabel('Length of service in years',fontsize=15)
plt.show()
train_data['KPIs_met >80%'].value_counts()
size=(35517,19291)
colors=('pink','magenta')
labels=('KPIs not met','KPIs met')

plt.rcParams['figure.figsize']=(20,10)
plt.pie(size,labels=labels,colors=colors,autopct="%.2f%%",shadow=True)
plt.legend()
plt.show()
train_data['awards_won?'].value_counts()
size=[52538,1270]
colors=['pink','magenta']
label=['Awards not won','Awards won']

plt.rcParams['figure.figsize']=(20,10)
plt.pie(size,colors=colors,labels=label,shadow=True,autopct = '%.2f%%')
plt.title('Percentage of employees who won awards',fontsize=30)
plt.legend()
plt.show()
plt.rcParams['figure.figsize']=(20,10)
sns.distplot(train_data['avg_training_score'],color='green')
plt.title('Distribution of the average training score')
plt.xlabel('Average Training Score')
plt.ylabel('Count')
plt.show()
#checking o of employees promoted
train_data['is_promoted'].value_counts()
# % of people promoted
promoted= (4668/(50140+4668))
print(promoted*100,'%')
"""
## Bivariate Data Visualization
"""
data = pd.crosstab(train_data['department'], train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,10), color=['blue','pink'])

plt.title('Departments and promotion', fontsize = 30)
plt.xlabel('Different Departments of the Company', fontsize = 15)
plt.legend()
plt.show()
"""
### We can see that there is an equal development in each departments of the company
"""
data = pd.crosstab(train_data['education'], train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,10), color=['blue','pink'])

plt.title('Education and promotion', fontsize = 30)
plt.xlabel('Education level of employees', fontsize = 15)
plt.legend()
plt.show()
"""
### There is not a significant difference in promotion based on level of education 
"""
data = pd.crosstab(train_data['region'], train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,10), color=['blue','pink'])

plt.title('Region and promotion', fontsize = 30)
plt.xlabel('Different Regions of the Company', fontsize = 15)
plt.legend()
plt.show()
"""
### The above graph shows that there is no biasedness over regions in terms of Promotion as all the regions share promotions almost equally.
"""
data = pd.crosstab(train_data['gender'], train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,10), color=['blue','pink'])

plt.title('Gender and promotion', fontsize = 30)
plt.xlabel('Genders', fontsize = 15)
plt.legend()
plt.show()
"""
### Both the genders have equal chances for promotion
"""
data = pd.crosstab(train_data['recruitment_channel'], train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,10), color=['blue','pink'])

plt.title('Recritment Channel and promotion', fontsize = 30)
plt.xlabel('Recruitment Channels', fontsize = 15)
plt.legend()
plt.show()
"""
### There is no biasedness in promotion based on recruitment channel
"""
data =pd.crosstab(train_data['no_of_trainings'], train_data['is_promoted'])
data.div(data.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True, figsize=(20,9), color=['blue','pink'])

plt.title('No of trainings and promotion')
plt.xlabel('No of Trainings', fontsize=15)
plt.show()
"""
### We can see in the figure that with increasing no of training, promotion decreases but the correlation is not perfectly clear.
"""
data =pd.crosstab(train_data['age'], train_data['is_promoted'])
data.div(data.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True, figsize=(20,9), color=['blue','pink'])

plt.title('Age and promotion')
plt.xlabel('Age of employees', fontsize=15)
plt.show()
"""
### The company promotes employees of all the ages equally even the freshers have equal share of promotion and also the senior citizen employees are getting the equal share of Promotion in the Company
"""
data = pd.crosstab(train_data['previous_year_rating'], train_data['is_promoted'])
data.div(data.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True, figsize=(20,9),color=['blue','pink'])

plt.title('Previous year rating and promotion')
plt.xlabel('Previous year rating of employees', fontsize=15)
plt.show()
"""
### We can see that the chances of getting promoted increase significantly with better previous year rating.
"""
data = pd.crosstab(train_data['length_of_service'], train_data['is_promoted'])
data.div(data.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True, figsize=(20,9),color=['blue','pink'])

plt.title('Length of service and promotion')
plt.xlabel('Length of service of employees', fontsize=15)
plt.show()
"""
### Promotions are almost equal but employees with more than 32 years are more likely
"""
data =pd.crosstab(train_data['KPIs_met >80%'],train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,9), color=['blue','pink'])

plt.title('Dependency of KPIs in promotion', fontsize = 30)
plt.xlabel('KPIs Met or Not', fontsize = 15)
plt.legend()
plt.show()
"""
### It is clearly seen that employees with better KPIs have better chance at promotion
"""
data =pd.crosstab(train_data['avg_training_score'],train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(20,9), color=['blue','pink'])

plt.title('Dependency of Training Score in promotion', fontsize = 30)
plt.xlabel('Average Training Scores', fontsize = 15)
plt.legend()
plt.show()
"""
### The chances of promotion significantly increases with high Training Score.
"""
data = pd.crosstab(train_data['awards_won?'], train_data['is_promoted'])
data.div(data.sum(1).astype(float),axis=0).plot(kind='bar', stacked=True, figsize=(10,7), color=['blue','pink'])

plt.title('Dependency of awards won in promotion', fontsize = 30)
plt.xlabel('Awards won or not', fontsize = 15)
plt.legend()
plt.show()
"""
### There is a very good chance of getting promoted if the employee has won an award
"""