── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(USairpollution)dim(USairpollution)
[1] 41 7
A)
Perform singular value decomposition of this data matrix. Then create the matrix \(D\). Describe what this matrix looks like.
This is a p * p matrix, where p is the amount of numeric variables are in the data (in this case, 7.)
The diagonal non-zero values are the single values σ, sorted from greatest to least. It measures the amount of variation along the i-th singular vector.
B)
Verify that \(X=UDV^T\) by plotting all the entries of \(X\) versus all the entries of \(UDV^T\) with the 0/1 line.
X_reconstructed <- svd_result$u %*% D %*%t(svd_result$v)# Flatten matrices to vectorsX_vec <-as.vector(X)X_recon_vec <-as.vector(X_reconstructed)# Scatter plotplot(X_vec, X_recon_vec, xlab ="Original X", ylab ="Reconstructed UDV^T",main ="SVD",pch =16, col ="blue")# Add the 0/1 line (y = x)abline(0, 1, col ="red", lwd =2)
C)
Consider low-dimensional approximations of the data matrix. What is the fewest number of dimensions required to yield a correlation between the entries of \(X\) and \(\tilde X\) of at least 0.9?
In this problem we explore how “high” a low-dimensional SVD approximation of an image has to be before you can recognize it.
.Rdata objects are essentially R workspace memory snapshots that, when loaded, load any type of R object that you want into your global environment. The command below, when executed, will load three objects into your memory: mysteryU4, mysteryD4, and mysteryV4. These are the first \(k\) vectors and singular values of an SVD I performed on a 700-pixels-tall \(\times\) 600-pixels-wide image of a well-known villain.
img<-load('Data/mystery_person_k4.Rdata')
A)
Write a function that takes SVD ingredients u, d and v and renders the \(700 \times 600\) image produced by this approximation using functions from the magick package. Use your function to determine whether a 4-dimensional approximation to this image is enough for you to tell who the mystery villain is. Recall that you will likely need to rescale your recomposed approximation so that all pixels are in [0,1].
library(magick)
Warning: package 'magick' was built under R version 4.5.2
U <- mysteryU4D <- mysteryD4V <- mysteryV4k <-4# Reconstruct low-rank approximationXtilde <- U[, 1:k, drop =FALSE] %*%diag(D[1:k]) %*%t(V[, 1:k, drop =FALSE])# Scale to [0,1]Xtilde <- Xtilde -min(Xtilde)Xtilde <- Xtilde /max(Xtilde)# Convert to raster and grayscale PNGimg <- Xtilde %>%as.raster() %>%image_read() %>%image_convert(colorspace ="Gray")print(img)
# A tibble: 1 × 7
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 PNG 600 700 Gray TRUE 0 72x72
B)
I’m giving you slightly higher-dimensional approximations (\(k=10\) and \(k=50\), respectively) in the objects below:
load('Data/mystery_person_k10.Rdata')U10 <- mysteryU10D10 <- mysteryD10V10 <- mysteryV10k <-10# Reconstruct low-rank approximationXtilde10 <- U10[, 1:k, drop =FALSE] %*%diag(D10[1:k]) %*%t(V10[, 1:k, drop =FALSE])# Scale to [0,1]Xtilde10 <- Xtilde10 -min(Xtilde10)Xtilde10 <- Xtilde10 /max(Xtilde10)# Convert to raster and grayscale PNGimg10 <- Xtilde10 %>%as.raster() %>%image_read() %>%image_convert(colorspace ="Gray")print(img10)
# A tibble: 1 × 7
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 PNG 600 700 Gray TRUE 0 72x72
load('Data/mystery_person_k50.Rdata')U50 <- mysteryU50D50 <- mysteryD50V50 <- mysteryV50k <-50# Reconstruct low-rank approximationXtilde50 <- U50[, 1:k, drop =FALSE] %*%diag(D50[1:k]) %*%t(V50[, 1:k, drop =FALSE])# Scale to [0,1]Xtilde50 <- Xtilde50 -min(Xtilde50)Xtilde50 <- Xtilde50 /max(Xtilde50)# Convert to raster and grayscale PNGimg50 <- Xtilde50 %>%as.raster() %>%image_read() %>%image_convert(colorspace ="Gray")print(img50)
# A tibble: 1 × 7
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 PNG 600 700 Gray TRUE 0 72x72
Create both of the images produced by these approximations. At what point can you tell who the mystery villain is?
k=50 is a very clear image, but you can reduce k to about 20, and can make out with relative ease who the person is.
C)
How many numbers need to be stored in memory for each of the following:
A full \(700\times 600\) image?
A 4-dimensional approximation?
A 10-dimensional approximation?
A 50-dimensional approximation?
Part A is simple, you need a dimension for each pixel. 700x600=420,000 FORMULA FOR APPROXIMATION:(MxK)+k+(NxK)
For Part B, we get 2800+4+2400= 5204
For Part C, we get 7000+10+6000 = 13010 For Part D, we get 35000 + 50 + 30000 = 65050