# BS1033 Lecture 8 Analysis
# Author: Chris Hansman
# Email: [email protected]
# Date : 25/02/20
#Loading Libraries
library(tidyverse)
#Function to Plot Images
plt_img <- function(x){ image(matrix(as.numeric(x), nrow = 64, byrow = T), col = grey(seq(0, 1, length = 256))) }#Loading Face Data (Faces are Rows)faces <- read_csv(“olivetti.csv”) %>%
as.matrix()
#Plotting One ca
plt_img(faces[105,1:4096])
#Loading Labels
y_df<- sort(rep(1:40, times = 10))# Look for the average face for each person. #Group by label then take average of every variable (pixel)AverageFace <- data.frame(faces) %>%
mutate(label=y_df) %>%
group_by(label) %>%
summarise_all(mean)
#Plot
plt_img(AverageFace[1, 2:4097]) # For 1st subject
#PCA
pc <- prcomp(faces, center = T, scale=F)eigenvalues <- as.matrix((pc$sdev^2))eigenvectors <- as.matrix(pc$rotation)princomp <- pc$x#Recovering Initial Photosrecover <- eigenvectors%*%t(pc$x)+ pc$center plt_img(recover[,12]) # For 1st subjectplt_img(faces[12,1:4096])#First 40 Eigenvalues Explain 85% of the variance sum(eigenvalues[1:40])/sum(eigenvalues)#Selecting Those:eigen40=eigenvectors[,1:40]princomp40 <- pc$x[,1:40]center=pc$center#Recreating Images with Just the first 40 Principle Componentsrecover <- eigen40%*%t(princomp40)+ pc$center plt_img(recover[,30]) plt_img(faces[30,])#Projecting a photo onto first 40 eigenvalues:PF1 <- as.matrix((faces[88,]-center)%*%eigen40)PFall <-princomp40 # as.matrix(faces-center)%*%eigen40#Closest Optionstest <- matrix(rep(1,400),nrow=400,byrow=T)test_PF1 <- test %*% PF1Diff <- PFall-(test_PF1)a <- diag(Diff%*%t(Diff))# Find the Most Similar Facesy <- diag(Diff%*%t(Diff))x=c(1:400)newdf=data.frame(cbind(x,y))a<-newdf[order(newdf[,2]),]a[1:10,]plt_img(faces[88,])plt_img(faces[84,])
Reviews
There are no reviews yet.