Example #1
0
    def train(self, test_users=None, b=None):
        block_size = 5000
        self.model = {}
        self.test_users = test_users

        if test_users is None:
            test_users = range(self.n_users)

        tu = {}
        if b is not None:
            for u in test_users:
                tu[u] = set(test_users[u][:b])
        else:
            for u in test_users:
                tu[u] = set(test_users[u])

        for i, u in enumerate(tu):
            if self.verbose and (i + 1) % 100 == 0:
                print("%d/%d" % (i + 1, len(test_users)))
                utils.fl()

            #Xp = list(self.data.get_items(u)) #get list of songs
            Xp = [int(self.K_indexing[str(idx)]) for idx in tu[u]]
            mtx_indexes = [a / block_size for a in Xp]
            pos_indexes = [a % block_size for a in Xp]

            npos = len(Xp)

            kp = np.zeros((npos, npos))

            for j in range(npos):
                #kp[j] = self.K[Xp[j],Xp].todense()
                kp[j] = self.K[mtx_indexes[j]][pos_indexes[j], Xp]

            kp = co.matrix(kp)

            kn = self.q_[Xp, :]

            I = self.lambda_p * utc.identity(npos)
            P = kp + I
            q = -kn
            G = -utc.identity(npos)
            h = utc.zeroes_vec(npos)
            A = utc.ones_vec(npos).T
            b = co.matrix(1.0)

            solver.options['show_progress'] = False
            sol = solver.qp(P, q, G, h, A, b)

            band = np.zeros((npos, len(self.inverse_indexing)))
            for j in range(npos):
                band[j] = self.K[mtx_indexes[j]][pos_indexes[j], :]

            self.model[u] = (co.matrix(band.T) * (sol['x'])) - self.q_
            #print self.model[u]

        # endfor
        return self
Example #2
0
    def train(self, test_users=None, b=None, selection=None):
        self.model = {}
        self.test_users = test_users

        if test_users is None:
            test_users = range(self.n_users)

        tu = {}
        if b is not None:
            for u in test_users:
                tu[u] = set(test_users[u][:b])
        else:
            for u in test_users:
                tu[u] = set(test_users[u])

        for i, u in enumerate(tu):
            if self.verbose and (i + 1) % 100 == 0:
                print("%d/%d" % (i + 1, len(test_users)))
                utils.fl()

            Xp = [idx for idx in tu[u]]

            npos = len(Xp)

            kp = np.zeros((npos, npos))

            for j in range(npos):
                kp[j] = self.K[Xp[j], Xp].todense()

            kp = co.matrix(kp)

            kn = self.q_[Xp, :]

            I = self.lambda_p * utc.identity(npos)
            P = kp + I
            q = -kn
            G = -utc.identity(npos)
            h = utc.zeroes_vec(npos)
            A = utc.ones_vec(npos).T
            b = co.matrix(1.0)

            solver.options['show_progress'] = False
            sol = solver.qp(P, q, G, h, A, b)
            #print(selection[u][0:5])
            #utils.fl()
            tmp = (self.K[np.array(Xp)[:, None], selection[u]].T).dot(
                sol['x']) - self.q_[[a for a in selection[u]]]

            self.model[u] = np.zeros(len(self.items_list)) - np.inf
            #print(tmp.shape)
            self.model[u][selection[u]] = tmp[:, 0]
            #for i, v in enumerate(selection[u]):
            #    self.model[u][v] = tmp[i]
            #print self.model[u]

        # endfor
        return self
