May you be faithful to yourself, live earnestly and laugh freel.
推荐系统算法简介
这里简单介绍下推荐系统中最为主要的协同过滤算法,大致分为如下几类:
之前用了python来写矩阵分解,现在换种写法,用keras来实现矩阵分解。
import pandas as pd
import numpy as np
rating = pd.read_csv('data/ratings.csv',sep=',')
num_user = np.max(rating['userId'])
num_movie = np.max(rating['movieId'])
print(num_user,num_movie,len(rating))
#计算用户电影组合的R矩阵填充率
R = len(rating)/(num_user*num_movie)
print(f"矩阵填充率:{R}")
矩阵的填充率为:0.000853805025622708
from keras import Model
import keras.backend as K
from keras.layers import Embedding ,Reshape,Input,Dot
K.clear_session()
def Recomand_model(num_user,num_movie,k):
input_user = Input(shape=[None,],dtype='int32')
model_user = Embedding(num_user+1,k,input_length = 1)(input_user)
model_user = Reshape((k,))(model_user)
input_moive = Input(shape=[None,],dtype="int32")
model_moive = Embedding(num_movie+1,k,input_length = 1)(input_moive)
model_moive = Reshape((k,))(model_moive)
out = Dot(1)([model_user,model_moive])
model = Model(input=[input_user,input_moive],outputs = out)
model.compile(loss = 'mse',optimizer = 'Adam')
model.summary()
return model
model =Recomand_model(num_user,num_movie,100)
注
model.summary()
输出模型各层的参数状况
model.layers.Reshape() :将输出调整为特定的形状
train_u = rating['userId'].values
train_i = rating['movieId'].values
train_x = [train_u,train_i]
train_y = rating["rating"].values
model.fit(train_x,train_y,batch_size = 100,epochs = 10)
model.predict([[1],[2]])
array([[ 3.76612234]], dtype=float32)