#! /usr/bin/env python3 # def faces_classify_nmf ( ): #*****************************************************************************80 # ## faces_classify_nmf() uses nonnegative matrix factorization (nmf) on face data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 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.decomposition import NMF 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 platform import sklearn print ( '' ) print ( 'faces_classify_nmf():' ) 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 nonnegative matrix factorization (nmf).' ) print ( '' ) # # Show the results of NMF on the two-dimensional toy data: # mglearn.plots.plot_nmf_illustration ( ) filename = 'toy_nmf.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # 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 # # 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 # # Split the data. # X_train, X_test, y_train, y_test = train_test_split ( X_people, y_people, stratify = y_people, random_state = 0 ) # # Show how the number of components affects the quality of the reconstruction. # if ( False ): mglearn.plots.plot_nmf_faces ( X_train, X_test, image_shape ) filename = 'faces_reconstruction_nmf.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Extract first 15 nmf omponents. # nmf = NMF ( n_components = 15, random_state = 0, max_iter = 6000 ) nmf.fit ( X_train ) X_train_nmf = nmf.transform ( X_train ) X_test_nmf = nmf.transform ( X_test ) # # Plot nmf components. # plt.clf ( ) fig, axes = plt.subplots ( 3, 5, figsize = ( 15, 12 ), subplot_kw = { 'xticks':(), 'yticks':() } ) for i, ( component, ax ) in enumerate ( zip ( nmf.components_, axes.ravel() ) ): ax.imshow ( component.reshape ( image_shape ) ) ax.set_title ( "Component {}".format ( ( i+1) ) ) filename = 'nmf_components.png' plt.savefig ( filename ) print ( " Graphics saved as '" + filename + "'" ) # # Look at images for which these components are strong. # Sort by component 3, plot first 10 images. # compn = 3 inds = np.argsort ( X_train_nmf[:,compn])[::-1] fig, axes = plt.subplots ( 2, 5, figsize = ( 15, 8 ), subplot_kw = { 'xticks':(), 'yticks':() } ) fig.suptitle ( "Large component 3" ) for i, ( ind, ax ) in enumerate ( zip ( inds, axes.ravel() ) ): ax.imshow ( X_train[ind].reshape(image_shape) ) filename = 'nmf_component3.png' plt.savefig ( filename ) print ( " Graphics saved as '" + filename + "'" ) # # Sort by component 7, plot first 10 images. # compn = 7 inds = np.argsort ( X_train_nmf[:,compn])[::-1] fig, axes = plt.subplots ( 2, 5, figsize = ( 15, 8 ), subplot_kw = { 'xticks':(), 'yticks':() } ) fig.suptitle ( "Large component 7" ) for i, ( ind, ax ) in enumerate ( zip ( inds, axes.ravel() ) ): ax.imshow ( X_train[ind].reshape(image_shape) ) filename = 'nmf_component7.png' plt.savefig ( filename ) print ( " Graphics saved as '" + filename + "'" ) # # Terminate. # print ( '' ) print ( 'faces_classify_nmf():' ) 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_nmf ( ) timestamp ( )