Example #3
0
    def train(self, test_users=None, bucket=None):
        t = time.time()
        self.model = {}

        Kar = self.Klist[1].todense()
        Kal = self.Klist[2].todense()

        self.test_users = test_users
        if test_users is None:
            self.test_users = range(self.n_users)

        if bucket is not None:
            tu = {u: set(self.test_users[u][:bucket]) for u in self.test_users}
        else:
            tu = {u: set(self.test_users[u]) for u in self.test_users}

        for i, u in enumerate(tu):
            if self.verbose and (i + 1) % 10 == 0:
                print("%d/%d" % (i + 1, len(self.test_users)))
                print(str(time.time() - t))
                utils.fl()

            Xp = [int(self.s2k[idx]) for idx in tu[u]]
            idxar = [self.k2ar[p] for p in Xp]
            idxal = [self.k2al[p] for p in Xp]
            npos = len(Xp)

            kp = np.zeros((npos, npos))
            for j in range(npos):
                kp[j] = self.mu[0] * self.Klist[0][Xp[j],
                                                   Xp].todense().reshape(
                                                       (npos, ))
                kp[j] = kp[j] + self.mu[1] * Kar[idxar[j], idxar].reshape(
                    (npos, ))
                kp[j] = kp[j] + self.mu[2] * Kal[idxal[j], idxal].reshape(
                    (npos, ))

            kp = co.matrix(kp)
            kn = self.q_[Xp, :]

            I = self.lambda_p * utc.identity(npos)
            P = kp + I
            q = -kn
            G = -utc.identity(npos)
            h = utc.zeroes_vec(npos)
            A = utc.ones_vec(npos).T
            b = co.matrix(1.0)

            solver.options['show_progress'] = False
            sol = solver.qp(P, q, G, h, A, b)

            K = co.matrix(self.mu[0] * self.Klist[0][Xp, :].todense())

            idxar = np.array(idxar).reshape((len(idxar), 1))
            idxal = np.array(idxal).reshape((len(idxal), 1))

            K = K + co.matrix(self.mu[1] * Kar[idxar, self.k2ar])
            K = K + co.matrix(self.mu[2] * Kal[idxal, self.k2al])

            self.model[u] = (K.T * sol['x']) - self.q_

        return self
Example #4
0
    def train(self, test_users=None, b=None):
        t = time.time()
        self.model = {}
        self.test_users = test_users

        if test_users is None:
            test_users = range(self.n_users)

        tu = {}
        if b is not None:
            for u in test_users:
                tu[u] = set(test_users[u][:b])
        else:
            for u in test_users:
                tu[u] = set(test_users[u])

        for i, u in enumerate(tu):
            if self.verbose and (i + 1) % 10 == 0:
                print("%d/%d" % (i + 1, len(test_users)))
                print(str(time.time() - t))
                utils.fl()

            Xp = [int(self.K_indexing[str(idx)]) for idx in tu[u]]

            npos = len(Xp)

            kp = np.zeros((npos, npos))

            for j in range(npos):
                kp[j] = self.K[Xp[j], Xp].todense()
            '''
            for row in range(kp.shape[0]):
                x = len(self.u2i[int(self.inverse_indexing[Xp[row]])])
                sx = math.sqrt(self.identity_disjunctive(self.N, x))
                xN = (x / N)
                for col in range(row+1, kp.shape[0]):
                    if kp[row,col] == 0.:
                        y = len(self.u2i[int(self.inverse_indexing[Xp[col]])])
                        sz = math.sqrt(self.identity_disjunctive(self.N, z))
                        kp[row,col] = (2. / (sx*sz)) * xN * (z / (N-1.))
                        kp[row,col] = kp[col,row]
            '''

            for row in range(kp.shape[0]):
                x = len(self.u2i[self.inverse_indexing[Xp[row]]])
                for col in range(row + 1, kp.shape[0]):
                    if kp[row, col] == 0.:
                        z = len(self.u2i[self.inverse_indexing[Xp[col]]])
                        mx, mn = (x, z) if x >= z else (z, x)
                        kp[row, col] = self.imp_dic["%d,%d" % (mx, mn)]
                        kp[col, row] = kp[row, col]

            kp = co.matrix(kp)

            kn = self.q_[Xp, :]

            I = self.lambda_p * utc.identity(npos)
            P = kp + I
            q = -kn
            G = -utc.identity(npos)
            h = utc.zeroes_vec(npos)
            A = utc.ones_vec(npos).T
            b = co.matrix(1.0)

            solver.options['show_progress'] = False
            sol = solver.qp(P, q, G, h, A, b)

            K_imp = co.matrix(self.K[Xp, :].todense())
            for row in range(K_imp.size[0]):
                x = len(self.u2i[self.inverse_indexing[Xp[row]]])
                for col in range(K_imp.size[1]):
                    if K_imp[row, col] == 0.:
                        z = len(self.u2i[self.inverse_indexing[col]])
                        mx, mn = (x, z) if x >= z else (z, x)
                        K_imp[row, col] = self.imp_dic["%d,%d" % (mx, mn)]

            self.model[u] = (K_imp.T * sol['x']) - self.q_
        #import numpy.random as rnd
        #with open("../files/"+rnd.randint()+".txt") as F:
        # endfor
        return self
