## Matrix Factorization Models

``````

In [22]:

%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

def PlotHeatmap(X):
c = 3.
plt.figure(figsize=(X.shape[1]/c,X.shape[0]/c))
plt.imshow(X, interpolation='nearest',vmax=1,vmin=0)
plt.set_cmap('gray_r')
plt.gca().set_xticks([])
plt.gca().set_yticks([])
plt.show()

N = 8
M = 10
R = 1

A = np.random.rand(N,R)**2
A = A/np.sum(A,axis=0)
B = np.random.rand(R,M)**2
B = B/np.sum(B,axis=1).reshape((R,1))
Y = A.dot(B)

PlotHeatmap(A)
PlotHeatmap(B)
PlotHeatmap(Y)

``````
``````

``````

## Singular Value Decomposition

``````

In [8]:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc
import scipy.linalg as la
import scipy.misc

X = sc.misc.ascent()
fig = plt.figure(figsize=(7,7))
plt.gray()
plt.imshow(X)
plt.show()

``````
``````

``````
``````

In [9]:

U,S,Vt = la.svd(X)

M,N = X.shape

for rnk in range(1,20,2):
Sr = np.zeros_like(S)
Sr[0:rnk] = S[0:rnk]
Sig = la.diagsvd(Sr, M, N)

A2 = np.dot(np.dot(U, Sig), Vt)

fig = plt.figure(figsize=(5,5))
plt.gray()
plt.imshow(A2)
plt.show()

``````
``````

``````