{"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":"#Load required libraries\nimport os\nimport numpy as np\nimport pandas as pd\nimport implicit\nfrom implicit.evaluation import mean_average_precision_at_k\nfrom scipy.sparse import coo_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom datetime import datetime\n","metadata":{"id":"R6b03O9PPk4q","execution":{"iopub.status.busy":"2022-05-01T19:50:38.67253Z","iopub.execute_input":"2022-05-01T19:50:38.673177Z","iopub.status.idle":"2022-05-01T19:50:38.678785Z","shell.execute_reply.started":"2022-05-01T19:50:38.67313Z","shell.execute_reply":"2022-05-01T19:50:38.678015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read csv files for articles, customers and transactions\narticles  = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv', dtype={'article_id': str})\ncustomers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntransactions_train = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',dtype={'article_id': str}, parse_dates = ['t_dat'])\nsample_sub = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"id":"C_sm9WPFPsL2","execution":{"iopub.status.busy":"2022-05-01T19:50:38.706575Z","iopub.execute_input":"2022-05-01T19:50:38.70686Z","iopub.status.idle":"2022-05-01T19:51:26.631075Z","shell.execute_reply.started":"2022-05-01T19:50:38.706832Z","shell.execute_reply":"2022-05-01T19:51:26.630196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.shape","metadata":{"id":"sS_o2qjUP3ln","outputId":"6a34a5c4-f649-408b-ca31-5613e40a139f","execution":{"iopub.status.busy":"2022-05-01T19:51:26.643019Z","iopub.execute_input":"2022-05-01T19:51:26.643456Z","iopub.status.idle":"2022-05-01T19:51:26.652272Z","shell.execute_reply.started":"2022-05-01T19:51:26.643415Z","shell.execute_reply":"2022-05-01T19:51:26.651501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.columns","metadata":{"id":"SIpP7OVhZjPJ","outputId":"819d3b9f-84a0-4beb-c468-f761a75e98f5","execution":{"iopub.status.busy":"2022-05-01T19:51:26.653735Z","iopub.execute_input":"2022-05-01T19:51:26.654255Z","iopub.status.idle":"2022-05-01T19:51:26.665144Z","shell.execute_reply.started":"2022-05-01T19:51:26.654216Z","shell.execute_reply":"2022-05-01T19:51:26.66439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (7,4))\nax = sns.histplot(data=articles, y='index_group_name', color='red')\nax.set_xlabel('count')\nax.set_ylabel('index group name')\nplt.show()","metadata":{"id":"FB9bfpr1Qubk","outputId":"050c9370-3b34-4279-c481-5b321f995c8e","execution":{"iopub.status.busy":"2022-05-01T19:51:26.667651Z","iopub.execute_input":"2022-05-01T19:51:26.668185Z","iopub.status.idle":"2022-05-01T19:51:26.996453Z","shell.execute_reply.started":"2022-05-01T19:51:26.668144Z","shell.execute_reply":"2022-05-01T19:51:26.995748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10, 7))\nax = sns.histplot(data=articles, y='garment_group_name', hue='index_group_name', multiple=\"stack\")\nax.set_xlabel('count by garment group')\nax.set_ylabel('garment group')\nplt.show()","metadata":{"id":"-8ivlzBIR8xN","outputId":"ce9a5621-1618-4cd5-b01c-fefc6a9a7c7e","execution":{"iopub.status.busy":"2022-05-01T19:51:26.997634Z","iopub.execute_input":"2022-05-01T19:51:26.997892Z","iopub.status.idle":"2022-05-01T19:51:27.74745Z","shell.execute_reply.started":"2022-05-01T19:51:26.997857Z","shell.execute_reply":"2022-05-01T19:51:27.746769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['text.color'] = 'black'\nplt.rcParams['axes.labelcolor'] = 'black'\nplt.rcParams['font.size'] = 10\n\nax = articles['product_group_name'].value_counts(normalize=True).mul(100).round(1).plot(kind = 'bar', color = 'blue', figsize = (10, 5),\n                                                    title = 'Bar graph of Product Group Name')\nax.set_xlabel(\"Product Group Name\", fontsize = 12)\nax.set_ylabel(\"Percentage %\", fontsize = 12)\nplt.tick_params(labelsize = 10)\nplt.grid()","metadata":{"id":"ptyouje3SC83","outputId":"179a7ee2-2844-4e21-afeb-a4994fcb12ac","execution":{"iopub.status.busy":"2022-05-01T19:51:27.748896Z","iopub.execute_input":"2022-05-01T19:51:27.749362Z","iopub.status.idle":"2022-05-01T19:51:28.098511Z","shell.execute_reply.started":"2022-05-01T19:51:27.749324Z","shell.execute_reply":"2022-05-01T19:51:28.097769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = articles['product_type_name'].value_counts(normalize=True, ascending = False).mul(100).round(1).plot(kind = 'bar', color = 'purple', figsize = (24, 8),\n                                                         title = 'Bar graphs on Product Type Name')\nax.set_xlabel('Product Type Name', fontsize = 12)\nax.set_ylabel('Percentage %', fontsize=12)\nplt.tick_params(labelsize = 10)\nplt.grid()","metadata":{"id":"lheKx5ZMSPks","outputId":"f6064f97-68f0-45c7-c334-60265ddbeec5","execution":{"iopub.status.busy":"2022-05-01T19:51:28.100074Z","iopub.execute_input":"2022-05-01T19:51:28.100555Z","iopub.status.idle":"2022-05-01T19:51:30.150244Z","shell.execute_reply.started":"2022-05-01T19:51:28.100515Z","shell.execute_reply":"2022-05-01T19:51:30.149593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = articles['graphical_appearance_name'].value_counts(normalize=True, ascending = False).mul(100).round(1).plot(kind = 'bar', color = 'green', figsize = (10, 5),\n                                                            title = 'Bar graph on Graphical Appearance Name')\nax.set_xlabel('Graphical Appearance Name')\nax.set_ylabel('Frequency')\nplt.tick_params(labelsize = 12)\nplt.grid()","metadata":{"id":"2cxhGGUsSVH7","outputId":"294d7270-fd79-4ce8-ae84-364a259bed1c","execution":{"iopub.status.busy":"2022-05-01T19:51:30.151251Z","iopub.execute_input":"2022-05-01T19:51:30.152149Z","iopub.status.idle":"2022-05-01T19:51:30.552176Z","shell.execute_reply.started":"2022-05-01T19:51:30.152108Z","shell.execute_reply":"2022-05-01T19:51:30.551458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.shape","metadata":{"id":"4imFYsbrazup","outputId":"edeecf1d-5c45-4abf-d853-ffd502ddaef4","execution":{"iopub.status.busy":"2022-05-01T19:51:30.553518Z","iopub.execute_input":"2022-05-01T19:51:30.553786Z","iopub.status.idle":"2022-05-01T19:51:30.55917Z","shell.execute_reply.started":"2022-05-01T19:51:30.55375Z","shell.execute_reply":"2022-05-01T19:51:30.558364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.columns","metadata":{"id":"crzgqX80aupF","outputId":"552c267a-fde3-4222-ecb8-5a4cd81a69f6","execution":{"iopub.status.busy":"2022-05-01T19:51:30.563265Z","iopub.execute_input":"2022-05-01T19:51:30.563795Z","iopub.status.idle":"2022-05-01T19:51:30.570291Z","shell.execute_reply.started":"2022-05-01T19:51:30.563755Z","shell.execute_reply":"2022-05-01T19:51:30.56945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['text.color'] = 'black'\nplt.rcParams['axes.labelcolor'] = 'black'\nplt.rcParams['font.size'] = 10\n\nax = customers['club_member_status'].value_counts(normalize=True).mul(100).round(1).plot(kind = 'bar',\n                                                                                        color = 'red', figsize = (8, 5),\n                                                                                        title = 'Bar graph of Club Member Status')\nax.set_xlabel(\"Club Member Status\", fontsize = 10)\nax.set_ylabel(\"Number of Customers\", fontsize = 10)\nplt.tick_params(labelsize = 10)\nplt.grid()","metadata":{"id":"LjUpFPrVSevK","outputId":"bb08dcc5-511d-4bf9-cb19-f5f8de9fd61d","execution":{"iopub.status.busy":"2022-05-01T19:51:30.57193Z","iopub.execute_input":"2022-05-01T19:51:30.572459Z","iopub.status.idle":"2022-05-01T19:51:30.958395Z","shell.execute_reply.started":"2022-05-01T19:51:30.572421Z","shell.execute_reply":"2022-05-01T19:51:30.957706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.shape\n","metadata":{"id":"nMZeTy-kWIiI","outputId":"1137f060-6fd1-4193-c014-b16bf4b8d3fd","execution":{"iopub.status.busy":"2022-05-01T19:51:30.959742Z","iopub.execute_input":"2022-05-01T19:51:30.960206Z","iopub.status.idle":"2022-05-01T19:51:30.965624Z","shell.execute_reply.started":"2022-05-01T19:51:30.960169Z","shell.execute_reply":"2022-05-01T19:51:30.964601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.columns","metadata":{"id":"wPnGpf5UUq5a","outputId":"6a3629a5-2287-446f-8018-e1aad12c8a96","execution":{"iopub.status.busy":"2022-05-01T19:51:30.967156Z","iopub.execute_input":"2022-05-01T19:51:30.967719Z","iopub.status.idle":"2022-05-01T19:51:30.9762Z","shell.execute_reply.started":"2022-05-01T19:51:30.967684Z","shell.execute_reply":"2022-05-01T19:51:30.975354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (10, 7))\nplt.xlim(0, 0.3)\nax = sns.histplot(data = transactions_train, x = 'price', bins = 75, color = 'green')\nax.set_xlabel('Distribution of price')\nplt.show()\n","metadata":{"id":"kTwwmbH6TtEX","outputId":"66858ef4-b21e-4108-c16b-a05040bb6781","execution":{"iopub.status.busy":"2022-05-01T19:51:30.977709Z","iopub.execute_input":"2022-05-01T19:51:30.978023Z","iopub.status.idle":"2022-05-01T19:51:38.660626Z","shell.execute_reply.started":"2022-05-01T19:51:30.977988Z","shell.execute_reply":"2022-05-01T19:51:38.659952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (12,8))\nax = sns.boxplot(data = transactions_train, x = 'price', color = \"green\")\nax.set_xlabel('Price boxplot')\nplt.show()","metadata":{"id":"cXes8iXySmSP","outputId":"b081205d-e952-4ddf-e30e-4df81c61ca68","execution":{"iopub.status.busy":"2022-05-01T19:51:38.661952Z","iopub.execute_input":"2022-05-01T19:51:38.66237Z","iopub.status.idle":"2022-05-01T19:51:42.677642Z","shell.execute_reply.started":"2022-05-01T19:51:38.662323Z","shell.execute_reply":"2022-05-01T19:51:42.676701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_data = transactions_train.groupby('sales_channel_id').count()['customer_id']\npie_data","metadata":{"id":"a88yrz4JVgAT","outputId":"069e946d-77f3-4a82-9d61-2c4a09fa63fc","execution":{"iopub.status.busy":"2022-05-01T19:51:42.679Z","iopub.execute_input":"2022-05-01T19:51:42.679498Z","iopub.status.idle":"2022-05-01T19:51:51.000044Z","shell.execute_reply.started":"2022-05-01T19:51:42.679455Z","shell.execute_reply":"2022-05-01T19:51:50.999314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (10, 7))\ncolors = sns.color_palette('deep')\nax.pie(pie_data, labels=['Online', 'Offline'], colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of sales channel')\nplt.show()","metadata":{"id":"0yznupL3VTLu","outputId":"4c10763a-794b-4c2c-d2ed-68eb9e3e53fb","execution":{"iopub.status.busy":"2022-05-01T19:51:51.003715Z","iopub.execute_input":"2022-05-01T19:51:51.005845Z","iopub.status.idle":"2022-05-01T19:51:51.095472Z","shell.execute_reply.started":"2022-05-01T19:51:51.005805Z","shell.execute_reply":"2022-05-01T19:51:51.094665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Max transaction date: ',transactions_train.t_dat.max())\nprint('Min transaction date: ', transactions_train.t_dat.min())","metadata":{"id":"chdbGzoAVdd4","outputId":"0603ee77-d570-413f-a3be-4881642f6fb3","execution":{"iopub.status.busy":"2022-05-01T19:51:51.096603Z","iopub.execute_input":"2022-05-01T19:51:51.096858Z","iopub.status.idle":"2022-05-01T19:51:51.333058Z","shell.execute_reply.started":"2022-05-01T19:51:51.096824Z","shell.execute_reply":"2022-05-01T19:51:51.331498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reducing the data to 1 month as the data is huge\ntransactions_train = transactions_train[transactions_train['t_dat'] > '2020-08-21']\ntransactions_train.shape","metadata":{"id":"yVG2pzffWPgn","outputId":"dee45868-fb56-4d6b-c62b-af20dae326f1","execution":{"iopub.status.busy":"2022-05-01T19:51:51.334617Z","iopub.execute_input":"2022-05-01T19:51:51.334893Z","iopub.status.idle":"2022-05-01T19:51:52.078647Z","shell.execute_reply.started":"2022-05-01T19:51:51.334855Z","shell.execute_reply":"2022-05-01T19:51:52.077778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.head()","metadata":{"id":"spcO0a-AWWZr","outputId":"b6bde3b8-1b10-4398-886c-6a6d3c98af66","execution":{"iopub.status.busy":"2022-05-01T19:51:52.080885Z","iopub.execute_input":"2022-05-01T19:51:52.081514Z","iopub.status.idle":"2022-05-01T19:51:52.095413Z","shell.execute_reply.started":"2022-05-01T19:51:52.081471Z","shell.execute_reply":"2022-05-01T19:51:52.094775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def user_item_list():\n  user_list = customers['customer_id'].unique().tolist()\n  item_list = articles['article_id'].unique().tolist()\n\n  user_ids = dict(list(enumerate(user_list)))\n  item_ids = dict(list(enumerate(item_list)))\n\n  user_map = {u: uidx for uidx, u in user_ids.items()}\n  item_map = {i: iidx for iidx, i in item_ids.items()}\n\n  transactions_train['user_id'] = transactions_train['customer_id'].map(user_map)\n  transactions_train['item_id'] = transactions_train['article_id'].map(item_map)\n  return transactions_train, user_list, item_list\n","metadata":{"id":"WmBdNtGgWZHr","execution":{"iopub.status.busy":"2022-05-01T19:51:52.096481Z","iopub.execute_input":"2022-05-01T19:51:52.09679Z","iopub.status.idle":"2022-05-01T19:51:52.103639Z","shell.execute_reply.started":"2022-05-01T19:51:52.096753Z","shell.execute_reply":"2022-05-01T19:51:52.102837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions, user_list, item_list = user_item_list()","metadata":{"id":"YYUrOGtLWeK2","execution":{"iopub.status.busy":"2022-05-01T19:51:52.105069Z","iopub.execute_input":"2022-05-01T19:51:52.105401Z","iopub.status.idle":"2022-05-01T19:51:54.67941Z","shell.execute_reply.started":"2022-05-01T19:51:52.105292Z","shell.execute_reply":"2022-05-01T19:51:54.67858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head()","metadata":{"id":"L7Buy3PDWjFV","outputId":"bbbb8027-39ef-4047-c54e-ef5e5f24de49","execution":{"iopub.status.busy":"2022-05-01T19:51:54.681025Z","iopub.execute_input":"2022-05-01T19:51:54.681314Z","iopub.status.idle":"2022-05-01T19:51:54.695697Z","shell.execute_reply.started":"2022-05-01T19:51:54.681276Z","shell.execute_reply":"2022-05-01T19:51:54.694511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generating training dataset\ndef training_data(transactions) -> pd.DataFrame:\n  user_ids = transactions['user_id'].values\n  item_ids = transactions['item_id'].values\n  data = np.ones(transactions.shape[0])\n  sparse_train = coo_matrix((data, (user_ids, item_ids)), shape=(len(user_list), len(item_list)))\n  return sparse_train","metadata":{"id":"gRAiLKQ6Wm1o","execution":{"iopub.status.busy":"2022-05-01T19:51:54.697581Z","iopub.execute_input":"2022-05-01T19:51:54.698267Z","iopub.status.idle":"2022-05-01T19:51:54.705277Z","shell.execute_reply.started":"2022-05-01T19:51:54.698204Z","shell.execute_reply":"2022-05-01T19:51:54.704407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_val_split(transactions, val_days=7):\n    \"\"\" \n    Splitting data into train and val\n    \"\"\"\n    val_cut = transactions['t_dat'].max() - pd.Timedelta(val_days)\n\n    df_train = transactions[transactions['t_dat'] < val_cut]\n    df_val = transactions[transactions['t_dat'] >= val_cut]\n    return df_train, df_val\n","metadata":{"id":"QmlcqyvAWqAD","execution":{"iopub.status.busy":"2022-05-01T19:51:54.707205Z","iopub.execute_input":"2022-05-01T19:51:54.708036Z","iopub.status.idle":"2022-05-01T19:51:54.715333Z","shell.execute_reply.started":"2022-05-01T19:51:54.707989Z","shell.execute_reply":"2022-05-01T19:51:54.714432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_val_matrices(transactions, val_days=7):\n    \"\"\" \n    Data split into train and val and get sparse matrices\n    \n    \"\"\"\n    df_train, df_val = train_val_split(transactions, val_days=val_days)\n    sparse_train = training_data(df_train)\n    sparse_val = training_data(df_val)\n\n    csr_matrix_train = sparse_train.tocsr()\n    csr_matrix_val = sparse_val.tocsr()\n    \n    return {'coo_train': sparse_train,\n            'csr_train': csr_matrix_train,\n            'csr_val': csr_matrix_val\n          }","metadata":{"id":"BImh1ee5WuZa","execution":{"iopub.status.busy":"2022-05-01T19:51:54.716968Z","iopub.execute_input":"2022-05-01T19:51:54.717359Z","iopub.status.idle":"2022-05-01T19:51:54.727366Z","shell.execute_reply.started":"2022-05-01T19:51:54.717318Z","shell.execute_reply":"2022-05-01T19:51:54.726391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mats = train_val_matrices(transactions)\nsparse_train, csr_matrix_train, csr_matrix_val = mats['coo_train'], mats['csr_train'], mats['csr_val']","metadata":{"id":"0V_qPNU3WzY6","execution":{"iopub.status.busy":"2022-05-01T19:51:54.729109Z","iopub.execute_input":"2022-05-01T19:51:54.729566Z","iopub.status.idle":"2022-05-01T19:51:54.900615Z","shell.execute_reply.started":"2022-05-01T19:51:54.729527Z","shell.execute_reply":"2022-05-01T19:51:54.899823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade implicit","metadata":{"execution":{"iopub.status.busy":"2022-05-01T19:51:54.90543Z","iopub.execute_input":"2022-05-01T19:51:54.90565Z","iopub.status.idle":"2022-05-01T19:52:04.093772Z","shell.execute_reply.started":"2022-05-01T19:51:54.905622Z","shell.execute_reply":"2022-05-01T19:52:04.09276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Define and fit ALS model and get mean average precision value of the model    \nmodel = implicit.als.AlternatingLeastSquares(factors=200, \n                                                 iterations=20, \n                                                 regularization=0.01, \n                                                 random_state=42)\nmodel.fit(sparse_train, show_progress=True)\n  \n# Get map value of the model\nmap12 = mean_average_precision_at_k(model, csr_matrix_train, csr_matrix_val, K=12, show_progress=True, num_threads=-1)","metadata":{"id":"9_GvCla0W2j3","outputId":"8d5ded98-41d4-49fa-c0b2-6db44170b88e","execution":{"iopub.status.busy":"2022-05-01T19:52:04.096702Z","iopub.execute_input":"2022-05-01T19:52:04.09701Z","iopub.status.idle":"2022-05-01T19:52:08.920488Z","shell.execute_reply.started":"2022-05-01T19:52:04.096969Z","shell.execute_reply":"2022-05-01T19:52:08.919557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map12","metadata":{"id":"-aGIJuXtY5hd","outputId":"a819f557-8f32-4246-d521-a9a098c9cf75","execution":{"iopub.status.busy":"2022-05-01T19:52:08.922045Z","iopub.execute_input":"2022-05-01T19:52:08.922325Z","iopub.status.idle":"2022-05-01T19:52:08.928606Z","shell.execute_reply.started":"2022-05-01T19:52:08.922287Z","shell.execute_reply":"2022-05-01T19:52:08.927789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model on full data(without validation set)\nsparse_train = training_data(transactions)\ncsr__matrix_train = sparse_train.tocsr()","metadata":{"id":"sc7IRwWRY94C","execution":{"iopub.status.busy":"2022-05-01T19:52:08.929995Z","iopub.execute_input":"2022-05-01T19:52:08.930267Z","iopub.status.idle":"2022-05-01T19:52:09.019701Z","shell.execute_reply.started":"2022-05-01T19:52:08.93023Z","shell.execute_reply":"2022-05-01T19:52:09.01896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = implicit.als.AlternatingLeastSquares(factors=200, \n                                                 iterations=20, \n                                                 regularization=0.01, \n                                                 random_state=42)\nmodel.fit(sparse_train, show_progress=True)","metadata":{"id":"OrFfwmqeZCRi","outputId":"bbb42f7b-05c1-470c-fd8a-4a62987e3ba6","execution":{"iopub.status.busy":"2022-05-01T19:52:09.02123Z","iopub.execute_input":"2022-05-01T19:52:09.021499Z","iopub.status.idle":"2022-05-01T19:52:13.473975Z","shell.execute_reply.started":"2022-05-01T19:52:09.02146Z","shell.execute_reply":"2022-05-01T19:52:13.473221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate submission file\nuser_ids = dict(list(enumerate(user_list)))\nitem_ids = dict(list(enumerate(item_list)))\npreds = []\nbatch_size = 1000\nto_generate = np.arange(len(user_list))\nfor idx in range(0, len(to_generate), batch_size):\n    batch = to_generate[idx : idx + batch_size]\n    ids, scores = model.recommend(batch, csr__matrix_train[batch], N=12, filter_already_liked_items=False)\n    for i, userid in enumerate(batch):\n        customer_id = user_ids[userid]\n        user_items = ids[i]\n        article_ids = [item_ids[item_id] for item_id in user_items]\n        preds.append((customer_id, ' '.join(article_ids)))\n\ndf_preds = pd.DataFrame(preds, columns=['customer_id', 'prediction'])\n","metadata":{"id":"BK3XUUjxZGTp","outputId":"82609d2d-2d2d-4d3b-a1de-5506f8d7e4c6","execution":{"iopub.status.busy":"2022-05-01T19:52:13.475344Z","iopub.execute_input":"2022-05-01T19:52:13.475752Z","iopub.status.idle":"2022-05-01T19:53:13.164719Z","shell.execute_reply.started":"2022-05-01T19:52:13.475713Z","shell.execute_reply":"2022-05-01T19:53:13.163896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(df_preds.head())\nprint(df_preds.shape)","metadata":{"id":"VioNxnuiZXRs","execution":{"iopub.status.busy":"2022-05-01T19:53:13.166214Z","iopub.execute_input":"2022-05-01T19:53:13.166614Z","iopub.status.idle":"2022-05-01T19:53:13.178668Z","shell.execute_reply.started":"2022-05-01T19:53:13.166575Z","shell.execute_reply":"2022-05-01T19:53:13.177968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds.to_csv('submission.csv', index=False)    ","metadata":{"execution":{"iopub.status.busy":"2022-05-01T19:53:13.180083Z","iopub.execute_input":"2022-05-01T19:53:13.180608Z","iopub.status.idle":"2022-05-01T19:53:24.777395Z","shell.execute_reply.started":"2022-05-01T19:53:13.18057Z","shell.execute_reply":"2022-05-01T19:53:24.776594Z"},"trusted":true},"execution_count":null,"outputs":[]}]}