import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.decomposition import PCA
from sklearn.neural_network import MLPClassifier

lfw_dataset = fetch_lfw_people(min_faces_per_person=100)

print(lfw_dataset.images.shape)
_,h,w = lfw_dataset.images.shape

X=lfw_dataset.data
y=lfw_dataset.target

target_names = lfw_dataset.target_names
print(target_names)

#split into training and testing
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3)

#COmpute a PCA
components = 100
pca = PCA(n_components=components,whiten=True).fit(X_train)

#apply PCA transformation
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)

#training a neural network
print("Fitting the classifier to the training set")

clf = MLPClassifier(hidden_layer_sizes=(1024,),batch_size=256,verbose=True,early_stopping=True).fit(X_train_pca, y_train)
y_pred = clf.predict(X_test_pca)
print(classification_report(y_test,y_pred,target_names=target_names))

#visualization

def plot_gallery(images,titles,h,w,rows=3,cols=4):
    plt.figure()
    for i in range(rows*cols):
        plt.subplot(rows,cols,i+1)
        plt.imshow(images[i].reshape(h,w),cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.xticks(())
        plt.yticks(())

def titles(y_pred,y_test,target_names):
    for i in range(y_pred.shape[0]):
        pred_name = target_names[y_pred[i]].split(' ')[-1]
        true_name = target_names[y_test[i]].split(' ')[-1]
        yield 'predicted: {0}\ntrue: {1}'.format(pred_name,true_name)

prediction_titles = list(titles(y_pred,y_test,target_names))       
plot_gallery(X_test,prediction_titles,h,w)
        
    
7104257033107285780522e4a60f1929cc280fba
<mask token>


def plot_gallery(images, titles, h, w, rows=3, cols=4):
    plt.figure()
    for i in range(rows * cols):
        plt.subplot(rows, cols, i + 1)
        plt.imshow(images[i].reshape(h, w), cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.xticks(())
        plt.yticks(())


def titles(y_pred, y_test, target_names):
    for i in range(y_pred.shape[0]):
        pred_name = target_names[y_pred[i]].split(' ')[-1]
        true_name = target_names[y_test[i]].split(' ')[-1]
        yield 'predicted: {0}\ntrue: {1}'.format(pred_name, true_name)


<mask token>

<mask token>
print(lfw_dataset.images.shape)
<mask token>
print(target_names)
<mask token>
print('Fitting the classifier to the training set')
<mask token>
print(classification_report(y_test, y_pred, target_names=target_names))


def plot_gallery(images, titles, h, w, rows=3, cols=4):
    plt.figure()
    for i in range(rows * cols):
        plt.subplot(rows, cols, i + 1)
        plt.imshow(images[i].reshape(h, w), cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.xticks(())
        plt.yticks(())


def titles(y_pred, y_test, target_names):
    for i in range(y_pred.shape[0]):
        pred_name = target_names[y_pred[i]].split(' ')[-1]
        true_name = target_names[y_test[i]].split(' ')[-1]
        yield 'predicted: {0}\ntrue: {1}'.format(pred_name, true_name)


<mask token>
plot_gallery(X_test, prediction_titles, h, w)

<mask token>
lfw_dataset = fetch_lfw_people(min_faces_per_person=100)
print(lfw_dataset.images.shape)
_, h, w = lfw_dataset.images.shape
X = lfw_dataset.data
y = lfw_dataset.target
target_names = lfw_dataset.target_names
print(target_names)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
components = 100
pca = PCA(n_components=components, whiten=True).fit(X_train)
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print('Fitting the classifier to the training set')
clf = MLPClassifier(hidden_layer_sizes=(1024,), batch_size=256, verbose=
    True, early_stopping=True).fit(X_train_pca, y_train)
y_pred = clf.predict(X_test_pca)
print(classification_report(y_test, y_pred, target_names=target_names))


def plot_gallery(images, titles, h, w, rows=3, cols=4):
    plt.figure()
    for i in range(rows * cols):
        plt.subplot(rows, cols, i + 1)
        plt.imshow(images[i].reshape(h, w), cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.xticks(())
        plt.yticks(())


def titles(y_pred, y_test, target_names):
    for i in range(y_pred.shape[0]):
        pred_name = target_names[y_pred[i]].split(' ')[-1]
        true_name = target_names[y_test[i]].split(' ')[-1]
        yield 'predicted: {0}\ntrue: {1}'.format(pred_name, true_name)


prediction_titles = list(titles(y_pred, y_test, target_names))
plot_gallery(X_test, prediction_titles, h, w)

import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.decomposition import PCA
from sklearn.neural_network import MLPClassifier
lfw_dataset = fetch_lfw_people(min_faces_per_person=100)
print(lfw_dataset.images.shape)
_, h, w = lfw_dataset.images.shape
X = lfw_dataset.data
y = lfw_dataset.target
target_names = lfw_dataset.target_names
print(target_names)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
components = 100
pca = PCA(n_components=components, whiten=True).fit(X_train)
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print('Fitting the classifier to the training set')
clf = MLPClassifier(hidden_layer_sizes=(1024,), batch_size=256, verbose=
    True, early_stopping=True).fit(X_train_pca, y_train)
y_pred = clf.predict(X_test_pca)
print(classification_report(y_test, y_pred, target_names=target_names))


def plot_gallery(images, titles, h, w, rows=3, cols=4):
    plt.figure()
    for i in range(rows * cols):
        plt.subplot(rows, cols, i + 1)
        plt.imshow(images[i].reshape(h, w), cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.xticks(())
        plt.yticks(())


def titles(y_pred, y_test, target_names):
    for i in range(y_pred.shape[0]):
        pred_name = target_names[y_pred[i]].split(' ')[-1]
        true_name = target_names[y_test[i]].split(' ')[-1]
        yield 'predicted: {0}\ntrue: {1}'.format(pred_name, true_name)


prediction_titles = list(titles(y_pred, y_test, target_names))
plot_gallery(X_test, prediction_titles, h, w)

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.decomposition import PCA
from sklearn.neural_network import MLPClassifier

lfw_dataset = fetch_lfw_people(min_faces_per_person=100)

print(lfw_dataset.images.shape)
_,h,w = lfw_dataset.images.shape

X=lfw_dataset.data
y=lfw_dataset.target

target_names = lfw_dataset.target_names
print(target_names)

#split into training and testing
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3)

#COmpute a PCA
components = 100
pca = PCA(n_components=components,whiten=True).fit(X_train)

#apply PCA transformation
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)

#training a neural network
print("Fitting the classifier to the training set")

clf = MLPClassifier(hidden_layer_sizes=(1024,),batch_size=256,verbose=True,early_stopping=True).fit(X_train_pca, y_train)
y_pred = clf.predict(X_test_pca)
print(classification_report(y_test,y_pred,target_names=target_names))

#visualization

def plot_gallery(images,titles,h,w,rows=3,cols=4):
    plt.figure()
    for i in range(rows*cols):
        plt.subplot(rows,cols,i+1)
        plt.imshow(images[i].reshape(h,w),cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.xticks(())
        plt.yticks(())

def titles(y_pred,y_test,target_names):
    for i in range(y_pred.shape[0]):
        pred_name = target_names[y_pred[i]].split(' ')[-1]
        true_name = target_names[y_test[i]].split(' ')[-1]
        yield 'predicted: {0}\ntrue: {1}'.format(pred_name,true_name)

prediction_titles = list(titles(y_pred,y_test,target_names))       
plot_gallery(X_test,prediction_titles,h,w)
        
    