Example #1
0
def get_eval(scores, items, index, top_n=10):
    """
    if the last element is the correct one, then
    index = len(scores[0])-1
    """
    # print('>>>>>>>>>>>>>>>> scores:', scores)
    # print('>>>>>>>>>>>>>>>> items:', items)
    eps = 1e-15

    ndcg = 0.0
    custom_hr = 0.0
    weighted_hr = 0.0
    hr = 0.0
    hits = np.array([0, 0, 0])

    assert len(scores[0]) > index and index >= 0
    # print(items)
    items_to_guess = np.array(items)[:, index]
    # print(items_to_guess.shape)

    assert len(items) == items_to_guess.shape[0]

    n_low = 0
    n_medium = 0
    n_high = 0

    for j in range(len(items_to_guess)):
        current_item = items_to_guess[j]
        current_popularity = Settings.normalized_popularity[current_item]
        if current_popularity <= Settings.low_popularity_threshold:
            n_low += 1
        elif Settings.low_popularity_threshold < current_popularity <= Settings.high_popularity_threshold:
            n_medium += 1
        else:
            n_high += 1

    # print('len(scores):', len(scores))
    assert n_low + n_medium + n_high == len(scores)

    # for score in scores:
    for i in range(len(scores)):
        score = scores[i]
        item_array = items[i]

        # print('index:', i, len(scores))
        # print('score:', score)
        # print('item:', item_array)

        # Get the top n indices
        arg_index = np.argsort(-score)[:top_n]

        if index in arg_index:
            # print('index, arg_index:', index, '-', arg_index)

            current_item = item_array[index]
            current_popularity = Settings.normalized_popularity[current_item]
            current_position = np.where(arg_index == index)[0][0]
            '''
            print('current_item:', current_item)
            print('current_popularity:', current_popularity)
            print('current_position:', current_position)
            print('arg_index:', arg_index)
            '''

            # Get the position
            ndcg += np.log(2.0) / np.log(arg_index.tolist().index(index) + 2.0)

            # Increment
            hr += 1.0

            # Custom HR
            custom_hr += y_custom(current_popularity, current_position, top_n)

            # Custom HR
            weighted_hr += (1 - current_popularity)

            # weighted_hr += sigmoid(1 / (current_popularity + eps))
            if current_popularity <= Settings.low_popularity_threshold:
                hits[0] += 1
            elif Settings.low_popularity_threshold < current_popularity <= Settings.high_popularity_threshold:
                hits[1] += 1
            else:
                hits[2] += 1

    # print('HITS:', hr)
    # print('len(scores):', len(scores))

    return hr / len(scores), \
           custom_hr / len(scores), \
           weighted_hr / len(scores), \
           ndcg / len(scores), \
           hits, \
           np.around(hits / np.sum(hits), 2), \
           hits[0] / n_low, \
           hits[1] / n_medium, \
           hits[2] / n_high, \
           [n_low, n_medium, n_high]
Example #2
0
def get_eval(scores, items, index, top_n=10):
    """
    if the last element is the correct one, then
    index = len(scores[0])-1
    """
    # print('>>>>>>>>>>>>>>>> scores:', scores)
    # print('>>>>>>>>>>>>>>>> items:', items)
    eps = 1e-15

    ndcg = 0.0
    custom_hr = 0.0
    weighted_hr = 0.0
    hr = 0.0
    hits = np.array([0, 0, 0])

    assert len(scores[0]) > index and index >= 0

    # for score in scores:
    for i in range(len(scores)):
        score = scores[i]
        item_array = items[i]

        # print('index:', i, len(scores))
        # print('score:', score)
        # print('item:', item_array)

        # Get the top n indices
        arg_index = np.argsort(-score)[:top_n]

        if index in arg_index:
            # print('index, arg_index:', index, '-', arg_index)

            current_item = item_array[index]
            current_popularity = Settings.normalized_popularity[current_item]
            current_position = np.where(arg_index == index)[0][0]
            '''
            print('current_item:', current_item)
            print('current_popularity:', current_popularity)
            print('current_position:', current_position)
            print('arg_index:', arg_index)
            '''

            # Get the position
            ndcg += np.log(2.0) / np.log(arg_index.tolist().index(index) + 2.0)

            # Increment
            hr += 1.0

            # Custom HR
            custom_hr += y_custom(current_popularity, current_position, top_n)

            # Custom HR
            weighted_hr += (1 - current_popularity)
            # weighted_hr += sigmoid(1 / (current_popularity + eps))
            if current_popularity <= 0.33:
                hits[0] += 1
            elif current_popularity > 0.33 and current_popularity <= 0.66:
                hits[1] += 1
            else:
                hits[2] += 1

    # print('HITS:', hr)
    # print('len(scores):', len(scores))

    return hr / len(scores), custom_hr / len(scores), weighted_hr / len(
        scores), ndcg / len(scores), hits, np.around(hits / np.sum(hits), 2)