{"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":"markdown","source":"# Reading subset of data and restrict only customer who have bought at least three transactions","metadata":{}},{"cell_type":"code","source":"# import numpy as np \n# import pandas as pd\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-03T03:27:38.073369Z","iopub.execute_input":"2023-05-03T03:27:38.073721Z","iopub.status.idle":"2023-05-03T03:27:38.079031Z","shell.execute_reply.started":"2023-05-03T03:27:38.073682Z","shell.execute_reply":"2023-05-03T03:27:38.077869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport plotly.graph_objects as go\nfrom skimage import io","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:27:38.081464Z","iopub.execute_input":"2023-05-03T03:27:38.081734Z","iopub.status.idle":"2023-05-03T03:27:38.892591Z","shell.execute_reply.started":"2023-05-03T03:27:38.081699Z","shell.execute_reply":"2023-05-03T03:27:38.891747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\narticles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\n# Randomely sample 1 Lakh records\nusers = df.sample(n=100000)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:27:38.893985Z","iopub.execute_input":"2023-05-03T03:27:38.894305Z","iopub.status.idle":"2023-05-03T03:29:07.827671Z","shell.execute_reply.started":"2023-05-03T03:27:38.894264Z","shell.execute_reply":"2023-05-03T03:29:07.826570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Join the user data with article id\ndf = users.merge(articles, on='article_id')\ndf = df[['t_dat', 'customer_id', 'article_id', 'prod_name', 'product_type_name',\n       'product_group_name', \n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name', 'detail_desc']]\n\nfeature_subset = ['product_group_name', \n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name']","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:07.829384Z","iopub.execute_input":"2023-05-03T03:29:07.829749Z","iopub.status.idle":"2023-05-03T03:29:09.370588Z","shell.execute_reply.started":"2023-05-03T03:29:07.829694Z","shell.execute_reply":"2023-05-03T03:29:09.369552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# We will only subset features ignoring transaction date","metadata":{}},{"cell_type":"code","source":"Only_features = df[['customer_id', 'article_id'] + feature_subset]\ndummies_df = pd.get_dummies(Only_features, columns=feature_subset)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:09.373617Z","iopub.execute_input":"2023-05-03T03:29:09.374574Z","iopub.status.idle":"2023-05-03T03:29:09.851631Z","shell.execute_reply.started":"2023-05-03T03:29:09.374517Z","shell.execute_reply":"2023-05-03T03:29:09.850534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummies_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:09.853118Z","iopub.execute_input":"2023-05-03T03:29:09.853454Z","iopub.status.idle":"2023-05-03T03:29:09.888559Z","shell.execute_reply.started":"2023-05-03T03:29:09.853410Z","shell.execute_reply":"2023-05-03T03:29:09.887380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Minimum we will choose minimum a customer has to be doing three transactions\nminimum_transaction = 3\ngroupby_customer = dummies_df.groupby('customer_id')\n\n\nl = []\ncutomer_ids = []\narticle_ids = []\nfor key in groupby_customer.groups.keys():\n    temp = groupby_customer.get_group(key)\n    if temp.article_id.nunique() >= minimum_transaction:\n        l.append(temp.drop('article_id', axis=1).sum(numeric_only=True).values)\n        cutomer_ids.append(key)\n        article_ids.extend(temp.article_id.values.tolist())","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:09.890191Z","iopub.execute_input":"2023-05-03T03:29:09.890522Z","iopub.status.idle":"2023-05-03T03:29:48.121410Z","shell.execute_reply.started":"2023-05-03T03:29:09.890468Z","shell.execute_reply":"2023-05-03T03:29:48.120205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_feature = pd.DataFrame(l, columns = dummies_df.columns[2:])\nnormalized_user_feature = user_feature.div(user_feature.sum(axis=1), axis=0)\nnormalized_user_feature.insert(0, 'customer_id', cutomer_ids)\nnormalized_user_feature = normalized_user_feature.set_index('customer_id')\nnormalized_user_feature","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:48.123098Z","iopub.execute_input":"2023-05-03T03:29:48.123377Z","iopub.status.idle":"2023-05-03T03:29:48.483356Z","shell.execute_reply.started":"2023-05-03T03:29:48.123328Z","shell.execute_reply":"2023-05-03T03:29:48.482243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_feature = dummies_df.drop_duplicates(subset='article_id')\nitem_feature = item_feature[item_feature.article_id.isin(article_ids)].drop('customer_id', axis=1)\nitem_feature = item_feature.set_index('article_id')\nitem_feature","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:48.484750Z","iopub.execute_input":"2023-05-03T03:29:48.485052Z","iopub.status.idle":"2023-05-03T03:29:48.681999Z","shell.execute_reply.started":"2023-05-03T03:29:48.485017Z","shell.execute_reply":"2023-05-03T03:29:48.681089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = normalized_user_feature.dot(item_feature.T)\nscores","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:48.683382Z","iopub.execute_input":"2023-05-03T03:29:48.684228Z","iopub.status.idle":"2023-05-03T03:29:48.878948Z","shell.execute_reply.started":"2023-05-03T03:29:48.684179Z","shell.execute_reply":"2023-05-03T03:29:48.877549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# We will performing matrix decomposition ","metadata":{}},{"cell_type":"code","source":"\nfrom numpy.linalg import svd\nmatrix = scores.values\n","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:48.880887Z","iopub.execute_input":"2023-05-03T03:29:48.891131Z","iopub.status.idle":"2023-05-03T03:29:48.898695Z","shell.execute_reply.started":"2023-05-03T03:29:48.891018Z","shell.execute_reply":"2023-05-03T03:29:48.896863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"u, s, vh = svd(matrix, full_matrices=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:48.901156Z","iopub.execute_input":"2023-05-03T03:29:48.902460Z","iopub.status.idle":"2023-05-03T03:29:53.600018Z","shell.execute_reply.started":"2023-05-03T03:29:48.902388Z","shell.execute_reply":"2023-05-03T03:29:53.598868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(u.shape)\nprint(s.shape)\nprint(vh.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:53.601220Z","iopub.execute_input":"2023-05-03T03:29:53.601455Z","iopub.status.idle":"2023-05-03T03:29:53.607036Z","shell.execute_reply.started":"2023-05-03T03:29:53.601425Z","shell.execute_reply":"2023-05-03T03:29:53.606132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"{u} \\n\\n\\n\\n {s} \\n\\n\\n\\n {vh}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:53.610438Z","iopub.execute_input":"2023-05-03T03:29:53.610881Z","iopub.status.idle":"2023-05-03T03:29:53.622089Z","shell.execute_reply.started":"2023-05-03T03:29:53.610834Z","shell.execute_reply":"2023-05-03T03:29:53.621045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reconstructed_vectors = u @ np.diag(s) @ vh\nnp.allclose(reconstructed_vectors,matrix)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T03:29:53.623719Z","iopub.execute_input":"2023-05-03T03:29:53.624047Z","iopub.status.idle":"2023-05-03T03:29:54.162815Z","shell.execute_reply.started":"2023-05-03T03:29:53.624003Z","shell.execute_reply":"2023-05-03T03:29:54.161876Z"},"trusted":true},"execution_count":null,"outputs":[]}]}