# Factorizing my face

Matrix factorization is a commonly used technique to fill in the blanks in sparse matrices, or to reduce features to lower dimensional spaces.

I found myself wondering how well they reconstruct data, so let’s factorize a matrix we can all agree on: my face.

I know, hard to improve on that. But still, lets throw it into a common matrix factorization algorithm, singular value decomposition.

First I open the image as an array in numpy:

I = np.asarray(Image.open(‘Lester.jpg’))

I’m going to reverse out the colour, so that I can set all that white space to be sparse.

rev = 255-I

and now I’m gonna normalise between 0 and 1 and put it in a sparse matrix:

from scipy.sparse import coo_matriximage_coo = coo_matrix(rev/255)

The SVD… we create 3 smaller matrices (u, s, vt) that can reconstruct out original image:

from scipy.sparse.linalg import svdsu, s, vt = svds(image_coo, k=10)s_diag = np.diag(s)

And recreate:

svd_reconstructed = np.dot(np.dot(u, s_diag), vt)svd_corrected = (1-svd_reconstructed)*255

Not bad for just 10 dimensions! This increases my faith that my mf-based recommendation engines are recreating the patterns that exist in my data.