Example #1
0
svm_cv_scores.mean(), svm_cv_scores.std()

from sklearn.grid_search import GridSearchCV 

param_grid = {
	'C': [1e3, 5e3, 1e4, 1e5],
	'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1],
}

clf = GridSearchCV(svm, param_grid, scoring='f1', cv=cv, n_jobs=2)

clf = clf.fit(X_train_pca, y_train)

print("Best estimator found by randomized hyper parameter search:")
print(clf.best_params_) 
print("Best parameters validation score: {:.3f}".format(clf.best_score_))


X_test_pca = pca.transforom(X_test)
y_pred = clf.predict(X_test_pca)

def title(y_pred, y_test, target_names, i):
    pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
    true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
    return 'predicted: %s\ntrue:      %s' % (pred_name, true_name)

prediction_titles = [title(y_pred, y_test, names, i)
                     for i in range(y_pred.shape[0])]

plot_gallery(X_test, prediction_titles, h, w)
pl.show()