<jupyter_start><jupyter_text>Exploring the 'faces'-datafileThe data can be downloaded from [the book's website](http://databookuw.com/).<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import os
import scipy.io
import imageio
plt.rcParams['figure.figsize'] = [8, 8]
plt.rcParams.update({'font.size': 18})
# Load the datafile and show the data structure
mat_contents = scipy.io.loadmat(os.path.join('..','data','allFaces.mat'))
print(mat_contents)
# number of faces from the different persons
nfaces = np.ndarray.flatten(mat_contents['nfaces'])
print(nfaces)
print("We have ", np.sum(nfaces[:]), "faces (single pictures) from ", mat_contents['person'], "people")
# the faces are defined by a n x m matrix
faces = mat_contents['faces']
m = int(mat_contents['m'])
n = int(mat_contents['n'])
print("Matrix: ", n, " x ", m)
# show the first picture of the first 36 persons together in a 6x6 matrix
allPersons = np.zeros((n*6,m*6))
count = 0

for j in range(6):
    for k in range(6):
        allPersons[j*n : (j+1)*n, k*m : (k+1)*m] = np.reshape(faces[:,np.sum(nfaces[:count])],(m,n)).T
        count += 1
        
img = plt.imshow(allPersons)
img.set_cmap('gray')
plt.axis('off')
plt.show()      
# show first face of person number 37
testperson = np.reshape(faces[:,np.sum(nfaces[:36])],(m,n)).T
img = plt.imshow(testperson)
img.set_cmap('gray')
plt.axis('off')
plt.show()
# Show the first face from each of the 38 persons 
for person in range(len(nfaces)):
    subset = faces[:,sum(nfaces[:person]) : sum(nfaces[:(person+1)])]
    oneFace = np.zeros((n, m))
    
    count = 0
    
    if count < nfaces[person]:
        oneFace = np.reshape(subset[:,count],(m,n)).T
        count += 1            
        nr = person + 1
        title = "Face-" + str(nr) + "-0" + str(count)
        img = plt.imshow(oneFace)
        img.set_cmap('gray')
        plt.axis('off')
        plt.title(title)
        plt.show()
# show all the faces from the dataset in a single picture
for person in range(len(nfaces)):
    subset = faces[:,sum(nfaces[:person]) : sum(nfaces[:(person+1)])]
    oneFace = np.zeros((n, m))
    
    count = 0
    
    for j in range(8):
        for k in range(8):
            if count < nfaces[person]:
                oneFace = np.reshape(subset[:,count],(m,n)).T
                count += 1
                
                nr = person + 1
                if count < 10:
                    title = "Face-" + str(nr) + "-0" + str(count)
                else:
                    title = "Face-" + str(nr) + "-" + str(count)
                
                img = plt.imshow(oneFace)
                img.set_cmap('gray')
                plt.axis('off')
                plt.title(title)
                plt.show()                
               
                # in order to write all the faces (2410 pictures) into the "./Faces"-directory uncomment the following two lines
                #name = "./Faces/" + title + ".jpg"
                #imageio.imwrite(name, oneFace)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15394780310538486, 'fraction_numerical': 0.013214403700033036, 'mean_word_length': 2.3756967670011147, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '398196', 'license': 'MIT', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1000, 'n_words': 257, 'path': 'eigenfaces-and-svd/allFaces.ipynb', 'repo_name': 'rh-eu/data-driven-science-and-engineering', 'size': '3027'}