{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\n\ninput_dir = '/kaggle/input/h-and-m-personalized-fashion-recommendations/'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-29T04:51:52.874871Z","iopub.execute_input":"2022-03-29T04:51:52.875780Z","iopub.status.idle":"2022-03-29T04:51:53.965134Z","shell.execute_reply.started":"2022-03-29T04:51:52.875675Z","shell.execute_reply":"2022-03-29T04:51:53.964202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_path = input_dir + 'articles.csv'\narticles = pd.read_csv(articles_path, index_col='article_id')\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:53.967468Z","iopub.execute_input":"2022-03-29T04:51:53.967967Z","iopub.status.idle":"2022-03-29T04:51:55.127077Z","shell.execute_reply.started":"2022-03-29T04:51:53.967922Z","shell.execute_reply":"2022-03-29T04:51:55.126135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.128723Z","iopub.execute_input":"2022-03-29T04:51:55.129295Z","iopub.status.idle":"2022-03-29T04:51:55.297156Z","shell.execute_reply.started":"2022-03-29T04:51:55.129253Z","shell.execute_reply":"2022-03-29T04:51:55.296086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.300450Z","iopub.execute_input":"2022-03-29T04:51:55.300706Z","iopub.status.idle":"2022-03-29T04:51:55.329388Z","shell.execute_reply.started":"2022-03-29T04:51:55.300673Z","shell.execute_reply":"2022-03-29T04:51:55.328384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_features = ['product_type_no', 'graphical_appearance_no', 'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n                    'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no']","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.330953Z","iopub.execute_input":"2022-03-29T04:51:55.331274Z","iopub.status.idle":"2022-03-29T04:51:55.339934Z","shell.execute_reply.started":"2022-03-29T04:51:55.331232Z","shell.execute_reply":"2022-03-29T04:51:55.339140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_articles = articles[article_features]","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.341162Z","iopub.execute_input":"2022-03-29T04:51:55.341783Z","iopub.status.idle":"2022-03-29T04:51:55.356600Z","shell.execute_reply.started":"2022-03-29T04:51:55.341697Z","shell.execute_reply":"2022-03-29T04:51:55.355550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_cols = [col for col in train_articles.columns if train_articles[col].dtype == 'object']\nobject_cols","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.358140Z","iopub.execute_input":"2022-03-29T04:51:55.358445Z","iopub.status.idle":"2022-03-29T04:51:55.371735Z","shell.execute_reply.started":"2022-03-29T04:51:55.358411Z","shell.execute_reply":"2022-03-29T04:51:55.370730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_articles['index_code'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.373198Z","iopub.execute_input":"2022-03-29T04:51:55.373422Z","iopub.status.idle":"2022-03-29T04:51:55.398648Z","shell.execute_reply.started":"2022-03-29T04:51:55.373396Z","shell.execute_reply":"2022-03-29T04:51:55.397825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\nencoder = OrdinalEncoder()\ntrain_articles[['index_code']] = encoder.fit_transform(articles[['index_code']]).astype(int)\ntrain_articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.399617Z","iopub.execute_input":"2022-03-29T04:51:55.400250Z","iopub.status.idle":"2022-03-29T04:51:55.604452Z","shell.execute_reply.started":"2022-03-29T04:51:55.400215Z","shell.execute_reply":"2022-03-29T04:51:55.603592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_path = input_dir + 'customers.csv'\ncustomers = pd.read_csv(customers_path)\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:51:55.607130Z","iopub.execute_input":"2022-03-29T04:51:55.607368Z","iopub.status.idle":"2022-03-29T04:52:01.443567Z","shell.execute_reply.started":"2022-03-29T04:51:55.607340Z","shell.execute_reply":"2022-03-29T04:52:01.442346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:01.445302Z","iopub.execute_input":"2022-03-29T04:52:01.445594Z","iopub.status.idle":"2022-03-29T04:52:02.067407Z","shell.execute_reply.started":"2022-03-29T04:52:01.445551Z","shell.execute_reply":"2022-03-29T04:52:02.066440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:02.068700Z","iopub.execute_input":"2022-03-29T04:52:02.069005Z","iopub.status.idle":"2022-03-29T04:52:02.285302Z","shell.execute_reply.started":"2022-03-29T04:52:02.068976Z","shell.execute_reply":"2022-03-29T04:52:02.284470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['fashion_news_frequency'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:02.286819Z","iopub.execute_input":"2022-03-29T04:52:02.287537Z","iopub.status.idle":"2022-03-29T04:52:02.513194Z","shell.execute_reply.started":"2022-03-29T04:52:02.287490Z","shell.execute_reply":"2022-03-29T04:52:02.512269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['customer_id', 'club_member_status', 'fashion_news_frequency', 'age']","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:02.515021Z","iopub.execute_input":"2022-03-29T04:52:02.515971Z","iopub.status.idle":"2022-03-29T04:52:02.524417Z","shell.execute_reply.started":"2022-03-29T04:52:02.515921Z","shell.execute_reply":"2022-03-29T04:52:02.523675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\ntrain_customers = customers[features]\ntrain_customers['club_member_status'] = customers['club_member_status'].fillna('PRE-CREATE').map(\n    {'LEFT CLUB': 0, 'PRE-CREATE': 1, 'ACTIVE': 2}).astype(int)\ntrain_customers['fashion_news_frequency'] = customers['fashion_news_frequency'].copy().fillna('NONE').map(\n    {'NONE': 0, 'None': 0, 'Monthly': 1, 'Regularly': 2}).astype(int)\n\nimputer = SimpleImputer(strategy='mean').fit(customers[['age']])\ntrain_customers[['age']] = imputer.transform(train_customers[['age']])\ntrain_customers = train_customers.set_index('customer_id')\ntrain_customers.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:02.526153Z","iopub.execute_input":"2022-03-29T04:52:02.526692Z","iopub.status.idle":"2022-03-29T04:52:03.627008Z","shell.execute_reply.started":"2022-03-29T04:52:02.526632Z","shell.execute_reply":"2022-03-29T04:52:03.626125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_path = input_dir + 'sample_submission.csv'\nsamples = pd.read_csv(sample_path)\nsamples.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:03.628469Z","iopub.execute_input":"2022-03-29T04:52:03.628848Z","iopub.status.idle":"2022-03-29T04:52:08.509836Z","shell.execute_reply.started":"2022-03-29T04:52:03.628804Z","shell.execute_reply":"2022-03-29T04:52:08.509090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_path = input_dir + 'transactions_train.csv'\ntransactions = pd.read_csv(transactions_path, parse_dates=['t_dat'])\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:52:08.511001Z","iopub.execute_input":"2022-03-29T04:52:08.511254Z","iopub.status.idle":"2022-03-29T04:53:21.070679Z","shell.execute_reply.started":"2022-03-29T04:52:08.511227Z","shell.execute_reply":"2022-03-29T04:53:21.070066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:53:21.071997Z","iopub.execute_input":"2022-03-29T04:53:21.072776Z","iopub.status.idle":"2022-03-29T04:53:24.723165Z","shell.execute_reply.started":"2022-03-29T04:53:21.072685Z","shell.execute_reply":"2022-03-29T04:53:24.722179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_by_customers = transactions.groupby('customer_id').article_id.apply(set).to_dict()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:53:24.724311Z","iopub.execute_input":"2022-03-29T04:53:24.724953Z","iopub.status.idle":"2022-03-29T04:54:24.022633Z","shell.execute_reply.started":"2022-03-29T04:53:24.724908Z","shell.execute_reply":"2022-03-29T04:54:24.021932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_popularity = transactions.groupby('article_id').customer_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:15:31.729105Z","iopub.execute_input":"2022-03-29T05:15:31.729393Z","iopub.status.idle":"2022-03-29T05:15:56.292834Z","shell.execute_reply.started":"2022-03-29T05:15:31.729365Z","shell.execute_reply":"2022-03-29T05:15:56.291902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_popular = articles_popularity.sort_values(ascending=False).index","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:17:16.510016Z","iopub.execute_input":"2022-03-29T05:17:16.510307Z","iopub.status.idle":"2022-03-29T05:17:16.527229Z","shell.execute_reply.started":"2022-03-29T05:17:16.510274Z","shell.execute_reply":"2022-03-29T05:17:16.526179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_popular","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:17:18.469992Z","iopub.execute_input":"2022-03-29T05:17:18.470271Z","iopub.status.idle":"2022-03-29T05:17:18.476827Z","shell.execute_reply.started":"2022-03-29T05:17:18.470244Z","shell.execute_reply":"2022-03-29T05:17:18.475841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NR_PREDS = 12","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:24.023946Z","iopub.execute_input":"2022-03-29T04:54:24.024195Z","iopub.status.idle":"2022-03-29T04:54:24.028262Z","shell.execute_reply.started":"2022-03-29T04:54:24.024164Z","shell.execute_reply":"2022-03-29T04:54:24.027259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaled_customers = pd.DataFrame(StandardScaler().fit_transform(train_customers), index=train_customers.index, columns=train_customers.columns)\nscaled_customers.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:25.700927Z","iopub.execute_input":"2022-03-29T04:54:25.701480Z","iopub.status.idle":"2022-03-29T04:54:25.780563Z","shell.execute_reply.started":"2022-03-29T04:54:25.701450Z","shell.execute_reply":"2022-03-29T04:54:25.779932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_articles = pd.DataFrame(StandardScaler().fit_transform(train_articles), index=train_articles.index, columns=train_articles.columns)\nscaled_articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:25.781702Z","iopub.execute_input":"2022-03-29T04:54:25.782099Z","iopub.status.idle":"2022-03-29T04:54:25.895137Z","shell.execute_reply.started":"2022-03-29T04:54:25.782070Z","shell.execute_reply":"2022-03-29T04:54:25.894299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import NearestNeighbors\n\nmodel = NearestNeighbors(n_neighbors=NR_PREDS, n_jobs=-1).fit(scaled_articles)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:25.896712Z","iopub.execute_input":"2022-03-29T04:54:25.897269Z","iopub.status.idle":"2022-03-29T04:54:26.105053Z","shell.execute_reply.started":"2022-03-29T04:54:25.897226Z","shell.execute_reply":"2022-03-29T04:54:26.104028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.kneighbors(scaled_articles)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:26.106251Z","iopub.execute_input":"2022-03-29T04:54:26.106466Z","iopub.status.idle":"2022-03-29T04:54:38.355264Z","shell.execute_reply.started":"2022-03-29T04:54:26.106440Z","shell.execute_reply":"2022-03-29T04:54:38.354396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dists = pd.DataFrame(preds[0], index=articles.index)\nsimilar_articles = pd.DataFrame([[dists.index[x] for x in y] for y in preds[1]], index=articles.index)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:38.356459Z","iopub.execute_input":"2022-03-29T04:54:38.356707Z","iopub.status.idle":"2022-03-29T04:54:41.414582Z","shell.execute_reply.started":"2022-03-29T04:54:38.356678Z","shell.execute_reply":"2022-03-29T04:54:41.413666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dists.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:41.419660Z","iopub.execute_input":"2022-03-29T04:54:41.419923Z","iopub.status.idle":"2022-03-29T04:54:41.441993Z","shell.execute_reply.started":"2022-03-29T04:54:41.419895Z","shell.execute_reply":"2022-03-29T04:54:41.440729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"similar_articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:41.443727Z","iopub.execute_input":"2022-03-29T04:54:41.444086Z","iopub.status.idle":"2022-03-29T04:54:41.460798Z","shell.execute_reply.started":"2022-03-29T04:54:41.444042Z","shell.execute_reply":"2022-03-29T04:54:41.460131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#choices = {}\n#for customer in customers.customer_id:\n#    if customer not in articles_by_customers:\n#        choices[customer] = most_popular[:NR_PREDS]\n#    else:\n#        similar_to_bought = set()\n#        for article_id in articles_by_customers[customer]:\n#            similar_to_bought.update(list(zip(dists.loc[article_id].to_list(), similar_articles.loc[article_id].to_list())))\n#        similar_to_bought = sorted(list(similar_to_bought), key=lambda x: x[0])\n#        customer_choices = []\n#        for _, article in similar_to_bought:\n#            if article not in customer_choices and article not in articles_by_customers[customer]:\n#                customer_choices.append(article)\n#                if len(customer_choices) == NR_PREDS:\n#                    break\n#        if len(customer_choices) < NR_PREDS:\n#            for _, article in similar_to_bought:\n#                if article not in customer_choices:\n#                    customer_choices.append(article)\n#                    if len(customer_choices) == NR_PREDS:\n#                        break\n#        if len(customer_choices) < NR_PREDS:\n#            for article in most_popular:\n#                if article not in customer_choices:\n#                    customer_choices.append(article)\n#                    if len(customer_choices) == NR_PREDS:\n#                        break\n#        choices[customer] = customer_choices","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:41.462046Z","iopub.execute_input":"2022-03-29T04:54:41.462371Z","iopub.status.idle":"2022-03-29T04:54:41.467519Z","shell.execute_reply.started":"2022-03-29T04:54:41.462344Z","shell.execute_reply":"2022-03-29T04:54:41.466717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.DataFrame({'customer_id': choices.keys(), 'prediction': list(' '.join(map(str, x)) for x in choices.values())}).to_csv('submission_art.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T04:54:41.468918Z","iopub.execute_input":"2022-03-29T04:54:41.469343Z","iopub.status.idle":"2022-03-29T04:54:41.486131Z","shell.execute_reply.started":"2022-03-29T04:54:41.469312Z","shell.execute_reply":"2022-03-29T04:54:41.485197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import NearestNeighbors\n\nmodel = NearestNeighbors(n_neighbors=NR_PREDS, n_jobs=-1).fit(scaled_customers)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:17:35.918620Z","iopub.execute_input":"2022-03-29T05:17:35.918924Z","iopub.status.idle":"2022-03-29T05:17:37.723257Z","shell.execute_reply.started":"2022-03-29T05:17:35.918895Z","shell.execute_reply":"2022-03-29T05:17:37.722255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.kneighbors(scaled_customers)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:17:41.284568Z","iopub.execute_input":"2022-03-29T05:17:41.284926Z","iopub.status.idle":"2022-03-29T05:30:51.653558Z","shell.execute_reply.started":"2022-03-29T05:17:41.284881Z","shell.execute_reply":"2022-03-29T05:30:51.652597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dists = pd.DataFrame(preds[0], index=train_customers.index)\nsimilar_users = pd.DataFrame([[dists.index[x] for x in y] for y in preds[1]], index=train_customers.index)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:34:33.478451Z","iopub.execute_input":"2022-03-29T05:34:33.478752Z","iopub.status.idle":"2022-03-29T05:35:08.828772Z","shell.execute_reply.started":"2022-03-29T05:34:33.478723Z","shell.execute_reply":"2022-03-29T05:35:08.827828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"choices = {}\nfor customer in customers.customer_id:\n    customer_choices = []\n    for sim_user in similar_users.loc[customer]:\n        if sim_user in articles_by_customers:\n            for article in articles_by_customers[sim_user]:\n                if article not in customer_choices and article not in articles_by_customers.get(customer, set()):\n                    customer_choices.append(article)\n                    if len(customer_choices) == NR_PREDS:\n                        break\n            if len(customer_choices) == NR_PREDS:\n                break\n    if len(customer_choices) < NR_PREDS:\n        for article in most_popular:\n            if article not in customer_choices:\n                customer_choices.append(article)\n                if len(customer_choices) == NR_PREDS:\n                    break\n    choices[customer] = customer_choices","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:35:12.269903Z","iopub.execute_input":"2022-03-29T05:35:12.270186Z","iopub.status.idle":"2022-03-29T05:35:41.470790Z","shell.execute_reply.started":"2022-03-29T05:35:12.270158Z","shell.execute_reply":"2022-03-29T05:35:41.469425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'customer_id': choices.keys(), 'prediction': list(' '.join(map(str, x)) for x in choices.values())}).to_csv('submission_cust.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-29T05:03:27.100014Z","iopub.status.idle":"2022-03-29T05:03:27.100561Z","shell.execute_reply.started":"2022-03-29T05:03:27.100342Z","shell.execute_reply":"2022-03-29T05:03:27.100366Z"},"trusted":true},"execution_count":null,"outputs":[]}]}