text_html_output_1.png
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)
print('Logistic Regression:')
print('%.3f' % accuracy(y, run_cv(X, y, LR)))
print('Gradient Boosting Classifier')
print('%.3f' % accuracy(y, run_cv(X, y, GBC)))
print('Support vector machines:')
print('%.3f' % accuracy(y, run_cv(X, y, SVC)))
print('Random forest:')
print('%.3f' % accuracy(y, run_cv(X, y, RF)))
print('K-nearest-neighbors:')
print('%.3f' % accuracy(y, run_cv(X, y, KNN)))