summaryrefslogtreecommitdiff
path: root/algorithms
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms')
-rwxr-xr-xalgorithms/matrix-factorization.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/algorithms/matrix-factorization.py b/algorithms/matrix-factorization.py
new file mode 100755
index 0000000..ffffc59
--- /dev/null
+++ b/algorithms/matrix-factorization.py
@@ -0,0 +1,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
+