#! /usr/bin/env python3 # def faces_classify_knn ( ): #*****************************************************************************80 # ## faces_classify_knn() uses k-nearest neighbor classification on face data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 September 2023 # # Author: # # Andreas Mueller, Sarah Guido. # This version by John Burkardt. # # Reference: # # Andreas Mueller, Sarah Guido, # Introduction to Machine Learning with Python, # OReilly, 2017, # ISBN: 978-1-449-36941-5 # from sklearn.datasets import fetch_lfw_people from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier import matplotlib.pyplot as plt import mglearn import numpy as np import pandas as pd import platform import sklearn print ( '' ) print ( 'faces_classify_knn():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) print ( ' Match a face to an item in the faces dataset.' ) print ( ' Use the k-nearest neighbor method.' ) print ( '' ) # # Generate the dataset. # print ( ' Generate the dataset' ) people = fetch_lfw_people ( min_faces_per_person = 20, resize = 0.7 ) image_shape = people.images[0].shape # # Display 2x5 grid of 10 sample faces. # print ( ' Plot the dataset (X0,X1).' ) plt.clf ( ) fig, axes = plt.subplots ( 2, 5, figsize = ( 15, 8 ), subplot_kw = { 'xticks': (), 'yticks': () } ) for target, image, ax in zip ( people.target, people.images, axes.ravel() ): ax.imshow ( image ) ax.set_title ( people.target_names[target] ) filename = 'faces_sample.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) print ( " people.images.shape:", people.images.shape ) print ( " Number of classes:", len ( people.target_names ) ) # # Count how often each target appears. # counts = np.bincount ( people.target ) for i, ( count, name ) in enumerate ( zip ( counts, people.target_names ) ): print ( name, count ) # # Trim the dataset so no more than 50 images of any person. # mask = np.zeros ( people.target.shape, dtype = bool ) for target in np.unique ( people.target ): mask[np.where ( people.target == target) [0][:50]] = 1 X_people = people.data[mask] y_people = people.target[mask] # # Scale the grayscale values to be between 0 and 1. # X_people = X_people / 255.0 # # ? # X_train, X_test, y_train, y_test = train_test_split ( X_people, y_people, stratify = y_people, random_state = 0 ) knn = KNeighborsClassifier ( n_neighbors = 1 ) knn.fit ( X_train, y_train ) print ( "" ) print ( " Test set score using 1 neighbor", knn.score(X_test,y_test) ) # # Terminate. # print ( '' ) print ( 'faces_classify_knn():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) faces_classify_knn ( ) timestamp ( )