import numpy as np
import pandas as pd
R = np.array([[4,0,2,0,1],
[0,2,3,0,0],
[1,0,2,4,0],
[5,0,0,3,1],
[0,0,1,5,1],
[0,3,2,4,1,],])
def LFM_grad_desc(R, K=2, max_iter=1000, alpha=0.0001, lamda=0.001):
M = len(R)
N = len(R[0])
P = np.random.rand(M,K)
Q = np.random.rand(N,K)
Q = Q.T
for step in range(max_iter):
for u in range(M):
for i in range(N):
if R[u][i] > 0:
eui = np.dot(P[u,:],Q[:,i]) - R[u][i]
for k in range(K):
P[u][k] -= alpha * (2 * eui * Q[k][i] + 2 * lamda * P[u][k])
Q[k][i] -= alpha * (2 * eui * P[u][k] + 2 * lamda * Q[k][i])
predR = np.dot( P, Q)
cost = 0
for u in range(M):
for i in range(N):
if R[u][i]>0:
cost += (np.dot(P[u,:],Q[:,i]) - R[u][i]) ** 2
for k in range(K):
cost += lamda * (P[u][k]**2 + Q[k][i]**2)
if cost< 0.0001:
break
return P, Q.T, cost
测试,这里选择的是jupyter notebook
K = 5
max_iter = 5000
alpha = 0.0002
lamda = 0.004
P, Q, cost = LFM_grad_desc(R, K, max_iter, alpha, lamda)
print(P)
print(Q)
print(cost)
predR = P.dot(Q.T)
predR
结果展示