<jupyter_start><jupyter_text>MAP@K Function<jupyter_code># https://www.kaggle.com/c/h-and-m-personalized-fashion-recommendations/discussion/306007
# https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py

def apk(actual, predicted, k=10):
    """
    Computes the average precision at k.
    This function computes the average prescision at k between two lists of
    items.
    Parameters
    ----------
    actual : list
             A list of elements that are to be predicted (order doesn't matter)
    predicted : list
                A list of predicted elements (order does matter)
    k : int, optional
        The maximum number of predicted elements
    Returns
    -------
    score : double
            The average precision at k over the input lists
    """
    
    if len(predicted)>k:
        predicted = predicted[:k]

    score = 0.0
    num_hits = 0.0

    for i,p in enumerate(predicted):
#         print('items 1: ')
        if p in actual and p not in predicted[:i]:
            num_hits += 1.0
            score += num_hits / (i+1.0)
#             print('num_hits: ',num_hits)
#             print('score: ',score)
#             print('final score:', score / min(len(actual), k))
#             print('='*50)
    # remove this case in advance
#     if not actual:
#         return 0.0
    try:
        return score / min(len(actual), k)
    except:
        return 0


def mapk(actual, predicted, k=10):
    """
    Computes the mean average precision at k.
    This function computes the mean average prescision at k between two lists
    of lists of items.
    Parameters
    ----------
    actual : list
             A list of lists of elements that are to be predicted 
             (order doesn't matter in the lists)
    predicted : list
                A list of lists of predicted elements
                (order matters in the lists)
    k : int, optional
        The maximum number of predicted elements
    Returns
    -------
    score : double
            The mean average precision at k over the input lists
    """
#     print([apk(a,p,k) for a,p in zip(actual, predicted)]) 
    return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>path = '../data/processed'
bird = pd.read_csv(os.path.join(path,'submission_bird.csv'))\
        .rename(columns={'last_article':'recent_purchase','recomends':'prediction'})\
        .drop(columns=['last_date','recent_purchase'])
bird['prediction'] = bird['prediction'].apply(lambda x: ' '.join(['0'+i for i in x.split()]))
bird.head()<jupyter_output><empty_output><jupyter_text>Submission files<jupyter_code>path = '../data/processed'
# submit = pd.read_csv('submissions.csv',dtype=str)
mew = pd.read_csv(os.path.join(path,'submissions (2).csv'))
mew_v3 = pd.read_csv(os.path.join(path,'submissions_v3.csv')) #  {'factors': 500, 'iterations': 3, 'regularization': 0.01}
mew_v4 = pd.read_csv(os.path.join(path,'submissions_v4.csv')) #  {'factors': 500, 'iterations': 3, 'regularization': 0.01} & filter_already_liked_items
# mew_v5 = pd.read_csv(os.path.join(path,'submissions_v5.csv')) #  past 1 year {'factors': 50, 'iterations': 15, 'regularization': 0.01}
# mew_v6 = pd.read_csv(os.path.join(path,'submissions_v6.csv')) #  all-time data {'factors': 50, 'iterations': 15, 'regularization': 0.01}
top_l1m = pd.read_csv(os.path.join(path,'top_l1m.csv'))  
got = pd.read_csv(os.path.join(path,'recom_data_got2.csv'))
non = pd.read_csv(os.path.join(path,'submission_full_v1_NON.csv'))
bird = pd.read_csv(os.path.join(path,'submission_bird.csv'))\
        .rename(columns={'last_article':'recent_purchase','recomends':'prediction'})\
        .drop(columns=['last_date','recent_purchase'])



bird['prediction'] = bird['prediction'].apply(lambda x: ' '.join(['0'+i for i in x.split()]))
bird.head()<jupyter_output><empty_output><jupyter_text>Index to Customer_id<jupyter_code># mapping index
path = '../data/processed'
infile = open(os.path.join(path,'index_to_cusId.pkl'),'rb')
index_to_id_dict = pickle.load(infile)
infile.close()<jupyter_output><empty_output><jupyter_text>Transaction file<jupyter_code>path = '../data/processed'
trans = pd.read_pickle(os.path.join(path,'transactions.pkl'))
trans["customer_id"] = trans["customer_id"].map(index_to_id_dict)
trans.head()<jupyter_output><empty_output><jupyter_text>7-day target<jupyter_code>start_dt =  datetime.datetime(2020,9,15)
end_dt = start_dt + timedelta(7)

trans = trans[(trans.t_dat > start_dt) & (trans.t_dat <= end_dt)]
print('Min date: ', trans.t_dat.min())
print('Max date: ', trans.t_dat.max())
print(f'Total Customers: {trans.customer_id.nunique()}')

target = pd.DataFrame(trans.groupby(['customer_id'])['article_id'].apply(lambda x: list(set(x))))\
                        .reset_index()\
                        .rename(columns={'article_id':'actual'})
target['actual'] = target['actual'].apply(lambda x: ' '.join(x))
# weekly_purchased['weekly_purchased_products'] = weekly_purchased['weekly_purchased_products'].apply(lambda x: list(set(x)))
target.head()<jupyter_output>Min date:  2020-09-16 00:00:00
Max date:  2020-09-22 00:00:00
Total Customers: 68984<jupyter_text>Evaluation  Map Target<jupyter_code>join_ = 'left'
new_top_l1m = top_l1m.merge(target, on = 'customer_id',how=join_).fillna('')
new_mew = mew.merge(target, on = 'customer_id',how=join_).fillna('')
new_mew_v3 = mew_v3.merge(target, on = 'customer_id',how=join_).fillna('')
new_mew_v4 = mew_v4.merge(target, on = 'customer_id',how=join_).fillna('')
# new_mew_v5 = mew_v5.merge(target, on = 'customer_id',how=join_).fillna('')
# new_mew_v6 = mew_v6.merge(target, on = 'customer_id',how=join_).fillna('')
new_got = got.merge(target, on = 'customer_id',how=join_).fillna('')
new_non = non.merge(target, on = 'customer_id',how=join_).fillna('')
new_bird = bird.merge(target, on = 'customer_id',how=join_).fillna('')