Example #5
0
    def train(self, test_users=None, b=None):
        self.model = {}
        self.test_users = test_users

        if test_users is None:
            test_users = range(self.n_users)

        tu = {}
        if b is not None:
            for u in test_users:
                tu[u] = set(test_users[u][:b])
        else:
            for u in test_users:
                tu[u] = set(test_users[u])

        for i, u in enumerate(tu):
            if self.verbose and (i + 1) % 100 == 0:
                print("%d/%d" % (i + 1, len(test_users)))

            #Xp = list(self.data.get_items(u)) #get list of songs
            Xp = [int(self.K_indexing[str(idx)]) for idx in tu[u]]

            npos = len(Xp)

            #for each element in Xp, you could either calculate the similarity or take it from the matrix
            '''
            directly calculate them
            kp = np.zeros((len(Xp), len(Xp))
            for i in Xp:
                for j in Xp:
                    kp[i, j] = (playlists_containing_track[i] & playlists_containing_track[j] / 
                    math.sqrt(len(playlists_containing_track[i])*len(playlist_containing_track[j])))
            '''
            '''
                take them from the matrix
                
            '''
            kp = np.zeros((npos, npos))

            for j in range(npos):
                #print(self.K[Xp[i], Xp[i]])
                #print(self.K[Xp[i],Xp])
                kp[j] = self.K[Xp[j], Xp].todense()

            kp = co.matrix(kp)

            kn = self.q_[Xp, :]

            I = self.lambda_p * utc.identity(npos)
            P = kp + I
            q = -kn
            G = -utc.identity(npos)
            h = utc.zeroes_vec(npos)
            A = utc.ones_vec(npos).T
            b = co.matrix(1.0)

            solver.options['show_progress'] = False
            sol = solver.qp(P, q, G, h, A, b)

            self.model[u] = (self.K[Xp, :].T).dot(sol['x']) - self.q_
            #print self.model[u]

        # endfor
        return self
Example #6
0
    def train(self, test_users=None, b=None, test_titles=None, c=0):
        self.model = {}
        self.test_users = test_users
        self.c = c
        if test_users is None:
            test_users = range(self.n_users)

        tu = {}
        if b is not None:
            for u in test_users:
                tu[u] = set(test_users[u][:b])
        else:
            for u in test_users:
                tu[u] = set(test_users[u])

        for i, u in enumerate(tu):
            t = time.time()
            beta = self.Beta[test_titles[u]]
            beta_K = self.K.dot(beta.T).todense()
            if self.verbose and (i + 1) % 1 == 0:
                print("%d/%d" % (i + 1, len(test_users)))
                utils.fl()

            Xp = np.array([idx for idx in tu[u]])

            npos = len(Xp) + 1
            kp = np.zeros((npos, npos))
            #print(self.K[Xp[:,None],Xp].todense())
            kp[:npos - 1, :npos - 1] = self.K[Xp[:, None], Xp].todense()

            kp[npos - 1, :] = np.concatenate(
                (self.K[Xp, :].dot(beta.T).todense(), np.ones(
                    (1, 1)))).flatten()
            kp[:npos - 1, npos - 1] = kp[npos - 1, :npos - 1]

            q0 = (co.matrix(beta.todense()) * self.q_)[0, 0]
            kp = co.matrix(kp)

            kn = co.matrix(
                np.concatenate(
                    (np.array(self.q_[list(Xp), :]), np.array([[q0]]))))

            I = self.lambda_p * utc.identity(npos)
            P = kp + I
            q = -kn
            G = np.vstack((-np.eye(npos), np.zeros(npos)))
            G[npos, -1] = 1
            G = co.matrix(G)
            h = np.zeros(npos + 1)
            h[-1] = self.c
            h = co.matrix(h)
            A = utc.ones_vec(npos).T
            b = co.matrix(1.0)

            solver.options['show_progress'] = False
            sol = solver.qp(P, q, G, h, A, b)

            self.model[u] = np.array((self.K[Xp, :].T).dot(sol['x'][:-1]) +
                                     beta_K * sol['x'][-1] -
                                     self.q_).flatten()
            #print self.model[u]
            print(time.time() - t)
        # endfor
        return self