text_html_output_1.png
from sklearn import  neighbors, svm

from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier,\

from sklearn.linear_model import LogisticRegressionCV

from sklearn.model_selection import cross_val_score, train_test_split,\

from sklearn.naive_bayes import MultinomialNB

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

hr = pd.read_csv(DIR_DATA + '/HR_comma_sep.csv')

hr.sales = hr.sales.astype('category').cat.codes
hr.salary = hr.salary.astype('category').cat.codes

def predict_left(df, clf, test_size=0.2):
    X = df.drop(['left'], 1)
    y = df.left
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
    clf.fit(X_train, y_train)
    train_pred = clf.predict(X_train)
    test_pred = clf.predict(X_test)

classifiers = [RandomForestClassifier(n_jobs=-1), RandomForestClassifier(criterion='entropy', n_jobs=-1), svm.SVC(), LogisticRegressionCV(n_jobs=-1), AdaBoostClassifier(), GradientBoostingClassifier(), neighbors.KNeighborsClassifier(n_jobs=-1), MultinomialNB(class_prior=[76.19, 23.81])]
np.random.seed(0)
for i, clf in enumerate(classifiers):
    predict_left(hr, clf, test_size=0.4)

def cross_val_left(hr, clf, cv_folds=CV_FOLDS, drop=['left']):
    X = hr.drop(drop, 1)
    y = hr.left
    scores = cross_val_score(clf, X, y, cv=cv_folds, n_jobs=-1)

clf = RandomForestClassifier(n_estimators=500, n_jobs=-1)
X = hr.drop(['left'], 1)
y = hr.left
train_sizes, train_scores, valid_scores = learning_curve(clf, X, y, train_sizes=np.linspace(0.1, 1.0, 10), cv=5, n_jobs=-1)

train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(valid_scores, axis=1)
test_scores_std = np.std(valid_scores, axis=1)
train_sizes = np.linspace(0.1, 1.0, 10)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color='r')
plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color='g')

train_scores_mean = np.mean(train_scores, axis=1)[3:]
train_scores_std = np.std(train_scores, axis=1)[3:]
test_scores_mean = np.mean(valid_scores, axis=1)[3:]
test_scores_std = np.std(valid_scores, axis=1)[3:]
train_sizes = np.linspace(0.1, 1.0, 10)[3:]
plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color='r')
plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color='g')
plt.plot(train_sizes, train_scores_mean, 'o-', color='r', label='Training score')
plt.plot(train_sizes, test_scores_mean, 'o-', color='g', label='Cross-validation score')
plt.xlabel('Training examples')
plt.ylabel('Score')
plt.grid()
plt.legend(loc='best')
plt.show()