{"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":"This is content-based filtering by Group 8, Krit and Oranich","metadata":{}},{"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)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-30T02:27:04.781594Z","iopub.execute_input":"2022-03-30T02:27:04.781950Z","iopub.status.idle":"2022-03-30T02:27:04.809212Z","shell.execute_reply.started":"2022-03-30T02:27:04.781863Z","shell.execute_reply":"2022-03-30T02:27:04.808571Z"},"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":"2022-03-30T02:27:04.810460Z","iopub.execute_input":"2022-03-30T02:27:04.810795Z","iopub.status.idle":"2022-03-30T02:27:05.489671Z","shell.execute_reply.started":"2022-03-30T02:27:04.810762Z","shell.execute_reply":"2022-03-30T02:27:05.488845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv ('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\ntransactions = pd.read_csv ('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ncustomers = pd.read_csv ('../input/h-and-m-personalized-fashion-recommendations/customers.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:27:05.490739Z","iopub.execute_input":"2022-03-30T02:27:05.490952Z","iopub.status.idle":"2022-03-30T02:28:15.940690Z","shell.execute_reply.started":"2022-03-30T02:27:05.490925Z","shell.execute_reply":"2022-03-30T02:28:15.939617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('min date: {}, max date: {}'.format(transactions.t_dat.min(), transactions.t_dat.max()))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:15.942449Z","iopub.execute_input":"2022-03-30T02:28:15.942755Z","iopub.status.idle":"2022-03-30T02:28:19.855106Z","shell.execute_reply.started":"2022-03-30T02:28:15.942717Z","shell.execute_reply":"2022-03-30T02:28:19.854236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#select one day \nts_1d = transactions.loc[(transactions['t_dat']=='2020-09-21')]\nts_1d","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:19.858045Z","iopub.execute_input":"2022-03-30T02:28:19.858657Z","iopub.status.idle":"2022-03-30T02:28:21.945200Z","shell.execute_reply.started":"2022-03-30T02:28:19.858608Z","shell.execute_reply":"2022-03-30T02:28:21.944352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Merge articles with transaction data\ndf = ts_1d.merge(articles, on='article_id')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:21.946696Z","iopub.execute_input":"2022-03-30T02:28:21.947073Z","iopub.status.idle":"2022-03-30T02:28:22.200715Z","shell.execute_reply.started":"2022-03-30T02:28:21.947026Z","shell.execute_reply":"2022-03-30T02:28:22.199891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = 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":"2022-03-30T02:28:22.202245Z","iopub.execute_input":"2022-03-30T02:28:22.202615Z","iopub.status.idle":"2022-03-30T02:28:22.223497Z","shell.execute_reply.started":"2022-03-30T02:28:22.202571Z","shell.execute_reply":"2022-03-30T02:28:22.222782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:22.224517Z","iopub.execute_input":"2022-03-30T02:28:22.224884Z","iopub.status.idle":"2022-03-30T02:28:22.229286Z","shell.execute_reply.started":"2022-03-30T02:28:22.224856Z","shell.execute_reply":"2022-03-30T02:28:22.228553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:22.230045Z","iopub.execute_input":"2022-03-30T02:28:22.230255Z","iopub.status.idle":"2022-03-30T02:28:22.254916Z","shell.execute_reply.started":"2022-03-30T02:28:22.230228Z","shell.execute_reply":"2022-03-30T02:28:22.254135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Choose features to build feature space\nfeatures = feature_subset\ndf1 = df[['customer_id', 'article_id'] + features]\ndummies_df = pd.get_dummies(df1, columns=features)\ndummies_df","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:22.256460Z","iopub.execute_input":"2022-03-30T02:28:22.256748Z","iopub.status.idle":"2022-03-30T02:28:22.402733Z","shell.execute_reply.started":"2022-03-30T02:28:22.256710Z","shell.execute_reply":"2022-03-30T02:28:22.401885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummies_df.groupby('customer_id').count()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:22.404122Z","iopub.execute_input":"2022-03-30T02:28:22.404817Z","iopub.status.idle":"2022-03-30T02:28:22.539117Z","shell.execute_reply.started":"2022-03-30T02:28:22.404767Z","shell.execute_reply":"2022-03-30T02:28:22.538405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"minimum_items = 1\ngroupby_customer = dummies_df.groupby('customer_id')\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_items:\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":"2022-03-30T02:28:22.540221Z","iopub.execute_input":"2022-03-30T02:28:22.540424Z","iopub.status.idle":"2022-03-30T02:28:44.375734Z","shell.execute_reply.started":"2022-03-30T02:28:22.540401Z","shell.execute_reply":"2022-03-30T02:28:44.374744Z"},"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":"2022-03-30T02:28:44.378289Z","iopub.execute_input":"2022-03-30T02:28:44.378671Z","iopub.status.idle":"2022-03-30T02:28:47.162576Z","shell.execute_reply.started":"2022-03-30T02:28:44.378638Z","shell.execute_reply":"2022-03-30T02:28:47.161766Z"},"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":"2022-03-30T02:28:47.165009Z","iopub.execute_input":"2022-03-30T02:28:47.165237Z","iopub.status.idle":"2022-03-30T02:28:47.252781Z","shell.execute_reply.started":"2022-03-30T02:28:47.165211Z","shell.execute_reply":"2022-03-30T02:28:47.251902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = normalized_user_feature.dot(item_feature.T)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:47.254070Z","iopub.execute_input":"2022-03-30T02:28:47.254445Z","iopub.status.idle":"2022-03-30T02:28:48.633229Z","shell.execute_reply.started":"2022-03-30T02:28:47.254411Z","shell.execute_reply":"2022-03-30T02:28:48.632309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rcmnd(customer_id, scores):\n    cutomer_scores = scores.loc[customer_id]\n    customer_prev_items = groupby_customer.get_group(customer_id)['article_id']\n    prev_dropped = cutomer_scores.drop(customer_prev_items.values)\n    ordered = prev_dropped.sort_values(ascending=False)   \n    return ordered, customer_prev_items\n\ndef plot_prev(prev_items):\n    fig = plt.figure(figsize=(20, 10))\n    for item, i in zip(prev_items, range(1, len(prev_items)+1)):\n        item = '0' + str(item)\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(2, 6, i)\n        plt.imshow(image)\n        \ndef plot_rcmnd(rcmnds):\n    fig = plt.figure(figsize=(20, 10))\n    for item, i in zip(rcmnds, range(1, k+1)):\n        item = '0' + str(item)\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(2, 6, i)\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:48.634973Z","iopub.execute_input":"2022-03-30T02:28:48.635542Z","iopub.status.idle":"2022-03-30T02:28:48.649615Z","shell.execute_reply.started":"2022-03-30T02:28:48.635493Z","shell.execute_reply":"2022-03-30T02:28:48.648646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\npca = PCA(n_components=100)\npca.fit(normalized_user_feature)\npca.explained_variance_ratio_.sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:48.651267Z","iopub.execute_input":"2022-03-30T02:28:48.652273Z","iopub.status.idle":"2022-03-30T02:28:50.491177Z","shell.execute_reply.started":"2022-03-30T02:28:48.652222Z","shell.execute_reply":"2022-03-30T02:28:50.490072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_feature_pca = pd.DataFrame(pca.transform(normalized_user_feature), columns=['component_{}'.format(i) for i in range(1, 101)]).set_index(normalized_user_feature.index)\nitem_feature_pca = pd.DataFrame(pca.transform(item_feature), columns=['component_{}'.format(i) for i in range(1, 101)]).set_index(item_feature.index)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:50.492701Z","iopub.execute_input":"2022-03-30T02:28:50.493037Z","iopub.status.idle":"2022-03-30T02:28:50.597329Z","shell.execute_reply.started":"2022-03-30T02:28:50.492994Z","shell.execute_reply":"2022-03-30T02:28:50.596369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores_pca = user_feature_pca.dot(item_feature_pca.T)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:50.598936Z","iopub.execute_input":"2022-03-30T02:28:50.599521Z","iopub.status.idle":"2022-03-30T02:28:51.109751Z","shell.execute_reply.started":"2022-03-30T02:28:50.599469Z","shell.execute_reply":"2022-03-30T02:28:51.108758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k = 12\ncustomer_id = scores.index[0]\nprint(customer_id)\nrcmnds, prev_items = get_rcmnd(customer_id, scores)\nrcmnds_pca, prev_items = get_rcmnd(customer_id, scores_pca)\nrcmnds = rcmnds.index.values[:k]\nrcmnds_pca = rcmnds_pca.index.values[:k]\npath = \"../input/h-and-m-personalized-fashion-recommendations/images\"","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:51.111645Z","iopub.execute_input":"2022-03-30T02:28:51.112360Z","iopub.status.idle":"2022-03-30T02:28:51.135868Z","shell.execute_reply.started":"2022-03-30T02:28:51.112312Z","shell.execute_reply":"2022-03-30T02:28:51.134866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_prev(prev_items)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:51.137823Z","iopub.execute_input":"2022-03-30T02:28:51.138547Z","iopub.status.idle":"2022-03-30T02:28:52.872740Z","shell.execute_reply.started":"2022-03-30T02:28:51.138494Z","shell.execute_reply":"2022-03-30T02:28:52.872142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_rcmnd(rcmnds)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T02:28:52.873740Z","iopub.execute_input":"2022-03-30T02:28:52.874029Z","iopub.status.idle":"2022-03-30T02:28:58.532711Z","shell.execute_reply.started":"2022-03-30T02:28:52.874003Z","shell.execute_reply":"2022-03-30T02:28:58.529186Z"},"trusted":true},"execution_count":null,"outputs":[]}]}