forked from j2kun/svd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
35 lines (27 loc) · 630 Bytes
/
demo.py
File metadata and controls
35 lines (27 loc) · 630 Bytes
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
from numpy.linalg import svd
import numpy as np
movieRatings = [
[2, 5, 3],
[1, 2, 1],
[4, 1, 1],
[3, 5, 2],
[5, 3, 1],
[4, 5, 5],
[2, 4, 2],
[2, 2, 5],
]
U, singularValues, V = svd(movieRatings)
print(U)
print(singularValues)
print(V)
Sigma = np.vstack([
np.diag(singularValues),
np.zeros((5, 3)),
])
print(np.round(movieRatings - np.dot(U, np.dot(Sigma, V)), decimals=10))
U, singularValues, V = svd(movieRatings, full_matrices=False)
print(U)
print(singularValues)
print(V)
Sigma = np.diag(singularValues)
print(np.round(movieRatings - np.dot(U, np.dot(Sigma, V)), decimals=10))