new_top_l1m['actual'] = new_top_l1m['actual'].apply(lambda x: x.split())
new_mew['actual'] = new_mew['actual'].apply(lambda x: x.split())
new_mew_v3['actual'] = new_mew_v3['actual'].apply(lambda x: x.split())
new_mew_v4['actual'] = new_mew_v4['actual'].apply(lambda x: x.split())
# new_mew_v5['actual'] = new_mew_v5['actual'].apply(lambda x: x.split())
# new_mew_v6['actual'] = new_mew_v6['actual'].apply(lambda x: x.split())
new_got['actual'] = new_got['actual'].apply(lambda x: x.split())
new_non['actual'] = new_non['actual'].apply(lambda x: x.split())

new_bird['actual'] = new_bird['actual'].apply(lambda x: x.split())

new_top_l1m['prediction'] = new_top_l1m['prediction'].apply(lambda x: x.split())
new_mew['prediction'] = new_mew['prediction'].apply(lambda x: x.split())
new_mew_v3['prediction'] = new_mew_v3['prediction'].apply(lambda x: x.split())
new_mew_v4['prediction'] = new_mew_v4['prediction'].apply(lambda x: x.split())
# new_mew_v5['prediction'] = new_mew_v5['prediction'].apply(lambda x: x.split())
# new_mew_v6['prediction'] = new_mew_v6['prediction'].apply(lambda x: x.split())
new_got['prediction'] = new_got['prediction'].apply(lambda x: x.split())
new_non['prediction'] = new_non['prediction'].apply(lambda x: x.split())
new_bird['prediction'] = new_bird['prediction'].apply(lambda x: x.split())

new_top_l1m.head()<jupyter_output><empty_output><jupyter_text>MAP@12<jupyter_code>top_l1m_result = mapk(new_top_l1m['actual'],new_top_l1m['prediction'], k=12)
mew_result = mapk(new_mew['actual'],new_mew['prediction'], k=12)
mew_v3_result = mapk(new_mew_v3['actual'],new_mew_v3['prediction'], k=12)
mew_v4_result = mapk(new_mew_v4['actual'],new_mew_v4['prediction'], k=12)
# mew_v5_result = mapk(new_mew_v5['actual'],new_mew_v5['prediction'], k=12)
# mew_v6_result = mapk(new_mew_v6['actual'],new_mew_v6['prediction'], k=12)
got_result = mapk(new_got['actual'],new_got['prediction'], k=12)
non_result = mapk(new_non['actual'],new_non['prediction'], k=12)
bird_result = mapk(new_bird['actual'],new_bird['prediction'], k=12)

print('mAP@12')
print('top_l1m_result: {:.4%}'.format(top_l1m_result))
print('mew_v2_result: {:.4%}'.format(mew_result))
print('mew_v3_result: {:.4%}'.format(mew_v3_result))
print('mew_v4_result: {:.4%}'.format(mew_v4_result))
# print('mew_v5_result: {:.4%}'.format(mew_v5_result))
# print('mew_v6_result: {:.4%}'.format(mew_v6_result))
print('got_result: {:.4%}'.format(got_result))
print('non_result: {:.4%}'.format(non_result))
print('Pbird_result: {:.4%}'.format(bird_result))
numbers = "{:,}".format(len(new_top_l1m)*12)
print(f'Total recommended items: {numbers}')
print('Approx. puchased items:')
print('\t - top_l1m: {:,}'.format(round(len(new_top_l1m)*12*top_l1m_result),0))
print('\t - mew_v2_result: {:,}'.format(round(len(new_top_l1m)*12*mew_result),0))
print('\t - mew_v3_result: {:,}'.format(round(len(new_top_l1m)*12*mew_v3_result),0))
print('\t - mew_v4_result: {:,}'.format(round(len(new_top_l1m)*12*mew_v4_result),0))
# print('\t - mew_v5_result: {:,}'.format(round(len(new_top_l1m)*12*mew_v5_result),0))
# print('\t - mew_v6_result: {:,}'.format(round(len(new_top_l1m)*12*mew_v6_result),0))
print('\t - got_result: {:,}'.format(round(len(new_top_l1m)*12*got_result),0))
print('\t - non_result: {:,}'.format(round(len(new_top_l1m)*12*non_result),0))
print('\t - bird_result: {:,}'.format(round(len(new_top_l1m)*12*bird_result),0))
print('Pbird_result: {:.4%}'.format(0.05))<jupyter_output>Pbird_result: 5.0000%
{'fraction_non_alphanumeric': 0.185, 'fraction_numerical': 0.027340425531914892, 'mean_word_length': 5.242363877822045, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '40668', 'license': 'Apache-2.0', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3587, 'n_words': 562, 'path': 'model/.ipynb_checkpoints/1.Evaluation_Metric_V2-checkpoint.ipynb', 'repo_name': 'mewprakarn/Product_Personalization_Project', 'size': '9400'}