%matplotlib inline
%config InlineBackend.figure_formats = set(['svg'])
import matplotlib.pyplot as plt
import numpy as np
import scipy.linalg as linalg
sample = plt.imread('sample_10.png')
plt.imshow(sample, cmap='gray')
(U, s, Vh) = linalg.svd(sample)
def reconstruct(U, s, Vh, n):
accum = np.zeros_like(U)
for i in range(0, n):
accum += np.outer(U[:,i], Vh[i,:]) * s[i]
return accum
plt.imshow(reconstruct(U,s,Vh,1), cmap='gray')
plt.imshow(reconstruct(U,s,Vh,2), cmap='gray')
plt.imshow(reconstruct(U,s,Vh,3), cmap='gray')
plt.imshow(reconstruct(U,s,Vh,5), cmap='gray')
plt.imshow(reconstruct(U,s,Vh,15), cmap='gray')
plt.imshow(reconstruct(U,s,Vh,256), cmap='gray')