{"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 pandas as pd\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-24T10:08:21.148457Z","iopub.execute_input":"2022-02-24T10:08:21.148816Z","iopub.status.idle":"2022-02-24T10:08:21.153633Z","shell.execute_reply.started":"2022-02-24T10:08:21.148776Z","shell.execute_reply":"2022-02-24T10:08:21.152767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load files","metadata":{}},{"cell_type":"code","source":"general_path = '../input/h-and-m-personalized-fashion-recommendations/'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:08:21.155279Z","iopub.execute_input":"2022-02-24T10:08:21.155521Z","iopub.status.idle":"2022-02-24T10:08:21.172539Z","shell.execute_reply.started":"2022-02-24T10:08:21.155491Z","shell.execute_reply":"2022-02-24T10:08:21.171711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv(general_path + 'articles.csv')\ncustomers = pd.read_csv(general_path + 'customers.csv')\nsample_submission = pd.read_csv(general_path + 'sample_submission.csv')\ntransactions_train = pd.read_csv(general_path + 'transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:08:21.174345Z","iopub.execute_input":"2022-02-24T10:08:21.174681Z","iopub.status.idle":"2022-02-24T10:09:23.707366Z","shell.execute_reply.started":"2022-02-24T10:08:21.174637Z","shell.execute_reply":"2022-02-24T10:09:23.706428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get info about transactions","metadata":{}},{"cell_type":"code","source":"transactions_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:09:23.708699Z","iopub.execute_input":"2022-02-24T10:09:23.709071Z","iopub.status.idle":"2022-02-24T10:09:23.720924Z","shell.execute_reply.started":"2022-02-24T10:09:23.709036Z","shell.execute_reply":"2022-02-24T10:09:23.719984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.sample(5)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:09:23.725777Z","iopub.execute_input":"2022-02-24T10:09:23.72619Z","iopub.status.idle":"2022-02-24T10:09:25.535238Z","shell.execute_reply.started":"2022-02-24T10:09:23.726144Z","shell.execute_reply":"2022-02-24T10:09:25.534507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['article_id'] = '0' + transactions_train['article_id'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:09:25.536222Z","iopub.execute_input":"2022-02-24T10:09:25.536431Z","iopub.status.idle":"2022-02-24T10:10:10.115053Z","shell.execute_reply.started":"2022-02-24T10:09:25.536407Z","shell.execute_reply":"2022-02-24T10:10:10.113946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['article_id'] = '0' + articles['article_id'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:10.116422Z","iopub.execute_input":"2022-02-24T10:10:10.116683Z","iopub.status.idle":"2022-02-24T10:10:10.259768Z","shell.execute_reply.started":"2022-02-24T10:10:10.116654Z","shell.execute_reply":"2022-02-24T10:10:10.258798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We should transform 't_dat' to datetime format.","metadata":{}},{"cell_type":"code","source":"transactions_train['t_dat'] = pd.to_datetime(transactions_train['t_dat'], format='%Y-%m-%d')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:10.261436Z","iopub.execute_input":"2022-02-24T10:10:10.26195Z","iopub.status.idle":"2022-02-24T10:10:16.715341Z","shell.execute_reply.started":"2022-02-24T10:10:10.261903Z","shell.execute_reply":"2022-02-24T10:10:16.714588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"maximum_history_date = transactions_train['t_dat'].max()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:16.716715Z","iopub.execute_input":"2022-02-24T10:10:16.717449Z","iopub.status.idle":"2022-02-24T10:10:16.823955Z","shell.execute_reply.started":"2022-02-24T10:10:16.717408Z","shell.execute_reply":"2022-02-24T10:10:16.822837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating features for personal recomendation\n## Location","metadata":{}},{"cell_type":"code","source":"print(f'We have {len(customers)} unique customers')\nprint(f'And {len(customers[\"postal_code\"].unique())} unique locations')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:16.825375Z","iopub.execute_input":"2022-02-24T10:10:16.825723Z","iopub.status.idle":"2022-02-24T10:10:17.395845Z","shell.execute_reply.started":"2022-02-24T10:10:16.825679Z","shell.execute_reply":"2022-02-24T10:10:17.394997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_per_location = customers.pivot_table(index='postal_code', aggfunc={'customer_id': 'count'})","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:17.397529Z","iopub.execute_input":"2022-02-24T10:10:17.39789Z","iopub.status.idle":"2022-02-24T10:10:19.233106Z","shell.execute_reply.started":"2022-02-24T10:10:17.39783Z","shell.execute_reply":"2022-02-24T10:10:19.232056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_per_location.sort_values(by=['customer_id'])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:19.234753Z","iopub.execute_input":"2022-02-24T10:10:19.235245Z","iopub.status.idle":"2022-02-24T10:10:19.29612Z","shell.execute_reply.started":"2022-02-24T10:10:19.23521Z","shell.execute_reply":"2022-02-24T10:10:19.295528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We could split all customers for two group:\n1. 2c29ae653a9282cce4151bd87643c907644e09541abc28ae87dea0d1f6603b1c - big_city\n2. Others","metadata":{}},{"cell_type":"code","source":"customers['location'] = 'other'\ncustomers.loc[customers['postal_code'] == '2c29ae653a9282cce4151bd87643c907644e09541abc28ae87dea0d1f6603b1c',\n          'location'] = 'big_city'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:19.299285Z","iopub.execute_input":"2022-02-24T10:10:19.299638Z","iopub.status.idle":"2022-02-24T10:10:19.569855Z","shell.execute_reply.started":"2022-02-24T10:10:19.299604Z","shell.execute_reply":"2022-02-24T10:10:19.568944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Price","metadata":{}},{"cell_type":"code","source":"transactions_train['price'].hist(bins=30, figsize=(16, 5))\nplt.title('Prices')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:19.571109Z","iopub.execute_input":"2022-02-24T10:10:19.571352Z","iopub.status.idle":"2022-02-24T10:10:20.617Z","shell.execute_reply.started":"2022-02-24T10:10:19.571323Z","shell.execute_reply":"2022-02-24T10:10:20.616257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Articles characteristics\nWill try to detect most comprehensive category","metadata":{}},{"cell_type":"code","source":"articles.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:20.618176Z","iopub.execute_input":"2022-02-24T10:10:20.618424Z","iopub.status.idle":"2022-02-24T10:10:20.81707Z","shell.execute_reply.started":"2022-02-24T10:10:20.618396Z","shell.execute_reply":"2022-02-24T10:10:20.816348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.sample(1)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:20.818226Z","iopub.execute_input":"2022-02-24T10:10:20.818442Z","iopub.status.idle":"2022-02-24T10:10:20.867366Z","shell.execute_reply.started":"2022-02-24T10:10:20.818416Z","shell.execute_reply":"2022-02-24T10:10:20.866437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['index_name'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:20.868581Z","iopub.execute_input":"2022-02-24T10:10:20.868818Z","iopub.status.idle":"2022-02-24T10:10:20.883934Z","shell.execute_reply.started":"2022-02-24T10:10:20.868789Z","shell.execute_reply":"2022-02-24T10:10:20.883116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can use 'index_name' as a category function that divides articles into: Women's, Men's and children's clothing.","metadata":{}},{"cell_type":"markdown","source":"## Age","metadata":{}},{"cell_type":"code","source":"customers['age_group'] = '<20'\ncustomers.loc[customers['age'] > 20, 'age_group'] = '20-45'\ncustomers.loc[customers['age'] > 45, 'age_group'] = '>45'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:20.885078Z","iopub.execute_input":"2022-02-24T10:10:20.885714Z","iopub.status.idle":"2022-02-24T10:10:20.978776Z","shell.execute_reply.started":"2022-02-24T10:10:20.885679Z","shell.execute_reply":"2022-02-24T10:10:20.977935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's use features than I previously create\n## Step 1: Create a view in which we can see clients preferences","metadata":{}},{"cell_type":"code","source":"def aggregate_to_list(row):\n    return [i for i in row]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:20.980019Z","iopub.execute_input":"2022-02-24T10:10:20.98032Z","iopub.status.idle":"2022-02-24T10:10:20.984657Z","shell.execute_reply.started":"2022-02-24T10:10:20.980281Z","shell.execute_reply":"2022-02-24T10:10:20.983714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's add article characteristics for each transaction.\ntransactions_train = pd.merge(transactions_train,\n                              articles[['article_id', 'index_name']],\n                              on='article_id',\n                              how='left')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:20.985798Z","iopub.execute_input":"2022-02-24T10:10:20.986058Z","iopub.status.idle":"2022-02-24T10:10:37.227294Z","shell.execute_reply.started":"2022-02-24T10:10:20.986029Z","shell.execute_reply":"2022-02-24T10:10:37.226383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's add client age_group for each transaction.\ntransactions_train = pd.merge(transactions_train,\n                              customers[['customer_id', 'age_group']],\n                              on='customer_id',\n                              how='left')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:37.228692Z","iopub.execute_input":"2022-02-24T10:10:37.228955Z","iopub.status.idle":"2022-02-24T10:10:55.499372Z","shell.execute_reply.started":"2022-02-24T10:10:37.228924Z","shell.execute_reply":"2022-02-24T10:10:55.498539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = customers[['customer_id', 'age_group']]\nsample_submission = pd.merge(sample_submission,\n                             transactions_train.pivot_table(index=['customer_id', 'index_name'],\n                                                            aggfunc={'article_id': ['count', \n                                                                                    aggregate_to_list]}\n                                                           ).reset_index(),\n                             on='customer_id',\n                             how='left')\n\nsample_submission.columns = ['customer_id', 'age_group', 'index_name', 'article_purchased', \n                             'article_id_count']","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:10:55.500793Z","iopub.execute_input":"2022-02-24T10:10:55.50102Z","iopub.status.idle":"2022-02-24T10:12:19.119303Z","shell.execute_reply.started":"2022-02-24T10:10:55.500995Z","shell.execute_reply":"2022-02-24T10:12:19.118375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 2: Remove not actual articules\nH&M is (probably) a fast fashion company, and we can remove some products that have not been sold for a long time.","metadata":{}},{"cell_type":"code","source":"articles_sale_interval = transactions_train.pivot_table(index='article_id', aggfunc={'t_dat': ['min', 'max']}).reset_index()\narticles_sale_interval.columns = [i[0] if (pd.isna(i[1]) or i[1] == '') else i[0] + '_' + i[1] for i in articles_sale_interval.columns]\nlong_time_have_not_sold = articles_sale_interval[articles_sale_interval['t_dat_max'] < maximum_history_date - pd.Timedelta('30 days')]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:19.120634Z","iopub.execute_input":"2022-02-24T10:12:19.120852Z","iopub.status.idle":"2022-02-24T10:12:25.163726Z","shell.execute_reply.started":"2022-02-24T10:12:19.120826Z","shell.execute_reply":"2022-02-24T10:12:25.162815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Remove this styles","metadata":{}},{"cell_type":"code","source":"len_before = len(transactions_train)\ntransactions_train = transactions_train[~transactions_train['article_id'].isin(long_time_have_not_sold['article_id'])]\nprint('Removed {:.2%} of articles'.format(1 - (len(transactions_train) / len_before)))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:25.164936Z","iopub.execute_input":"2022-02-24T10:12:25.165193Z","iopub.status.idle":"2022-02-24T10:12:37.769095Z","shell.execute_reply.started":"2022-02-24T10:12:25.165162Z","shell.execute_reply":"2022-02-24T10:12:37.768183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train = transactions_train[transactions_train['t_dat'] > \n                                        transactions_train['t_dat'].max() - pd.Timedelta('15 days')]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:37.77066Z","iopub.execute_input":"2022-02-24T10:12:37.771178Z","iopub.status.idle":"2022-02-24T10:12:38.529772Z","shell.execute_reply.started":"2022-02-24T10:12:37.771132Z","shell.execute_reply":"2022-02-24T10:12:38.528861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 3: Creating recommendation ratings","metadata":{}},{"cell_type":"code","source":"# Group all articles into groups by age and characteristics of the articles\ngroup_recomendation = transactions_train.pivot_table(index=['age_group', 'index_name', 'article_id'],\n                                                     aggfunc={'customer_id': 'count'}).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:38.531194Z","iopub.execute_input":"2022-02-24T10:12:38.531592Z","iopub.status.idle":"2022-02-24T10:12:39.127035Z","shell.execute_reply.started":"2022-02-24T10:12:38.531545Z","shell.execute_reply":"2022-02-24T10:12:39.126076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sort, remove small, and rename\ngroup_recomendation.sort_values(by=['age_group', 'index_name', 'customer_id'], ascending=False, \n                                inplace=True)\ngroup_recomendation.query('customer_id > 2', inplace=True)\ngroup_recomendation.rename({'customer_id': 'article_raiting'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:39.128262Z","iopub.execute_input":"2022-02-24T10:12:39.1285Z","iopub.status.idle":"2022-02-24T10:12:39.194298Z","shell.execute_reply.started":"2022-02-24T10:12:39.128471Z","shell.execute_reply":"2022-02-24T10:12:39.193415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 4: Add top articles for each group for each client","metadata":{}},{"cell_type":"code","source":"sample_submission = pd.merge(sample_submission,\n                             group_recomendation.pivot_table(index=['age_group', 'index_name'],\n                                                             aggfunc={'article_id': aggregate_to_list}\n                                                            ).reset_index(),\n                             on=['age_group', 'index_name'],\n                             how='left')\nsample_submission.rename(columns={'article_id' : 'top_article_id'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:39.195737Z","iopub.execute_input":"2022-02-24T10:12:39.196655Z","iopub.status.idle":"2022-02-24T10:12:40.318531Z","shell.execute_reply.started":"2022-02-24T10:12:39.196608Z","shell.execute_reply":"2022-02-24T10:12:40.317579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 5: Determine how many articles you need to recommend to each client so that the total is 12","metadata":{}},{"cell_type":"code","source":"sample_submission['group_possibility'] = sample_submission.groupby('customer_id'\n                                                                  )['article_id_count'].transform('sum')\nsample_submission.query('group_possibility != 0', inplace=True)\nsample_submission['article_id_count'] /= sample_submission['group_possibility']\nsample_submission['article_id_count'] *= 12\nsample_submission['article_id_count'] = sample_submission['article_id_count'].astype(float).round().astype('Int64')\nsample_submission.rename(columns={'article_id_count': 'qty_to_recomend'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:40.32003Z","iopub.execute_input":"2022-02-24T10:12:40.320387Z","iopub.status.idle":"2022-02-24T10:12:44.191045Z","shell.execute_reply.started":"2022-02-24T10:12:40.320355Z","shell.execute_reply":"2022-02-24T10:12:44.190252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:44.192271Z","iopub.execute_input":"2022-02-24T10:12:44.192502Z","iopub.status.idle":"2022-02-24T10:12:44.202392Z","shell.execute_reply.started":"2022-02-24T10:12:44.192475Z","shell.execute_reply":"2022-02-24T10:12:44.201514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = sample_submission[sample_submission['qty_to_recomend'] != 0]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:44.203858Z","iopub.execute_input":"2022-02-24T10:12:44.204105Z","iopub.status.idle":"2022-02-24T10:12:44.800802Z","shell.execute_reply.started":"2022-02-24T10:12:44.204077Z","shell.execute_reply":"2022-02-24T10:12:44.799934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 6: From top remove styles which client already bought.","metadata":{}},{"cell_type":"code","source":"def articles_remove(row):\n    bought = row['article_purchased']\n    recomendation = []\n    if type(bought) != list:\n        recomendation.append(row['top_article_id'])\n    i = 0\n    while len(recomendation) < row['qty_to_recomend'] and i < len(row['top_article_id']):\n        current_acticle = row['top_article_id'][i]\n        i += 1\n        if current_acticle in bought:\n            continue\n            \n        if i == len(row['top_article_id']):\n            break\n        recomendation.append(current_acticle)\n    return recomendation","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:44.802127Z","iopub.execute_input":"2022-02-24T10:12:44.802433Z","iopub.status.idle":"2022-02-24T10:12:44.811468Z","shell.execute_reply.started":"2022-02-24T10:12:44.802393Z","shell.execute_reply":"2022-02-24T10:12:44.810373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission[sample_submission['customer_id'] == '0118ed570ff6ff085cde55a6e801c6861a4e9ff9a8d9e82ee36b33c8b3af8f59']","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:44.81373Z","iopub.execute_input":"2022-02-24T10:12:44.814225Z","iopub.status.idle":"2022-02-24T10:12:45.422478Z","shell.execute_reply.started":"2022-02-24T10:12:44.81419Z","shell.execute_reply":"2022-02-24T10:12:45.421622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['recomendation'] = sample_submission.apply(articles_remove, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:12:45.423866Z","iopub.execute_input":"2022-02-24T10:12:45.424127Z","iopub.status.idle":"2022-02-24T10:19:45.847196Z","shell.execute_reply.started":"2022-02-24T10:12:45.424098Z","shell.execute_reply":"2022-02-24T10:19:45.846108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lists_aggregate_to_list(row):\n    output = []\n    for i in row:\n\n        output += i\n    return output","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:19:45.893649Z","iopub.execute_input":"2022-02-24T10:19:45.894008Z","iopub.status.idle":"2022-02-24T10:19:45.897844Z","shell.execute_reply.started":"2022-02-24T10:19:45.893966Z","shell.execute_reply":"2022-02-24T10:19:45.897238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = sample_submission.pivot_table(index='customer_id',\n                                                  aggfunc={'recomendation': lists_aggregate_to_list}\n                                                 ).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:19:45.902407Z","iopub.execute_input":"2022-02-24T10:19:45.902748Z","iopub.status.idle":"2022-02-24T10:20:06.980124Z","shell.execute_reply.started":"2022-02-24T10:19:45.902712Z","shell.execute_reply":"2022-02-24T10:20:06.979168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unknown_customer_id = customers[~customers['customer_id'].isin(sample_submission['customer_id'])]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:20:06.981801Z","iopub.execute_input":"2022-02-24T10:20:06.982177Z","iopub.status.idle":"2022-02-24T10:20:07.497136Z","shell.execute_reply.started":"2022-02-24T10:20:06.982133Z","shell.execute_reply":"2022-02-24T10:20:07.496355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unknown_customer_id = unknown_customer_id[['customer_id']]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:20:07.498461Z","iopub.execute_input":"2022-02-24T10:20:07.498911Z","iopub.status.idle":"2022-02-24T10:20:07.505742Z","shell.execute_reply.started":"2022-02-24T10:20:07.498847Z","shell.execute_reply":"2022-02-24T10:20:07.50487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unknown_customer_id['prediction'] = ' '.join((transactions_train.pivot_table(index='article_id', \n                                                                             aggfunc={'customer_id': \n                                                                                      'count'})\n                                                                .reset_index()\n                                                                .sort_values(by='customer_id',\n                                                                             ascending=False)\n                                                )['article_id'].values[:12])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:20:07.508577Z","iopub.execute_input":"2022-02-24T10:20:07.510836Z","iopub.status.idle":"2022-02-24T10:20:07.849047Z","shell.execute_reply.started":"2022-02-24T10:20:07.510788Z","shell.execute_reply":"2022-02-24T10:20:07.848143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['prediction'] = sample_submission['recomendation'].str.join(' ')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:21:17.761715Z","iopub.execute_input":"2022-02-24T10:21:17.762028Z","iopub.status.idle":"2022-02-24T10:21:19.168277Z","shell.execute_reply.started":"2022-02-24T10:21:17.761997Z","shell.execute_reply":"2022-02-24T10:21:19.167248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.concat([sample_submission, unknown_customer_id])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:24:48.970504Z","iopub.execute_input":"2022-02-24T10:24:48.97104Z","iopub.status.idle":"2022-02-24T10:24:49.07389Z","shell.execute_reply.started":"2022-02-24T10:24:48.971005Z","shell.execute_reply":"2022-02-24T10:24:49.073188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.drop('recomendation', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:25:28.838377Z","iopub.execute_input":"2022-02-24T10:25:28.83882Z","iopub.status.idle":"2022-02-24T10:25:29.296789Z","shell.execute_reply.started":"2022-02-24T10:25:28.838785Z","shell.execute_reply":"2022-02-24T10:25:29.295869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('predict.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:20:07.873301Z","iopub.status.idle":"2022-02-24T10:20:07.874088Z","shell.execute_reply.started":"2022-02-24T10:20:07.873768Z","shell.execute_reply":"2022-02-24T10:20:07.873797Z"},"trusted":true},"execution_count":null,"outputs":[]}]}