<jupyter_start><jupyter_text>Let's check if dataset has any null/Nan values<jupyter_code>dataset.isnull().sum()
# ALL values are zero , that means no Nan value<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>dataset.hist(bins = 30 , figsize = (14,14) , color='b')<jupyter_output><empty_output><jupyter_text>As one can see , Standardhours , Over18 , EmployeeCount are same<jupyter_code>dataset.drop(['EmployeeCount','Over18','EmployeeNumber','StandardHours'],axis=1,inplace=True)
dataset.columns
correlation = dataset.corr()
f,ax = plt.subplots(figsize = (20,20))
sns.heatmap(correlation , annot = True)
plt.figure(figsize = (15,8))
sns.countplot(x = 'Age' , hue = 'Attrition' , data = dataset)
sns.boxplot(x='MonthlyIncome' , y = 'Gender' , data = dataset)
plt.figure(figsize=(15,10))
sns.boxplot(x='MonthlyIncome' , y ='JobRole' , data = dataset)<jupyter_output><empty_output><jupyter_text>Feature Engineering<jupyter_code>dataset.info()
# Convert Categorical features
X_cat = dataset[['BusinessTravel','Department','EducationField','Gender','JobRole','MaritalStatus']]
X_cat.head()
from sklearn.preprocessing import LabelEncoder
label = LabelEncoder()

X_cat_2 = pd.DataFrame()
for col in X_cat:
    X_cat_2[col] = label.fit_transform(X_cat[col])
X_cat_2.head()
X_numerical = dataset[['Age','DailyRate','DistanceFromHome','Education','EnvironmentSatisfaction','HourlyRate', 'JobInvolvement','JobLevel','JobSatisfaction','MonthlyIncome','MonthlyRate', 'NumCompaniesWorked', 'OverTime','PercentSalaryHike', 'PerformanceRating', 'RelationshipSatisfaction','StockOptionLevel', 'TotalWorkingYears', 'TrainingTimesLastYear','WorkLifeBalance', 'YearsAtCompany', 'YearsInCurrentRole','YearsSinceLastPromotion', 'YearsWithCurrManager']]
X_all = pd.concat([X_cat_2 , X_numerical] , axis=1)
X_all.head()
# Let's Normalize the data before feeding to neural network
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()

X = scaler.fit_transform(X_all)
y = dataset['Attrition']
y.head()
y.value_counts()<jupyter_output><empty_output><jupyter_text>As the dataset is imbalanced,let's balance it!<jupyter_code>from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler

over = SMOTE(sampling_strategy = 0.20)  # Let's increase the records of minority class by 25 percent
under = RandomUnderSampler(sampling_strategy = 0.5) # Decrease majority class upto ratio such that ratio is 40%
X_over ,y_over = over.fit_resample(X,y)
X_new ,y_new = under.fit_resample(X_over,y_over)
print(X_over.shape)
print(X.shape)
print(X_new.shape)<jupyter_output>(1479, 30)
(1470, 30)
(738, 30)<jupyter_text>Lets Build the models<jupyter_code># 1. Machine Learning Approach
#Splitting data into training and testing
from sklearn.model_selection import train_test_split
X_train,X_test , y_train,y_test = train_test_split(X_new,y_new)
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline , Pipeline
classifier = RandomForestClassifier()
pipe = Pipeline([('classifier',classifier)])
params = {'classifier__n_estimators':[50,100 , 10] , 'classifier__max_depth':[1,3,5]}
model = GridSearchCV(pipe , param_grid=params , cv=3)
model.fit(X_train,y_train)
model.best_params_
classifer = RandomForestClassifier(max_depth=5 , n_estimators = 50)
y_pred = model.predict(X_test)
from sklearn.metrics import classification_report , confusion_matrix
cm = confusion_matrix(y_test , y_pred)
print(cm)
print(classification_report(y_test,y_pred))
# Deep Learning Model
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units = 128,activation='relu',input_shape = (X_new.shape[1], )))
model.add(Dense(units = 128 , activation = 'relu'))
model.add(Dense(units = 1 , activation = 'sigmoid'))
model.summary()
model.compile(optimizer='Adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
history = model.fit(X_new , y_new , epochs = 40 , batch_size = 32)
y_pred_2 = model.predict(X_test)
y_pred_2 = (y_pred_2 > 0.5)
plt.plot(history.history['loss'])
cm = confusion_matrix(y_test ,y_pred_2)
print(cm)<jupyter_output>[[125   0]
 [  2  58]]
{'fraction_non_alphanumeric': 0.13117099928452183, 'fraction_numerical': 0.02217982351538278, 'mean_word_length': 8.510204081632653, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '193682', 'license': 'MIT', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1448, 'n_words': 281, 'path': 'Projects/Python/Machine_Learning_Project/Churn_rate_prediction/Solution_2.ipynb', 'repo_name': 'siddhantmh4/hacktoberfest2020', 'size': '4193'}