Exemplo n.º 1
0
def _get_pointwise_all_data(dataset, num_negatives):
    user_input, item_input, labels = [], [], []
    train_matrix = dataset.train_matrix
    num_items = dataset.num_items
    num_users = dataset.num_users

    for u in range(num_users):
        items_by_u = train_matrix[u].indices
        num_items_by_u = len(items_by_u)
        if num_items_by_u > 0:
            negative_items = randint_choice(num_items,
                                            num_items_by_u * num_negatives,
                                            replace=True,
                                            exclusion=items_by_u)
            index = 0
            for i in items_by_u:
                # positive instance
                user_input.append(u)
                item_input.append(i)
                labels.append(1)
                # negative instance
                user_input.extend([u] * num_negatives)
                item_input.extend(negative_items[index:index + num_negatives])
                labels.extend([0] * num_negatives)
                index = index + 1
    return user_input, item_input, labels
Exemplo n.º 2
0
def _get_pointwise_all_highorder_data(dataset, high_order, num_negatives,
                                      train_dict):
    user_input, item_input, item_input_recents, labels = [], [], [], []
    num_items = dataset.num_items
    num_users = dataset.num_users

    for u in range(num_users):
        items_by_u = train_dict[u]
        num_items_by_u = len(items_by_u)
        if num_items_by_u > high_order:

            negative_items = randint_choice(
                num_items, (num_items_by_u - high_order) * num_negatives,
                replace=True,
                exclusion=items_by_u)
            index = 0
            for idx in range(high_order, num_items_by_u):
                user_input.append(u)
                i = items_by_u[idx]  # item id
                item_input.append(i)
                item_input_recents.append(items_by_u[idx - high_order:idx])
                labels.append(1)
                user_input.extend([u] * num_negatives)
                item_input.extend(negative_items[index:index + num_negatives])
                item_input_recents.extend([items_by_u[idx - high_order:idx]] *
                                          num_negatives)
                labels.extend([0] * num_negatives)
                index = index + 1

    return user_input, item_input, item_input_recents, labels
Exemplo n.º 3
0
def _get_pairwise_all_likefossil_data(dataset, high_order, train_dict):
    user_input_id,user_input_pos,user_input_neg, num_idx_pos, num_idx_neg, item_input_pos,item_input_neg,item_input_recents = [],[], [], [],[],[],[],[]
    for u in range(dataset.num_users):
        items_by_user = train_dict[u].copy()
        num_items_by_u = len(items_by_user)
        if num_items_by_u > high_order:
            negative_items = randint_choice(dataset.num_items,
                                            num_items_by_u,
                                            replace=True,
                                            exclusion=items_by_user)
            for idx in range(high_order, len(train_dict[u])):
                i = train_dict[u][idx]  # item id
                item_input_recent = []
                for t in range(1, high_order + 1):
                    item_input_recent.append(train_dict[u][idx - t])
                item_input_recents.append(item_input_recent)
                j = negative_items[idx]
                user_input_neg.append(items_by_user)
                num_idx_neg.append(num_items_by_u)
                item_input_neg.append(j)

                items_by_user.remove(i)
                user_input_id.append(u)
                user_input_pos.append(items_by_user)
                num_idx_pos.append(num_items_by_u - 1)
                item_input_pos.append(i)

    return user_input_id, user_input_pos, user_input_neg, num_idx_pos, num_idx_neg, item_input_pos, item_input_neg, item_input_recents
Exemplo n.º 4
0
def _get_pairwise_all_likefism_data(dataset):
    user_input_pos, user_input_neg, num_idx_pos, num_idx_neg, item_input_pos, item_input_neg = [], [], [], [], [], []
    num_items = dataset.num_items
    num_users = dataset.num_users
    train_matrix = dataset.train_matrix
    for u in range(num_users):
        items_by_u = train_matrix[u].indices.copy().tolist()
        num_items_by_u = len(items_by_u)
        if num_items_by_u > 1:
            negative_items = randint_choice(num_items,
                                            num_items_by_u,
                                            replace=True,
                                            exclusion=items_by_u)

            for index, i in enumerate(items_by_u):
                j = negative_items[index]
                user_input_neg.append(items_by_u)
                num_idx_neg.append(num_items_by_u)
                item_input_neg.append(j)

                items_by_u.remove(i)
                user_input_pos.append(items_by_u)
                num_idx_pos.append(num_items_by_u - 1)
                item_input_pos.append(i)

    return user_input_pos, user_input_neg, num_idx_pos, num_idx_neg, item_input_pos, item_input_neg
