Skip to content

Commit 58fd405

Browse files
Create nmf.py
1 parent 3f4a7da commit 58fd405

File tree

1 file changed

+61
-0
lines changed
  • Chapter_15 MatrixFactorization

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# coding:UTF-8
2+
3+
import numpy as np
4+
from mf import load_data, save_file, prediction, top_k
5+
6+
def train(V, r, maxCycles, e):
7+
m, n = np.shape(V)
8+
# 1、初始化矩阵
9+
W = np.mat(np.random.random((m, r)))
10+
H = np.mat(np.random.random((r, n)))
11+
12+
# 2、非负矩阵分解
13+
for step in xrange(maxCycles):
14+
V_pre = W * H
15+
E = V - V_pre
16+
err = 0.0
17+
for i in xrange(m):
18+
for j in xrange(n):
19+
err += E[i, j] * E[i, j]
20+
21+
if err < e:
22+
break
23+
if step % 1000 == 0:
24+
print "\titer: ", step, " loss: " , err
25+
26+
a = W.T * V
27+
b = W.T * W * H
28+
for i_1 in xrange(r):
29+
for j_1 in xrange(n):
30+
if b[i_1, j_1] != 0:
31+
H[i_1, j_1] = H[i_1, j_1] * a[i_1, j_1] / b[i_1, j_1]
32+
33+
c = V * H.T
34+
d = W * H * H.T
35+
for i_2 in xrange(m):
36+
for j_2 in xrange(r):
37+
if d[i_2, j_2] != 0:
38+
W[i_2, j_2] = W[i_2, j_2] * c[i_2, j_2] / d[i_2, j_2]
39+
40+
return W, H
41+
42+
43+
if __name__ == "__main__":
44+
# 1、导入用户商品矩阵
45+
print "----------- 1、load data -----------"
46+
V = load_data("data.txt")
47+
# 2、非负矩阵分解
48+
print "----------- 2、training -----------"
49+
W, H = train(V, 5, 10000, 1e-5)
50+
# 3、保存分解后的结果
51+
print "----------- 3、save decompose -----------"
52+
save_file("W", W)
53+
save_file("H", H)
54+
# 4、预测
55+
print "----------- 4、prediction -----------"
56+
predict = prediction(V, W, H, 0)
57+
# 进行Top-K推荐
58+
print "----------- 5、top_k recommendation ------------"
59+
top_recom = top_k(predict, 2)
60+
print top_recom
61+
print W * H

0 commit comments

Comments
 (0)