LFM--梯度降低法--實現基於模型的協同過濾0.引入依賴1.數據準備2.算法的實現3.測試python
import numpy as np # 數值計算、矩陣運算、向量運算
import pandas as pd # 數值分析、科學計算
# 定義評分矩陣 R
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],
])
# R.shape # (6, 5)
# R.shape[0] # 6
# R.shape[1] # 5
# len(R) # 6
# len(R[0]) # 5
"""
@輸入參數:
R:M*N 的評分矩陣
K:隱特徵向量維度
max_iter: 最大迭代次數
alpha:步長
lamda:正則化係數
@輸出:
分解以後的 P,Q
P:初始化用戶特徵矩陣 M*K
Q:初始化物品特徵矩陣 N*K,Q 的轉置是 K*N
"""
# 給定超參數
K = 5
max_iter = 5000
alpha = 0.0002
lamda = 0.004
# 核心算法
def LMF_grad_desc(R, K=2, max_iter=1000, alpha=0.0001, lamda=0.002):
# 定義基本維度參數
M = len(R)
N = len(R[0])
# P、Q 的初始值隨機生成
P = np.random.rand(M, K)
Q = np.random.rand(N, K)
Q = Q.T
# 開始迭代
for steps in range(max_iter):
# 對全部的用戶 u,物品 i 作遍歷,而後對對應的特徵向量 Pu、Qi 作梯度降低
for u in range(M):
for i in range(N):
# 對於每個大於 0 的評分,求出預測評分偏差 e_ui
if R[u][i] > 0:
e_ui = np.dot(P[u,:], Q[:,i]) - R[u][i]
# 代入公式,按照梯度降低算法更新當前的 Pu、Qi
for k in range(K):
P[u][k] = P[u][k] - alpha * (2 * e_ui * Q[k][i] + 2 * lamda * P[u][k])
Q[k][i] = Q[k][i] - alpha * (2 * e_ui * P[u][k] + 2 * lamda * Q[k][i])
# u,i 遍歷完成,全部的特徵向量更新完成,能夠獲得 P、Q,能夠計算預測評分矩陣
predR = np.dot(P, Q)
# 計算當前損失函數(全部的預測偏差平方後求和)
cost = 0
for u in range(M):
for i in range(N):
# 對於每個大於 0 的評分,求出預測評分偏差後,將全部的預測偏差平方後求和
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
P, Q, cost = LMF_grad_desc(R, K, max_iter, alpha, lamda)
print(P)
print(Q)
print(cost)
predR = P.dot(Q.T)
print(R)
predR
當 K = 2 時,輸出結果以下:算法
[[1.44372596 1.29573962]
[1.82185633 0.0158696 ]
[1.5331521 0.16327061]
[0.31364667 1.9008297 ]
[1.03622742 2.03603634]
[1.34107967 0.93406796]]
[[ 0.4501051 2.55477489]
[ 1.18869845 1.20910294]
[ 1.54255106 -0.23514326]
[ 2.33556583 1.21026575]
[ 0.43753164 0.34555928]]
1.0432768290554293
[[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]]
array([[3.96015147, 3.2828374 , 1.92233657, 4.9401063 , 1.07943065],
[0.86057008, 2.18482578, 2.80657478, 4.27427181, 0.80260368],
[1.10719924, 2.0198665 , 2.32657341, 3.77837848, 0.72722223],
[4.99736596, 2.6711301 , 0.03684871, 3.03305153, 0.79407969],
[5.66802576, 3.69353946, 1.11967348, 4.8843224 , 1.15695354],
[2.98996017, 2.72352365, 1.84904408, 4.2626503 , 0.90954065]])
當 K = 5 時,輸出結果以下:app
[[ 0.77991893 0.95803701 0.75945903 0.74581653 0.58070622]
[ 1.51777367 0.66949331 0.89818609 0.23566984 0.56583223]
[ 0.03567022 0.58391558 1.42477223 0.87262652 -0.52553017]
[ 1.24101793 0.86257736 0.73772417 0.18181617 0.97014545]
[ 0.58789616 0.53522492 0.48830352 1.80622908 0.81202167]
[ 1.08640318 0.87660384 0.68935314 0.84506882 0.92284071]]
[[ 1.64469428 1.10535565 0.56686066 0.38656745 1.56519511]
[ 0.61680687 0.57188343 0.49729111 0.9623455 0.43969708]
[ 0.99260822 0.6007452 1.14768173 -0.16998497 -0.14094479]
[ 0.47070988 0.85347655 1.43546859 1.8185161 0.29759968]
[ 0.07923314 0.49412497 0.53285806 0.23753882 -0.05146021]]
0.7478305665280703
[[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]]
array([[3.9694342 , 2.37968507, 2.01268221, 3.8040546 , 1.08714641],
[4.72218838, 2.2412959 , 2.81976984, 3.17210672, 0.95653992],
[1.02652007, 1.67315396, 1.94711343, 3.99085212, 1.28488146],
[5.0014878 , 2.22716585, 2.42906339, 2.99867943, 0.91091753],
[3.80452512, 3.00679363, 1.04401937, 4.96078887, 0.95850804],
[4.91762916, 2.73324389, 2.1224277 , 4.06049468, 1.03980543]])