Please forgive if this is a repeat but I couldn't find a similar question (at least as it pertains to me).
I have a database of 30,000 images of digits (0-9). Every image is 28*28. So, every image is represented by a row of 785 columns with the first column as a label (whether the digit is 0 or 1 or … 9) and columns 2: 785 having a value of black(from 0-255).
Now, I have the pca using pca <- prcomp(df[, -1], center=TRUE)
with 784 PCAs. I also have a mean of all the digits using meanDig <- apply(df[, -1], 2, mean)
(I don't know if this mean is useful or not).
Now, I am being asked to recreate the '100th' image from the database using first 15 PCs.
I understand from this question and other related questions how to recreate one single image from the PCA of that single image.
But if I have PCA of a collective of 30000 images, is it possible to recreate the one single image?
I tried:
recreation <- pca$x[, 1:15] %*% t(pca$rotation[, 1:15])
# which gives me a matrix of 23520000 elements.
# I am not sure how I can recreate a 28*28 image?
# Then I thought maybe I can do this for the "15th" row:
recreation <- pca$x[100, 1:15] %*% t(pca$rotation[100, 1:15])
# but I am not sure what this even means.
Any suggestions?
Edit #1
I am adding more information after taking @chechy_levas suggestions into consideration.
# Read the data:
df <- read.csv("classDigits.csv")
head(df[, 1:5])
label pixel0 pixel1 pixel2 pixel3
1 2 0 0 0 0
2 4 0 0 0 0
3 7 0 0 0 0
4 2 0 0 0 0
5 8 0 0 0 0
6 9 0 0 0 0
# Calculate PCA:
pca <- prcomp(df[, 2:785], center = TRUE)
head(pca$rotation[, 1:3])
PC1 PC2 PC3
pixel0 2.219274e-20 -5.732181e-19 6.287447e-20
pixel1 2.081668e-17 1.110223e-16 2.081668e-17
pixel2 -1.942890e-16 0.000000e+00 4.857226e-17
pixel3 -1.387779e-16 1.110223e-16 4.336809e-17
pixel4 5.551115e-17 0.000000e+00 -1.387779e-17
pixel5 1.110223e-16 1.387779e-16 2.081668e-17
# calculate the mean digit
meanImage <- apply(df[, 2:785], 2, mean)
# mean image looks like this:
# The 15th image for reference:
# recreate the image at row 15 with 15 PC.
img15 <- pca$x[15, 1:15] %*% t(pca$rotation[, 1:15])
img15 <- img15 + meanImage
# Image with 15 pc:
# recreate the image at row 15 with 100 PC.
img15 <- pca$x[15, 1:100] %*% t(pca$rotation[, 1:100])
img15 <- img15 + meanImage
# Image with 100 pc:
# Image with 200 PC