from scipy import *
from pylab import *
img = imread("images/initial.png")[:,:,0]
gray()
figure(1) # prepare the canvas for figure 1.
imshow(img)
<matplotlib.image.AxesImage at 0x24a888d7438>
m, n = img.shape
U, S, Vt = svd(img)
S = resize(S, [m,1]) * eye(m,n) # see below for a demo.
k = 20
figure(2)
imshow(dot(U[:,1:k], dot(S[1:k,1:k], Vt[1:k,:])))
<matplotlib.image.AxesImage at 0x24a865ca898>
col_v = resize(asarray([1,2]), [2,1])
eye_mat = eye(2,3)
result = col_v * eye_mat
col_v
array([[1], [2]])
eye_mat
array([[1., 0., 0.], [0., 1., 0.]])
result
array([[1., 0., 0.], [0., 2., 0.]])