Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculate the average face on the Olivetti dataset in R
rm(list=ls())
require(dplyr)
load(file="images_formatted.Rdata")
D <- out; rm(out)
#-------------------------------------------------------------------------------
# Brief description of the data
# D is a 399x4096 matrix. It contains 399 images (rows) of 64x64 grayscale pixels (4096 columns)
# Each row represents a sample (an image)
# Each column represents a variable/feature (a pixel)
# y_df contains the label for each image (vector of size 399x1)
# Check
nrow(D) # 399 images
ncol(D) # 64*64 = 4096 pixels
# Use this function to plot images from D
plt_img <- function(x){ image(matrix(x, nrow = 64, byrow = T), col = grey(seq(0, 1, length = 256))) }
# Plot of image 1
plt_img(D[1, ])
#-------------------------------------------------------------------------------
# Average face
# Look for the average face. Group by label then take average of every variable (pixel)
AverageFace <- data.frame(D) %>% mutate(label=y_df) %>% group_by(label) %>% summarise_all(mean)
# Plot the average faces for labels 0, 1 and 2.
plt_img(as.numeric(AverageFace[1, 2:4097])) # For label 0. (1st subject)
plt_img(as.numeric(AverageFace[2, 2:4097])) # For label 1. (2nd subject)
plt_img(as.numeric(AverageFace[3, 2:4097])) # For label 2. (3rd subject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment