基于标签的推荐系统和奇异值分解

问题描述

我正在尝试实现一个推荐系统,其中用户项目表如下所示:

User_item_table

我正在尝试改进带有标签的 SVD 方法

如何在python中实现?

解决方法

好的,我实现了基于标签的 SVD 模型的代码:

def matrix_factorization_tags(R,# rating matrix train
                          R_test,# rating matrix test
                          P,# learned matrix P
                          Q,# learned matrix Q
                          TU,# matrix of tags for users
                          TI,# matrix of tags for items
                          xs,# learned matrix for tags
                          yt,# learned matrix for tags
                          mu,# mean value in the training data
                          bu,# users biases
                          bi,# item biases
                          num_factors,# number of latent factors
                          num_tags,# number of tags
                          lambda_,lambda_bu,lambda_bi,steps,alpha,alpha_bu,alpha_bi,):
# Q = Q.T
for step in range(steps):
    for u in range(len(R)):
        for i in range(len(R[u])):
            if R[u,i] > 0:
                tg_u_len = 1 / np.sum(TU[u]) if np.sum(TU[u]) > 0 else 0
                xs_pu = tg_u_len * np.dot(TU[u,np.nonzero(TU[u])[0]],xs[np.nonzero(TU[u])[0],:])
                tg_i_len = 1 / np.sum(TI[i]) if np.sum(TI[i]) > 0 else 0
                yt_qi = tg_i_len * np.dot(TI[i,np.nonzero(TI[i])[0]],yt[np.nonzero(TI[i])[0],:])
                eui = R[u,i] - (mu + bu[u] + bi[i] + np.dot(P[u,:] + xs_pu,(Q[i,:] + yt_qi).T))
                bu[u] = bu[u] - alpha_bu * (lambda_bu * bu[u] - eui)
                bi[i] = bi[i] - alpha_bi * (lambda_bi * bi[i] - eui)
                P[u,:] = P[u,:] - alpha * (lambda_ * P[u,:] - eui * (Q[i,:] + yt_qi).T)
                Q[i,:] = Q[i,:] - alpha * (lambda_ * Q[i,:] - eui * (P[u,:] + xs_pu).T)
                for t in (np.nonzero(TU[u])[0]):
                    xs[t,:] = xs[t,:] - alpha * (
                            lambda_ * xs[t,:] - eui * TU[u,t] * tg_u_len * (Q[i,:] + yt_qi))
                for t in (np.nonzero(TI[i])[0]):
                    yt[t,:] = yt[t,:] - alpha * (
                            lambda_ * yt[t,:] - eui * TI[i,t] * tg_i_len * (P[u,:] + xs_pu))
    err = 0

    num_points = 0
    rmse_error = 0
    for u in range(len(R)):
        for i in range(len(R[u])):
            if R[u,i] > 0:
                num_points += 1
                tg_u_len = 1 / np.sum(TU[u]) if np.sum(TU[u]) > 0 else 0
                xs_pu = tg_u_len * np.dot(TU[u,:])
                err = err + 0.5 * pow(R[u,:] + yt_qi).T)),2)
                error_xt = 0
                error_yt = 0
                for t in (np.nonzero(TU[u])[0]):
                    # length of vector squared
                    error_xt += np.dot(xs[t,:],xs[t,:])
                for t in (np.nonzero(TI[i])[0]):
                    # length of vector squared
                    error_yt += np.dot(yt[t,yt[t,:])
                # the error estimation is here
                err += lambda_ / 2 * (pow(bu[u],2) + pow(bi[i],2) + np.dot(P[u,P[u,:]) +
                                      np.dot(Q[i,Q[i,:]) + error_xt + error_yt)
                rmse_error += pow(R[u,i] -
                                  (mu + bu[u] + bi[i] + np.dot(P[u,2)
    if err < 0.001:
        break

return P,Q,bu,bi,xs,yt

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...