## OPTIMIZE CLASSIFIER AND CALCULATE HIS ACCURACY(with categorical data)
def classifer_optimize_accuracy(X,Y,test_size,model,tuned_parameters,sscores = 'precision'):
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
# Split the dataset in two equal parts
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=test_size, random_state=0)
# define scores to be used
if sscores=='precision': scores = ['precision']
elif sscores=='recall': scores = ['recall']
elif sscores =='all': scores = ['precision', 'recall']
else: scores = ['precision']
# loop of scores
for score in scores:
print("##############################################")
print("# Tuning hyper-parameters for %s" % score)
print("##############################################\n")
print()
clf = GridSearchCV(model, tuned_parameters, cv=5,
scoring='%s_macro' % score)
clf.fit(X_train, y_train)
print("Best parameters set found on development set:")
print()
print(clf.best_params_)
print()
print("Grid scores on development set:")
print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.3f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print()
print("Detailed classification report:")
print()
print("The model is trained on the full development set.")
print("The scores are computed on the full evaluation set.")
print()
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
print()
# return
return None