Exemplo n.º 5
0
def _get_pointwise_all_firstorder_data(dataset, num_negatives, train_dict):
    user_input, item_input, item_input_recent, labels = [], [], [], []
    num_items = dataset.num_items
    num_users = dataset.num_users
    for u in range(num_users):
        items_by_user = train_dict[u]
        num_items_by_u = len(items_by_user)
        negative_items = randint_choice(num_items,
                                        (num_items_by_u - 1) * num_negatives,
                                        replace=True,
                                        exclusion=items_by_user)
        index = 0
        for idx in range(1, num_items_by_u):
            i = items_by_user[idx]  # item id
            user_input.append(u)
            item_input.append(i)
            item_input_recent.append(items_by_user[idx - 1])
            labels.append(1)
            # negative instance
            user_input.extend([u] * num_negatives)
            item_input.extend(negative_items[index:index + num_negatives])
            item_input_recent.extend([items_by_user[idx - 1]] * num_negatives)
            labels.extend([0] * num_negatives)
            index = index + 1
    return user_input, item_input, item_input_recent, labels
Exemplo n.º 6
0
def _get_pairwise_all_firstorder_data(dataset, train_dict):
    user_input, item_input_pos, item_input_recent, item_input_neg = [], [], [], []
    num_items = dataset.num_items
    num_users = dataset.num_users
    for u in range(num_users):
        items_by_u = train_dict[u]
        num_items_by_u = len(items_by_u)
        if num_items_by_u > 1:
            user_input.extend([u] * (num_items_by_u - 1))
            item_input_pos.extend(items_by_u[1:])
            item_input_recent.extend(items_by_u[:-1])
            item_input_neg.extend(
                randint_choice(num_items, (num_items_by_u - 1),
                               replace=True,
                               exclusion=items_by_u))
    return user_input, item_input_pos, item_input_recent, item_input_neg
Exemplo n.º 7
0
def _get_pairwise_all_data(dataset):
    user_input, item_input_pos, item_input_neg = [], [], []
    train_matrix = dataset.train_matrix
    num_items = dataset.num_items
    num_users = dataset.num_users

    for u in range(num_users):
        items_by_u = train_matrix[u].indices
        num_items_by_u = len(items_by_u)
        if num_items_by_u > 0:
            user_input.extend([u] * num_items_by_u)
            item_input_pos.extend(items_by_u)
            item_input_neg.extend(
                randint_choice(num_items,
                               num_items_by_u,
                               replace=True,
                               exclusion=items_by_u))

    return user_input, item_input_pos, item_input_neg
Exemplo n.º 8
0
 def get_negatives(self, evaluate_neg):
     if evaluate_neg > 0:
         user_list = []
         neg_item_list = []
         for u in np.arange(self.num_users):
             items_by_u = self.train_matrix[u].indices.tolist(
             ) + self.test_matrix[u].indices.tolist()
             neg_items = randint_choice(self.num_items,
                                        evaluate_neg,
                                        replace=False,
                                        exclusion=items_by_u).tolist()
             neg_item_list.extend(neg_items)
             user_list.extend(len(neg_items) * [u])
         negatives = sp.csr_matrix(
             ([1] * len(user_list), (user_list, neg_item_list)),
             shape=(self.num_users, self.num_items))
     else:
         negatives = None
     return negatives
Exemplo n.º 9
0
def _get_pairwise_all_highorder_data(dataset, high_order, train_dict):
    user_input, item_input_pos, item_input_recents, item_input_neg = [], [], [], []
    num_items = dataset.num_items
    num_users = dataset.num_users
    for u in range(num_users):
        items_by_u = train_dict[u]
        num_items_by_u = len(items_by_u)

        if num_items_by_u > high_order:
            user_input.extend([u] * (num_items_by_u - high_order))
            item_input_pos.extend(items_by_u[high_order:])
            item_input_neg.extend(
                randint_choice(num_items, (num_items_by_u - high_order),
                               replace=True,
                               exclusion=items_by_u))

            for idx in range(high_order, num_items_by_u):
                item_input_recents.append(items_by_u[idx - high_order:idx])

    return user_input, item_input_pos, item_input_recents, item_input_neg