<jupyter_start><jupyter_text>Supervised Learning* When we have the training data where each instance hasan input (a set of attributes) and a desired output (a target class)* Supervised learning methods are nowadays a standard tool in a wide rangeof disciplines, from medical diagnosis to natural language processing, imagerecognition, and searching for new particles at the Large Hadron Collider (LHC).   1. Image recognition with Support Vector Machines* Imagine that the instances in your dataset are points in a multidimensional space;we can assume that the model built by our classifier can be a surface or using linearalgebra terminology, a hyperplane that separates instances (points) of one classfrom the rest. Support Vector Machines (SVM) are supervised learning methodsthat try to obtain these hyperplanes in an optimal way, by selecting the ones thatpass through the widest possible gaps between instances of different classes. Newinstances will be classified as belonging to a certain category based on which side ofthe surfaces they fall on.<jupyter_code>import sklearn as sk
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces
import pandas as pd
faces = fetch_olivetti_faces()
pd.DataFrame(faces.data)[:2]
print(faces.DESCR)
faces.keys()
faces.data.shape
faces.images.shape
faces.target.shape
faces.target[:2]
def print_faces(images, target, top_n = 20):
    fig = plt.figure(figsize=(10, 10))
    fig.subplots_adjust(left=0, right=1, bottom =0, top =1,
                       hspace = .05, wspace = .5)
    for i in range(top_n):
        p = fig.add_subplot(10, 10, i+1, xticks=[], yticks=[])
        p.imshow(images[i], cmap=plt.cm.bone)
        
        p.text(0,14, str(target[i]))
        p.text(0, 60, str(i))
print_faces(faces.images, faces.target, 30)<jupyter_output><empty_output><jupyter_text>Training a Support Vector Machine> To use SVM in scikit-learn to solve our task, we will import the SVC class from thesklearn.svm module:<jupyter_code>from sklearn.svm import SVC<jupyter_output><empty_output><jupyter_text>> The Support Vector Classifier (SVC) will be used for classification<jupyter_code>svc_1 = SVC(kernel ='linear')
from sklearn.model_selection import train_test_split, cross_val_score, KFold
X_train, X_test, y_train, y_test = train_test_split(
 faces.data, faces.target, test_size=0.25, random_state=33)
from scipy.stats import sem
def evaluate_cross_validation(clf, X, y, K):
    cv = KFold(n_splits=K,shuffle=True, random_state=0)
    scores = cross_val_score(clf, X, y, cv=cv)
    print(scores)
    print ("Mean score: {0:.3f} (+/-{1:.3f})".format(
 np.mean(scores), sem(scores)))
evaluate_cross_validation(svc_1, X_train, y_train, 5)<jupyter_output>[0.96666667 0.96666667 0.95       0.88333333 0.88333333]
Mean score: 0.930 (+/-0.019)
{'fraction_non_alphanumeric': 0.09058988764044944, 'fraction_numerical': 0.03125, 'mean_word_length': 4.947807933194155, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '156039', 'license': 'MIT', 'n_tokens_mistral': 966, 'n_tokens_neox': 891, 'n_words': 314, 'path': 'Supervised Learning.ipynb', 'repo_name': 'CrispenGari/Machine-Leaning-Scikit-learn', 'size': '2848'}