这里写自定义目录标题
梯度下降法
LFM梯度下降代码实现
##0.导入依赖
import numpy as np
import pandas as pd
##1.数据准备
#评分矩阵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],])
print(len(R[0]))
##2.算法实现
# 给定超参数
K = 5
max_iter = 5000
alpha = 0.0002
lamda = 0.004
#2.核心算法
def LFM_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 step in range(max_iter):
# 对所有的用户u、物品i做遍历,对应的特征向量Pu、Qi梯度下降
for u in range(M):
for i in range(N):
# 对于每一个大于0的评分,求出预测评分误差
if R[u][i] > 0:
eui = np.dot( P[u,:], Q[:,i] ) - R[u][i]
# 代入公式,按照梯度下降算法更新当前的Pu、Qi
for k in range(K):
P[u][k] = P[u][k] - alpha * ( 2 * eui * Q[k][i] + 2 * lamda * P[u][k] )
Q[k][i] = Q[k][i] - alpha * ( 2 * eui * 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):
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
#3.测试
P, Q, cost = LFM_grad_desc(R, K, max_iter, alpha, lamda)
print(P)
print(Q)
print(cost)
predR =P.dot(Q.T)
print(R)
print(predR)
5
[[ 0.56673613 0.34826721 0.85866493 1.06504025 0.97110728]
[ 0.8188752 1.13953097 0.41349327 0.38201337 0.54952153]
[-0.40161804 1.17894459 0.96427247 0.982718 -0.20638006]
[ 0.96921651 0.46051735 0.64186908 0.31884266 1.5209907 ]
[-0.04778176 0.31875172 1.8564225 0.55367684 0.90224171]
[ 0.45637385 0.54400324 1.10656551 0.97319149 0.59461569]]
[[ 1.47248732 0.56266328 0.75710756 0.57855207 1.73710174]
[ 0.205509 0.71386831 0.90072089 1.12293978 0.54246766]
[ 0.96653192 1.55383739 -0.00655418 0.61777864 0.25883868]
[-0.04585982 1.06352216 1.84788883 1.08257104 0.66658521]
[ 0.00743762 0.26468697 0.18749461 0.38398617 0.397147 ]]
0.5674073254738292
[[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]]
[[3.98366404 2.86127409 1.99261058 3.7314243 1.05202507]
[3.3356039 2.08127897 2.93764188 2.71831211 0.75016592]
[1.0120797 2.61919405 1.99107456 3.98041232 0.78524697]
[4.99882183 2.28920571 2.23880626 2.99046476 0.97593666]
[3.4021199 3.0010284 1.0125232 4.97246869 1.00301009]
[3.41183438 2.8942375 2.03426631 4.05235129 0.96470188]]