Rank-1 Approximation

In [3]:
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
In [67]:
np.random.seed(17)
n = 10
X = np.random.randn(2, n)

X[1] = 0.7*X[0] + 0.2 * X[1]

# uncomment this for a different data set
#X[0] = 0.1 * X[0]
In [47]:
pt.figure(figsize=(6,6))
pt.gca().set_aspect("equal")
pt.xlim([-2, 2])
pt.ylim([-2, 2])
pt.grid()
pt.plot(X[0], X[1], "o")
Out[47]:
[<matplotlib.lines.Line2D at 0x7f0e4bcf1dd8>]

Now compute the SVD. Use numpy.linalg.svd(..., full_matrices=False).

In [52]:
U, sigma, VT = la.svd(X, full_matrices=False)

Now find the vectors u and v:

In [53]:
u = U[:, 0]
v = VT[0]

Now find X1:

In [61]:
X1 = np.outer(u, v) * sigma[0]
In [66]:
pt.figure(figsize=(6,6))

pt.arrow(0, 0, u[0], u[1], lw=3)

pt.gca().set_aspect("equal")
pt.xlim([-2, 2])
pt.ylim([-2, 2])
pt.grid()

pt.plot(X[0], X[1], "ob", label="X")
pt.plot(X1[0], X1[1], "og", label="X1")
pt.legend(loc="best")
Out[66]:
<matplotlib.legend.Legend at 0x7f0e4ba4a2e8>
  • Is this the same as least-squares data fitting?
  • Would least-squares data fitting deal with the second data set above?