summaryrefslogtreecommitdiff
path: root/algorithms/matrix-factorization.py
blob: ffffc5913191e8b4373cf3e50204e2fc9a46359c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python

import numpy as np
import scipy
from scipy.sparse.linalg import svds

m = 512
n = 1024
M = np.random.rand(m,n)
print "original "+str(m*n)


# svds
if False:
  print "svds"
  U, sigma, V = svds(M, k = 42)
  S = np.diag(sigma)
  M_ = np.dot(U, np.dot(S, V))
  shapes = U.shape, S.shape, V.shape
  sum = 0
  for sh in shapes:
    sum = sum + sh[0]*sh[1]
  print "params "+str(sum)
  print np.absolute(np.sum(M - M_))

  print

# qr
if False:
  print "qr"
  q, r = np.linalg.qr(M,mode='reduced')
  sum = 0
  for sh in (q.shape, r.shape):
    sum = sum + sh[0]*sh[1]
  print "params "+str(sum)

# eig
#print "eig (just square, also: Cholesky)"
#la, v = scipy.linalg.eig(M)

#print "M"
#print M
#print "U"
#print U
#print "V"
#print V
#print "shapes"
#print "U*diag(S)*V^T"
#print(M_)
#print "M_ - M"
#print M.shape, M_.shape
#print M - M_
#print "sum"
#print "shapes"
#print U.shape, S.shape, V.shape