text_plain_output_1.png
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve,confusion_matrix, f1_score

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

df = pd.read_csv('../input/ibm-hr-analytics-attrition-dataset/WA_Fn-UseC_-HR-Employee-Attrition.csv')

df = df.drop(['EmployeeNumber', 'EmployeeCount', 'StandardHours'], axis=1)
df.isna().sum()

df.isnull().values.any()

categorial_col = df.select_dtypes(include='object')

import matplotlib.pyplot as plt
import seaborn as sns

df['Attrition'] = df.Attrition.astype('category').cat.codes
df.Attrition.value_counts()

from sklearn.model_selection import train_test_split
X = df.drop('Attrition', axis=1)
y = df.Attrition
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
X_train.shape

from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
dtc.fit(X_train, y_train)

from sklearn.model_selection import GridSearchCV
params = {'criterion': ('gini', 'entropy'), 'splitter': ('best', 'random'), 'max_depth': list(range(1, 20)), 'min_samples_split': [2, 3, 4], 'min_samples_leaf': list(range(1, 20))}
dtc = DecisionTreeClassifier(random_state=42)
tree_cv = GridSearchCV(dtc, params, scoring='accuracy', n_jobs=-1, verbose=1, cv=3)
tree_cv.fit(X_train, y_train)
best_params = tree_cv.best_params_
dtc_best = DecisionTreeClassifier(**best_params)
dtc_best.fit(X_train, y_train)

y_train_pred = dtc_best.predict(X_train)
y_train_prob = dtc_best.predict_proba(X_train)[0, 1]
y_test_pred = dtc_best.predict(X_test)
y_test_prob = dtc_best.predict_proba(X_test)[:, 1]
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve, confusion_matrix, f1_score
accuracy_score(y_train, y_train_pred)

roc_auc_score(y_test, y_test_prob)