text_plain_output_1.png
from scipy import interp

from sklearn.metrics import confusion_matrix

from sklearn.metrics import roc_curve, auc

from sklearn.model_selection import KFold

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

import matplotlib.pyplot as plt

import numpy as np # linear algebra

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

import seaborn as sns

employees.shape

employees.mean()

import seaborn as sns
correlation_matrix = employees.corr()

employees['salary'] = pd.factorize(employees['salary'])[0]

employees['sales'] = pd.factorize(employees['sales'])[0]

leave_result = employees['left']
y = np.where(leave_result == 1, 1, 0)
y

X = employees.drop('left', axis=1).as_matrix().astype(np.float)
X

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)

from sklearn.model_selection import KFold
def run_cv(X, y, clf_class, **kwargs):
    kf = KFold(n_splits=3, shuffle=True)
    y_pred = y.copy()
    for train_index, test_index in kf.split(X):
        X_train, X_test = (X[train_index], X[test_index])
        y_train = y[train_index]
        clf = clf_class(**kwargs)
        clf.fit(X_train, y_train)
        y_pred[test_index] = clf.predict(X_test)
    return y_pred

from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier as RF
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.linear_model import LogisticRegression as LR
from sklearn.ensemble import GradientBoostingClassifier as GBC
from sklearn.metrics import average_precision_score
def accuracy(y_true, y_pred):
    return np.mean(y_true == y_pred)

from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score

def draw_confusion_matrices(confusion_matricies, class_names):
    fig = plt.figure()
    class_names = class_names.tolist()
    for cm in confusion_matrices:
        classifier, cm, pos = cm[0], cm[1], cm[2]
        print("\n%s" % classifier)
        print(cm)
        
        ax = fig.add_subplot(pos, 
                             title = 'Confusion matrix for %s' % classifier, 
                             xlabel = 'Predicted',
                             ylabel = 'True')
        cax = ax.matshow(cm)
        plt.title('Confusion matrix for %s' % classifier)
        fig.colorbar(cax)
        ax.set_xticklabels([''] + class_names)
        ax.set_yticklabels([''] + class_names)
    plt.tight_layout()
    plt.show()

y = np.array(y)
class_names = np.unique(y)

confusion_matrices = [
    ( "Support Vector Machines", confusion_matrix(y, run_cv(X, y, SVC)), 321 ),
    ( "Random Forest", confusion_matrix(y, run_cv(X, y, RF)), 322 ),
    ( "K-Nearest-Neighbors", confusion_matrix(y, run_cv(X, y, KNN)), 323 ),
    ( "Gradient Boosting Classifier", confusion_matrix(y, run_cv(X, y, GBC)), 324 ),
    ( "Logisitic Regression", confusion_matrix(y, run_cv(X, y, LR)), 325 )
]

draw_confusion_matrices(confusion_matrices, class_names)

from sklearn.metrics import roc_curve, auc
from scipy import interp
def plot_roc(X, y, clf_class, clf_name, **kwargs):
    kf = KFold(n_splits=3, shuffle=True)
    y_prob = np.zeros((len(y), 2))
    mean_tpr = 0.0
    mean_fpr = np.linspace(0, 1, 100)
    all_tpr = []
    for i, (train_index, test_index) in enumerate(kf.split(X)):
        X_train, X_test = (X[train_index], X[test_index])
        y_train = y[train_index]
        clf = clf_class(**kwargs)
        clf.fit(X_train, y_train)
        y_prob[test_index] = clf.predict_proba(X_test)
        fpr, tpr, thresholds = roc_curve(y[test_index], y_prob[test_index, 1])
        mean_tpr += interp(mean_fpr, fpr, tpr)
        mean_tpr[0] = 0.0
        roc_auc = auc(fpr, tpr)
    mean_tpr /= kf.get_n_splits(X)
    mean_tpr[-1] = 1.0
    mean_auc = auc(mean_fpr, mean_tpr)
    plt.plot(mean_fpr, mean_tpr, lw=2, label='%s (area = %0.3f)' % (clf_name, mean_auc))
def plot_all(title, max_x, min_y):
    plot_roc(X, y, SVC, 'Support vector machines', probability=True)
    plot_roc(X, y, RF, 'Random forests', n_estimators=18)
    plot_roc(X, y, KNN, 'K-nearest-neighbors')
    plot_roc(X, y, GBC, 'Gradient Boosting Classifier')
    plt.plot([-0.05, max_x], [min_y, 1.05], '--', color=(0.6, 0.6, 0.6), label='Random')
    plt.xlim(-0.05, max_x)
    plt.ylim(min_y, 1.05)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title(title)
    plt.legend(loc='lower right')
    plt.show()
plt.figure(1)
plot_all('Receiver operating characteristic', 1.05, -0.05)
plt.figure(2)
plot_all('ROC (zoomed in at top left corner)', 0.6, 0.7)