# -*- coding: utf-8-*-import numpy as np
def build_rating(m,n,ratings_df):'''
m(int):用户数
n(int):电影数
ratings_df(DataFrame):用户对电影评分记录表
rating(ndarray):用户-电影评分矩阵
'''
#*********Begin*********#
#创建电影评分矩阵
#获取用户数与电影数
userNo = m
movieNo = n
#创建电影评分表
rating = np.zeros((userNo,movieNo))for index,row in ratings_df.iterrows():
rating[int(row['userId']),int(row['movieRow'])]=row['rating']
#*********End*********#
return rating
#训练模型
def fit(lr,alpha,d,n_iter,data):'''
userID(int):推荐用户ID
lr(float):学习率
alpha(float):权重衰减系数
d(int):矩阵分解因子
n_iter(int):训练轮数
data(ndarray):用户-电影评分矩阵
'''
#*********Begin*********#
#获取用户数与电影数
m,n = data.shape
#初始化喜好矩阵与内容矩阵,值为0-1之间,符合正态分布
x = np.random.uniform(0,1,(m,d))
w = np.random.uniform(0,1,(d,n))
#创建评分记录表,无评分记为0,有评分记为1
record = np.array(data>0,dtype=int)
#梯度下降,更新参数
for i in range(n_iter):
x_grads = np.dot(np.multiply(record,np.dot(x,w)-data),w.T)
w_grads = np.dot(x.T,np.multiply(record,np.dot(x,w)-data))
x = alpha*x - lr*x_grads
w = alpha*w - lr*w_grads
#预测
predict = np.dot(x,w)
#*********End*********#
return predict
#进行推荐
def recommend(userID,predict,data):'''
userID(int):推荐用户ID
predict(ndarray):预测结果
data(ndarray):用户-电影评分矩阵
'''
#*********Begin*********#
#将用户未看过的电影分值从低到高进行排列
recommend = np.argsort(predict[userID-1])
a = recommend[-1]
b = recommend[-2]
c = recommend[-3]
d = recommend[-4]
e = recommend[-5]
#*********End*********#
return recommend
# -*- coding: utf-8-*-import numpy as np
def build_rating(m,n,ratings_df):'''
m(int):用户数
n(int):电影数
ratings_df(DataFrame):用户对电影评分记录表
rating(ndarray):用户-电影评分矩阵
'''
#*********Begin*********#
#创建电影评分矩阵
rating = np.zeros((m,n))for index,row in ratings_df.iterrows():
rating[int(row['userId']),int(row['movieRow'])]=row['rating']
#*********End*********#
return rating
#训练模型
def fit(lr,alpha,d,n_iter,data):'''
userID(int):推荐用户ID
lr(float):学习率
alpha(float):权重衰减系数
d(int):矩阵分解因子
n_iter(int):训练轮数
data(ndarray):用户-电影评分矩阵
'''
#*********Begin*********#
#获取用户数与电影数
m,n = data.shape
#初始化喜好矩阵与内容矩阵,值为0-1之间,符合正态分布
x = np.zeros((m,d))
w = np.zeros((d,n))
i =0
while i < m:
j =0
while j < d:
a = np.random.normal(loc =0.5, scale =0.25)if a >=0 and a <=1:
x[i][j]= a
j +=1
i +=1
i =0
while i < d:
j =0
while j < n:
a = np.random.normal(loc =0.5, scale =0.25)if a >=0 and a <=1:
w[i][j]= a
j +=1
i +=1
#创建评分记录表,无评分记为0,有评分记为1
record = np.zeros(data.shape)
i =0
while i < m:
j =0
while j < n:if data[i][j]==0:
record[i][j]=0else:
record[i][j]=1
j +=1
i +=1
#梯度下降,更新参数
loss = np.mean(np.multiply((data-np.dot(x,w))**2,record))
x_grads = np.dot(np.multiply(record,np.dot(x,w)-data),w.T)
w_grads = np.dot(x.T,np.multiply(record,np.dot(x,w)-data))for i in range(n_iter):
x_grads = np.dot(np.multiply(record,np.dot(x,w)-data),w.T)
w_grads = np.dot(x.T,np.multiply(record,np.dot(x,w)-data))
x = alpha*x - lr*x_grads
w = alpha*w - lr*w_grads
#预测
predict = np.dot(x,w)
#*********End*********#
return predict
#进行推荐
def recommend(userID,predict,data):'''
userID(int):推荐用户ID
predict(ndarray):预测结果
data(ndarray):用户-电影评分矩阵
'''
#*********Begin*********#
#将用户未看过的电影分值从低到高进行排列
a,b = data.shape
record = np.zeros(data.shape)
i =0
while i < a:
j =0
while j < b:if data[i][j]==0:
record[i][j]=0else:
record[i][j]=1
j +=1
i +=1
r = record[userID -1]
m = predict[userID -1]
n =range(b)
dic =dict(zip(n,m))for i in range(b):if r[i]==1:
del dic[i]
recommend = np.zeros(len(dic))for i in range(len(dic)):
k =list(dic.keys())
recommend[i]= k[0]for l in dic.keys():if dic[l]>= dic[recommend[i]]:
recommend[i]= l
del dic[recommend[i]]
recommend = recommend[::-1]
#*********End*********#
return recommend