def recommend_with_scores(self, test_device_events, tips):
        last_session_events = _get_last_session(test_device_events)
        event_to_score = {}
        for (group_id, event_id) in self.event_types:
            event_to_score[(group_id, event_id)] = 0
            for (group_id2, event_id2) in last_session_events.keys():
                if (group_id2, event_id2) in self.cp_matrix[(group_id,
                                                             event_id)].keys():
                    event_to_score[(group_id, event_id)] += self.cp_matrix[(
                        group_id, event_id)][(group_id2, event_id2)]
                if (group_id2, event_id2) in self.dp_matrix.keys():
                    event_to_score[(group_id, event_id)] += self.dp_matrix[(
                        group_id2, event_id2)][(group_id, event_id)]

        sorted_by_score_events = sorted(event_to_score.items(),
                                        key=operator.itemgetter(1),
                                        reverse=True)

        tips_to_recommend = {}

        for top_event in sorted_by_score_events:
            event = top_event[0]
            score = top_event[1]
            if (event not in test_device_events.keys()) \
                    and _is_intersection(tips, event_to_tips(event[0], event[1])) > 0:
                for tip in event_to_tips(event[0], event[1]):
                    if tip in tips:
                        tips_to_recommend[tip] = score

        if self.is_logging:
            logging.info("RecommenderCo+Dis:recommend: recommendation made.")
        return tips_to_recommend
 def _generate_recommendation_list(self, recommendations, test_device_events, tips):
     recommendation_map = {}
     for event, score in recommendations:
         if event_to_tips(self.event_types[event][0], self.event_types[event][1]) and \
                 _is_intersection(tips, event_to_tips(self.event_types[event][0], self.event_types[event][1])) > 0 \
                 and self.event_types[event] not in test_device_events:
             for tip in event_to_tips(self.event_types[event][0], self.event_types[event][1]):
                 if tip in tips:
                     recommendation_map[tip] = max(0, score)
     return recommendation_map
def compute_map_k(user_to_done, user_to_recommendation, K):
    user_cnt = 0
    map_k = 0
    for device_id in user_to_recommendation.keys():
        user_cnt += 1
        done_tips = user_to_done[device_id]
        recommended_tips = user_to_recommendation[device_id]
        ap_k = 0
        for j in range(min(len(recommended_tips), K)):
            if _is_intersection(done_tips, [recommended_tips[j]]):
                p_k = 0
                for k in range(j + 1):
                    if _is_intersection(done_tips, [recommended_tips[k]]):
                            p_k += 1
                p_k = p_k * 1. / (j + 1)
                ap_k += p_k
        ap_k = ap_k * 1. / K
        map_k += ap_k

    map_k = map_k * 1. / user_cnt * 10
    return map_k
def compute_accuracy_first(user_to_done, user_to_recommendation):
    prec = 0
    user_cnt = 0
    for device_id in user_to_recommendation.keys():
        user_cnt += 1
        done_tips = user_to_done[device_id]
        recommended_tips = user_to_recommendation[device_id]
        if _is_intersection(done_tips, [recommended_tips[0]]):
            prec += 1

    prec = prec * 1. / user_cnt
    return prec
    def _get_not_done_events_with_probability(self, test_device_events, tips):
        not_done_event_with_prob = []
        all_not_done_top_sum = 0

        for top_event in self.top_events:
            event = top_event[0]
            probability = top_event[1]
            if (event not in test_device_events.keys()) \
                    and _is_intersection(tips, event_to_tips(event[0], event[1])) > 0:
                not_done_event_with_prob.append(top_event)
                all_not_done_top_sum += probability

        return not_done_event_with_prob, all_not_done_top_sum
def compute_mrr_k(user_to_done, user_to_recommendation, K):
    mrr_k = 0
    user_cnt = 0
    for device_id in user_to_recommendation.keys():
        user_cnt += 1
        done_tips = user_to_done[device_id]
        recommended_tips = user_to_recommendation[device_id]
        rr_k = 0
        for j in range(min(len(recommended_tips), K)):
            if _is_intersection(done_tips, [recommended_tips[j]]):
                rr_k = 1. / (j + 1)
                break
        mrr_k += rr_k

    mrr_k = mrr_k * 1. / user_cnt
    return mrr_k
def compute_ndcg_k(user_to_done, user_to_recommendation, K):
    ndcg_k = 0
    user_cnt = 0
    for device_id in user_to_recommendation.keys():
        user_cnt += 1
        done_tips = user_to_done[device_id]
        recommended_tips = user_to_recommendation[device_id]
        dcg_k = 0
        idcg_k = 0
        for j in range(min(len(recommended_tips), K)):
            idcg_k += 1. / math.log2(j + 2)
            if _is_intersection(done_tips, [recommended_tips[j]]):
                dcg_k += 1. / math.log2(j + 2)
        ndcg_k += dcg_k * 1. / idcg_k

    ndcg_k = ndcg_k * 1. / user_cnt
    return ndcg_k
    def recommend_with_scores(self, test_device_events, tips):
        if self.is_logging:
            logging.info(
                "RecommenderTopEvent:recommend: recommendation started.")
        test_device_events = self._filter_old_test_device_events(
            test_device_events)

        tips_to_recommend = {}

        for top_event in self.top_events:
            event = top_event[0]
            score = top_event[1]
            if (event not in test_device_events.keys())\
                    and _is_intersection(tips, event_to_tips(event[0], event[1])) > 0:
                for tip in event_to_tips(event[0], event[1]):
                    if tip in tips:
                        tips_to_recommend[tip] = score
        if self.is_logging:
            logging.info("RecommenderTopEvent:recommend: recommendation made.")
        return tips_to_recommend