@@ -0,0 +1,2455 @@
+import sklearn as sk%0Aimport numpy as np%0Aimport matplotlib.pyplot as plt%0Afrom sklearn.svm import SVC%0Afrom sklearn.cross_validation import train_test_split%0Afrom sklearn.cross_validation import cross_val_score, KFold%0Afrom scipy.stats import sem%0Afrom sklearn.datasets import fetch_olivetti_faces%0Afrom sklearn import metrics%0A%0Adef print_faces(images, target, top_n):%0A    '''%0A    helper function to plot the faces%0A    '''%0A    # set up the figure size in inches%0A    fig = plt.figure(figsize=(12, 12))%0A    fig.subplots_adjust(left=0, right=1, bottom=0, top=1,%0A       hspace=0.05, wspace=0.05)%0A    for i in range(top_n):%0A        # plot the images in a matrix of 10x10, so altogether can %0A        #plot 100 faces%0A        print i%0A        p = fig.add_subplot(10, 10, i + 1, xticks=%5B%5D,yticks=%5B%5D)%0A        p.imshow(images%5Bi%5D, cmap=plt.cm.bone)%0A        # label the image with the target value%0A        p.text(0, 14, str(target%5Bi%5D))%0A        p.text(0, 60, str(i))%0A        %0A    plt.show()%0A    %0Adef evaluate_cross_validation(clf, X, y, K):%0A    '''%0A    helper function to evaluate a classifier with cross validation%0A    '''%0A    # create a k-fold croos validation iterator%0A    cv = KFold(len(y), K, shuffle=True, random_state=0)%0A    # by default the score used is the one returned by score method of the estimator (accuracy)%0A    scores = cross_val_score(clf, X, y, cv=cv)%0A    print scores%0A    print (%22Mean score: %7B0:.3f%7D (+/-%7B1:.3f%7D)%22).format(np.mean(scores), sem(scores))%0A        %0Adef train_and_evaluate(clf, X_train, X_test, y_train, y_test):%0A    '''%0A    helper function to do the training and testing%0A    '''%0A    clf.fit(X_train, y_train)%0A    print %22Accuracy on training set:%22%0A    print clf.score(X_train, y_train)%0A    print %22Accuracy on testing set:%22%0A    print clf.score(X_test, y_test)%0A    y_pred = clf.predict(X_test)%0A    print %22Classification Report:%22%0A    print metrics.classification_report(y_test, y_pred)%0A    print %22Confusion Matrix:%22%0A    print metrics.confusion_matrix(y_test, y_pred)%0A%0A%0A#get the dataset%0Afaces = fetch_olivetti_faces()%0Aprint faces.DESCR%0A%0A#plot the faces%0Aprint_faces(faces.images, faces.target, 20)%0A%0A#split data%0AX_train, X_test, y_train, y_test = train_test_split(faces.data, faces.target, test_size=0.25, random_state=0)%0Asvc_1 = SVC(kernel='linear')%0A%0A#using cross validation to evaluate on the training data%0Aevaluate_cross_validation(svc_1, X_train, y_train, 5)%0A%0A#training and testing%0Atrain_and_evaluate(svc_1, X_train, X_test, y_train, y_test)%0A%0A%0A%0A%0A%0A%0A%0A

Add a script using svm to do the classification