Skip to content

Instantly share code, notes, and snippets.

@smly
Created January 20, 2011 20:28
Show Gist options
  • Select an option

  • Save smly/788586 to your computer and use it in GitHub Desktop.

Select an option

Save smly/788586 to your computer and use it in GitHub Desktop.
>>> x= np.matrix([[2,3],[4,5],[5,6],[6,7]]).T
>>> x
matrix([[2, 4, 5, 6],
[3, 5, 6, 7]])
>>> u,s,vh = np.linalg.svd(x)
>>> u
matrix([[-0.63626513, -0.77147047],
[-0.77147047, 0.63626513]])
>>> s
array([ 14.13594166, 0.41851331])
>>> vh
matrix([[-0.25374621, -0.45291732, -0.55250288, -0.65208843],
[ 0.87417635, 0.22805428, -0.09500675, -0.41806779],
[-0.41253131, 0.79111926, 0.06788672, -0.44647467],
[ 0.03530567, 0.3420343 , -0.82529127, 0.4479513 ]])
>>> u.T[0] * x
matrix([[-3.58694167, -6.40241286, -7.81014846, -9.21788406]])
>>> vh[0] * s[0]
matrix([[-3.58694167, -6.40241286, -7.81014846, -9.21788406]])
>>> u.T[1] * x
matrix([[ 0.36585444, 0.09544375, -0.03976159, -0.17496693]])
>>> vh[1] * s[1]
matrix([[ 0.36585444, 0.09544375, -0.03976159, -0.17496693]])
>>> (u.T[0] * x - vh[0] * s[0]).sum()
6.2172489379008766e-15
>>> (u.T[1] * x - vh[1] * s[1]).sum()
7.4107386893729199e-15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment