{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport datetime as dt\nfrom collections import defaultdict","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:36:32.251703Z","iopub.execute_input":"2022-08-10T11:36:32.252317Z","iopub.status.idle":"2022-08-10T11:36:32.279693Z","shell.execute_reply.started":"2022-08-10T11:36:32.252228Z","shell.execute_reply":"2022-08-10T11:36:32.278701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ncustomer_detail = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\nsub = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\narticles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:36:32.352460Z","iopub.execute_input":"2022-08-10T11:36:32.353361Z","iopub.status.idle":"2022-08-10T11:37:35.743319Z","shell.execute_reply.started":"2022-08-10T11:36:32.353322Z","shell.execute_reply":"2022-08-10T11:37:35.742433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def iter_to_str(iterable):\n    return \" \".join(map(lambda x: str(0) + str(x), iterable))\n\n# https://www.kaggle.com/c/h-and-m-personalized-fashion-recommendations/discussion/306007\n# https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py\n\ndef apk(actual, predicted, k=10):\n    \"\"\"\n    Computes the average precision at k.\n    This function computes the average prescision at k between two lists of\n    items.\n    Parameters\n    ----------\n    actual : list\n             A list of elements that are to be predicted (order doesn't matter)\n    predicted : list\n                A list of predicted elements (order does matter)\n    k : int, optional\n        The maximum number of predicted elements\n    Returns\n    -------\n    score : double\n            The average precision at k over the input lists\n    \"\"\"\n    if len(predicted)>k:\n        predicted = predicted[:k]\n\n    score = 0.0\n    num_hits = 0.0\n\n    for i,p in enumerate(predicted):\n        if p in actual and p not in predicted[:i]:\n            num_hits += 1.0\n            score += num_hits / (i+1.0)\n\n    # remove this case in advance\n    # if not actual:\n    #     return 0.0\n\n    return score / min(len(actual), k)\n\n\ndef mapk(actual, predicted, k=10):\n    \"\"\"\n    Computes the mean average precision at k.\n    This function computes the mean average prescision at k between two lists\n    of lists of items.\n    Parameters\n    ----------\n    actual : list\n             A list of lists of elements that are to be predicted \n             (order doesn't matter in the lists)\n    predicted : list\n                A list of lists of predicted elements\n                (order matters in the lists)\n    k : int, optional\n        The maximum number of predicted elements\n    Returns\n    -------\n    score : double\n            The mean average precision at k over the input lists\n    \"\"\"\n    return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:37:35.744726Z","iopub.execute_input":"2022-08-10T11:37:35.744966Z","iopub.status.idle":"2022-08-10T11:37:35.754285Z","shell.execute_reply.started":"2022-08-10T11:37:35.744943Z","shell.execute_reply":"2022-08-10T11:37:35.753384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['t_dat'] = pd.to_datetime(train_csv['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:37:35.755598Z","iopub.execute_input":"2022-08-10T11:37:35.756320Z","iopub.status.idle":"2022-08-10T11:37:39.786700Z","shell.execute_reply.started":"2022-08-10T11:37:35.756265Z","shell.execute_reply":"2022-08-10T11:37:39.785682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_csv.loc[train_csv['t_dat'] < '2020-09-16'].reset_index()\\\n          .merge(customer_detail , on = 'customer_id', how='left')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:37:39.789122Z","iopub.execute_input":"2022-08-10T11:37:39.789418Z","iopub.status.idle":"2022-08-10T11:38:06.660551Z","shell.execute_reply.started":"2022-08-10T11:37:39.789392Z","shell.execute_reply":"2022-08-10T11:38:06.659450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:06.661747Z","iopub.execute_input":"2022-08-10T11:38:06.663060Z","iopub.status.idle":"2022-08-10T11:38:06.685556Z","shell.execute_reply.started":"2022-08-10T11:38:06.663031Z","shell.execute_reply":"2022-08-10T11:38:06.684589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = (lambda x: (x//10) * 10)\ntrain['age'] = train['age'].apply(f)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:06.686675Z","iopub.execute_input":"2022-08-10T11:38:06.686945Z","iopub.status.idle":"2022-08-10T11:38:17.687768Z","shell.execute_reply.started":"2022-08-10T11:38:06.686918Z","shell.execute_reply":"2022-08-10T11:38:17.686786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_detail['age'] = customer_detail['age'].apply(f)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:17.688892Z","iopub.execute_input":"2022-08-10T11:38:17.689161Z","iopub.status.idle":"2022-08-10T11:38:18.161827Z","shell.execute_reply.started":"2022-08-10T11:38:17.689136Z","shell.execute_reply":"2022-08-10T11:38:18.160923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_age = train.groupby(['age' , 'article_id']).customer_id.count().reset_index()\ncustomer_age","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:18.162904Z","iopub.execute_input":"2022-08-10T11:38:18.163188Z","iopub.status.idle":"2022-08-10T11:38:21.292425Z","shell.execute_reply.started":"2022-08-10T11:38:18.163163Z","shell.execute_reply":"2022-08-10T11:38:21.291463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = defaultdict(str)\nfor age in customer_age['age'].unique():\n    values[age] = iter_to_str(customer_age.loc[customer_age['age'] == age].sort_values('customer_id' , ascending = False).article_id.tolist()[:12])","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:21.293830Z","iopub.execute_input":"2022-08-10T11:38:21.294163Z","iopub.status.idle":"2022-08-10T11:38:21.385990Z","shell.execute_reply.started":"2022-08-10T11:38:21.294131Z","shell.execute_reply":"2022-08-10T11:38:21.384885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:21.388807Z","iopub.execute_input":"2022-08-10T11:38:21.389686Z","iopub.status.idle":"2022-08-10T11:38:21.396212Z","shell.execute_reply.started":"2022-08-10T11:38:21.389651Z","shell.execute_reply":"2022-08-10T11:38:21.395076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['prediction'] = customer_detail.age.map(values)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:21.397509Z","iopub.execute_input":"2022-08-10T11:38:21.398193Z","iopub.status.idle":"2022-08-10T11:38:28.805835Z","shell.execute_reply.started":"2022-08-10T11:38:21.398161Z","shell.execute_reply":"2022-08-10T11:38:28.805021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:38:28.806897Z","iopub.execute_input":"2022-08-10T11:38:28.807603Z","iopub.status.idle":"2022-08-10T11:38:28.818557Z","shell.execute_reply.started":"2022-08-10T11:38:28.807575Z","shell.execute_reply":"2022-08-10T11:38:28.817478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_csv = train_csv.loc[train_csv['t_dat'] >= '2020-09-16'].reset_index(drop=True)\nvalid = val_csv.groupby('customer_id')['article_id'].apply(iter_to_str).reset_index()\nvalid = pd.merge(sub, valid, on='customer_id', how='left').fillna('')\nvalid = valid[valid['article_id'] != ''].reset_index(drop=True)        \nvalid                                   ","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:39:51.072752Z","iopub.execute_input":"2022-08-10T11:39:51.073701Z","iopub.status.idle":"2022-08-10T11:39:53.619788Z","shell.execute_reply.started":"2022-08-10T11:39:51.073645Z","shell.execute_reply":"2022-08-10T11:39:53.618440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mapk(\n    valid['article_id'].map(lambda x: x.split()), \n    valid['prediction'].map(lambda x: x.split()), \n    k=12\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:40:12.710586Z","iopub.execute_input":"2022-08-10T11:40:12.710992Z","iopub.status.idle":"2022-08-10T11:40:13.353639Z","shell.execute_reply.started":"2022-08-10T11:40:12.710963Z","shell.execute_reply":"2022-08-10T11:40:13.352385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv' , index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T11:40:26.149657Z","iopub.execute_input":"2022-08-10T11:40:26.150038Z","iopub.status.idle":"2022-08-10T11:40:30.623554Z","shell.execute_reply.started":"2022-08-10T11:40:26.150006Z","shell.execute_reply":"2022-08-10T11:40:30.622470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}