<jupyter_start><jupyter_text><jupyter_code>from __future__ import print_function
%matplotlib inline
import os
import warnings
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as image
import pandas as pd
import pandas_profiling
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (12,8)
hr = pd.read_csv('/content/employee_data.csv')
hr.head(10)
hr.shape
import sys
!{sys.executable} -m pip install -U pandas-profiling[notebook]
!jupyter nbextension enable --py widgetsnbextension
from pandas_profiling import ProfileReport
pd.crosstab(hr.salary, hr.quit).plot(kind='bar')
plt.title('Turnover Frequency on Salary Bracket')
plt.xlabel('salary')
plt.ylabel('Frequency of Turnover')
plt.show()
pd.crosstab(hr.department, hr.quit).plot(kind='bar')
plt.title('Turnover Frequency on Salary Department')
plt.xlabel('salary')
plt.ylabel('Frequency of Turnover')
plt.show()
cat_vars = ['department', 'salary']
for var in cat_vars:
  cat_list = pd.get_dummies(hr[var], prefix=var)
  hr = hr.join(cat_list)
hr.head()
hr.drop(columns=['department', 'salary'], axis = 1, inplace=True)
hr.head()
hr.shape
from yellowbrick.target import ClassBalance
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = [12,8]
visualizer = ClassBalance(labels=['stayed', 'quit']).fit(hr.quit)
visualizer.show()
X = hr.loc[:, hr.columns!='quit']
y = hr.quit
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.2, stratify=y)
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import export_graphviz # display the tree within a Jupyter notebook
from IPython.display import SVG
from graphviz import Source
from IPython.display import display
from ipywidgets import interactive, IntSlider, FloatSlider, interact
import ipywidgets
from IPython.display import Image
from subprocess import call
import matplotlib.image as mpimg
@interact
def plot_tree(
    crit=['gini', 'entropy'],
    split=['best', 'radom'],
    depth=IntSlider(min=1,max=30, value=2, continuous_update=False),
    min_split=IntSlider(min=2,max=5, value=2, continuous_update=False),
    min_leaf=IntSlider(min=1,max=5, value=1, continuous_update=False)):
  estimator = DecisionTreeClassifier(random_state=0,
                                     criterion=crit,
                                     splitter=split,
                                     max_depth=depth,
                                     min_samples_split=min_split,
                                     min_samples_leaf=min_leaf)
  estimator.fit(X_train, y_train)
  print('Decision Tree Training Accuracy: {:.3f}'.format(accuracy_score(y_train, estimator.predict(X_train))))
  print('Decision Tree Training Accuracy: {:.3f}'.format(accuracy_score(y_test, estimator.predict(X_test))))
  graph = Source(tree.export_graphviz(estimator, out_file=None, feature_names=X_train.columns, class_names=['stayed', 'quit'], filled=True))
  display(Image(data=graph.pipe(format='png')))
  return estimator
@interact
def plot_tree_rf(
    crit=['gini', 'entropy'],
    bootstrap=['True', 'False'],
    depth=IntSlider(min=1,max=30, value=2, continuous_update=False),
    forests=IntSlider(min=1,max=200, value=100, continuous_update=False),
    min_split=IntSlider(min=2,max=5, value=2, continuous_update=False),
    min_leaf=IntSlider(min=1,max=5, value=1, continuous_update=False)):
  estimator = RandomForestClassifier(random_state=0,
                                     criterion=crit,
                                     bootstrap=bootstrap,
                                     max_depth=depth,
                                     n_estimators=forests,
                                     n_jobs=-1,
                                     verbose=False,
                                     min_samples_split=min_split,
                                     min_samples_leaf=min_leaf)
  estimator.fit(X_train, y_train)
  print('Random Forests Training Accuracy: {:.3f}'.format(accuracy_score(y_train, estimator.predict(X_train))))
  print('Random Forests Training Accuracy: {:.3f}'.format(accuracy_score(y_test, estimator.predict(X_test))))
  num_tree = estimator.estimators_[0]
  print('\Visulaizing Tree:',0)
  graph = Source(tree.export_graphviz(num_tree,  out_file=None, feature_names=X_train.columns, class_names=['stayed', 'quit'], filled=True))
  display(Image(data=graph.pipe(format='png')))
  return estimator
from yellowbrick.classifier import ROCAUC
dt = DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
                       max_depth=2, max_features=None, max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, presort='deprecated',
                       random_state=0, splitter='best')
rf = RandomForestClassifier(bootstrap='True', ccp_alpha=0.0, class_weight=None,
                       criterion='gini', max_depth=2, max_features='auto',
                       max_leaf_nodes=None, max_samples=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=100,
                       n_jobs=-1, oob_score=False, random_state=0,
                       verbose=False, warm_start=False)
visualizer = ROCAUC(rf, classes=['stayed', 'quit'])
visualizer.fit(X_train, y_train)
visualizer.score(X_test, y_test)
visualizer.poof()
visualizer = ROCAUC(dt, classes=['stayed', 'quit'])
visualizer.fit(X_train, y_train)
visualizer.score(X_test, y_test)
visualizer.poof()<jupyter_output><empty_output>