Esempio n. 1
0
    def Update(self, R, k, r, lamb1, lamb2, aerfa, aerfa1):
        '''

		:param R: user-item matrix
		:param k: The number of iterations
		:param r: r-rank factors
		:param lamb1: used to calculate U
		:param lamb2: used to calculate V
		:param aerfa:
		:param aerfa1:
		:return:
		'''
        print("R:\n", R)
        I = copy.deepcopy(R)
        I[I > 0] = 1

        m, n = R.shape
        U = np.array(np.random.random((r, m)), dtype='float64')
        V = np.array(np.random.random((r, n)), dtype='float64')

        #这里可以通过KNN找到user的朋友矩阵
        simiX = trainW(R)
        W = myKNN(simiX, 5)
        # print("w:",W)

        # updating formulas
        for i in range(k):
            # U
            for i_u in range(m):
                subU1 = np.zeros((r, 1), dtype='float64')
                for j_u in range(n):
                    # print(np.array(U[:,i_u].T).shape, np.array(V[:,j_u]).shape)
                    # print(U[:,j_u].T)
                    # print(V[:,j_u])
                    # print(I[i_u][j_u])
                    subU1 = subU1 + (I[i_u][j_u] *
                                     (np.dot(U[:, i_u].T, V[:, j_u]) -
                                      R[i_u][j_u])) * V[:, j_u]
                subU1 = subU1 + lamb1 * U[:, i_u]

                subU2on = np.zeros((r, 1))
                Fri = np.argwhere(W[i_u] == 1)
                # print(len(Fri))
                for f in range(len(Fri)):
                    # print(Fri[f][0])
                    # print("i_u\t",i_u)
                    # print("Fri[%d][0]\t" % f,Fri[f][0])
                    # print(U[:,Fri[f][0]].shape)
                    # print("asdasdd:\n",subU2on)
                    subU2on = subU2on + (metrices.VectorSpaceSimilarity(
                        R, I, i_u, Fri[f][0])) * (
                            np.array(U[:, i_u], dtype='float64') -
                            np.array(U[:, Fri[f][0]], dtype='float64'))
                # print("subU2on:\n", subU2on)
                subU2 = aerfa * subU2on[0]
                subU = subU1 + 2 * subU2

                # print("subU1:\n",subU1)
                # print("subU2:\n",subU2)
                # print("subU:\n",subU)
                del subU1
                del subU2
                U[:, i_u] = U[:, i_u] - aerfa1 * np.array(subU[0])

            #V
            for i_v in range(n):
                subV = np.zeros((1, r))
                for j_v in range(m):
                    subV = subV + (I[j_v][i_v] *
                                   (np.dot(U[:, j_v].T, V[:, i_v]) -
                                    R[j_v][i_v])) * U[:, j_v]
                subV = subV + lamb2 * V[:, i_v]
                # print("subV:\n", subV)
                V[:, i_v] = V[:, i_v] - aerfa1 * subV[0]
            print("run%d" % i)

        return U, V
Esempio n. 2
0
    def Update(self, R, k, r, lamb1, lamb2, aerfa, aerfa1):
        '''

		:param R: user-item matrix
		:param k: The number of iterations
		:param r: r-rank factors
		:param lamb1: used to calculate U
		:param lamb2: used to calculate V
		:param aerfa:
		:return:
		'''
        print("R:\n", R)
        I = copy.copy(R)
        I[I > 0] = 1

        m, n = R.shape
        U = np.array(np.random.random((r, m)), dtype='float64')
        V = np.array(np.random.random((r, n)), dtype='float64')

        #这里可以通过KNN找到user的朋友矩阵
        simiX = trainW(R)
        W = myKNN(simiX, 5)
        # print("w:",W)

        # updating formulas
        for i in range(k):
            # U
            for i_u in range(m):
                subU1 = np.zeros((r, 1), dtype='float64')
                for j_u in range(n):
                    # print(np.array(U[:,i_u].T).shape, np.array(V[:,j_u]).shape)
                    # print(U[:,j_u].T)
                    # print(V[:,j_u])
                    # print(I[i_u][j_u])
                    subU1 = subU1 + (I[i_u][j_u] *
                                     (np.dot(U[:, i_u].T, V[:, j_u]) -
                                      R[i_u][j_u])) * V[:, j_u]
                subU1 = subU1 + lamb1 * U[:, i_u]

                subU2on = np.zeros((r, 1))
                subU2down = 0
                Fri = np.argwhere(W[i_u] == 1)
                # print(len(Fri))
                for f in range(len(Fri)):
                    # print(Fri[f][0])
                    # print("i_u\t",i_u)
                    # print("Fri[%d][0]\t" % f,Fri[f][0])
                    # print(U[:,Fri[f][0]].shape)
                    # print("asdasdd:\n",subU2on)
                    subU2on = np.round(
                        subU2on + (metrices.VectorSpaceSimilarity(
                            R, I, i_u, Fri[f][0])) * np.array(U[:, Fri[f][0]]),
                        6)
                    # print("asd",U[:,Fri[f][0]])
                    # print("")
                    subU2down = np.round(
                        subU2down +
                        metrices.VectorSpaceSimilarity(R, I, i_u, Fri[f][0]),
                        6)
                # print("subU2on:\n", subU2on)
                # print("subU2down:\n", subU2down)
                subU2 = aerfa * np.array(U[:, i_u] - (subU2on[0] / subU2down))

                subU3 = np.zeros((r, 1))
                subU3on = np.zeros((r, 1))
                subU3down = 0
                subU3onon = np.zeros((r, 1))
                subU3downdown = 0
                for g in range(len(Fri)):
                    # for f in range(len(Fri)):
                    # subU3onon = subU3onon + (metrices.VectorSpaceSimilarity(R, I, Fri[g][0], Fri[f][0])) * U[:,Fri[f][0]]
                    # subU3downdown = subU3downdown + metrices.VectorSpaceSimilarity(R, I, Fri[g][0], Fri[f][0])
                    # subU3down = subU3down + metrices.VectorSpaceSimilarity(R, I, Fri[g][0], Fri[f][0])
                    subU3on = np.round(
                        subU3on +
                        -(metrices.VectorSpaceSimilarity(R, I, i_u, Fri[g][0])
                          * np.array(U[:, g] - (subU2on[0] / subU2down))), 6)
                    subU3 = subU3 + (subU3on / subU2down)
                    # print("subU3 run\t\t:",g)
                subU3 = aerfa * np.array(subU3)

                subU = subU1 + subU2 + subU3

                # print("subU1:\n",subU1)
                # print("subU2:\n",subU2)
                # print("subU3:\n",subU3)
                # print("subU:\n",subU)
                U[:, i_u] = U[:, i_u] - aerfa1 * np.array(subU[0])

            #V
            for i_v in range(n):
                subV = np.zeros((1, r))
                for j_v in range(m):
                    uv = np.dot(U[:, j_v].T, V[:, i_v]) - R[j_v][i_v]
                    uv1 = I[j_v][i_v] * uv
                    subV = np.round(subV + uv1 * U[:, j_v], 6)
                subV = subV + lamb2 * V[:, i_v]
                # print("subV:\n", subV)
                V[:, i_v] = V[:, i_v] - aerfa1 * subV[0]
            print("run%d" % i)

        return U, V