{"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)\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\nfor 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-02-10T19:10:09.489411Z","iopub.execute_input":"2022-02-10T19:10:09.490142Z","iopub.status.idle":"2022-02-10T19:10:46.372031Z","shell.execute_reply.started":"2022-02-10T19:10:09.490105Z","shell.execute_reply":"2022-02-10T19:10:46.369384Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np # linear algebra\nimport os\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nfrom os import listdir\nfrom os.path import isfile, join\n\nfrom termcolor import colored\nfrom IPython.display import HTML\n\nimport warnings\npd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\npd.set_option('float_format', '{:f}'.format)\nwarnings.filterwarnings('ignore')\n# Standard plotly imports\nimport plotly as py\nimport plotly.graph_objs as go\nfrom plotly.offline import iplot, init_notebook_mode\n# Using plotly + cufflinks in offline mode\nimport cufflinks\ncufflinks.go_offline(connected=True)\ninit_notebook_mode(connected=True)\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:49:27.583154Z","iopub.execute_input":"2022-02-10T19:49:27.583850Z","iopub.status.idle":"2022-02-10T19:49:31.341928Z","shell.execute_reply.started":"2022-02-10T19:49:27.583809Z","shell.execute_reply":"2022-02-10T19:49:31.341118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles  = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ncustomers = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ntransactions = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:49:31.343441Z","iopub.execute_input":"2022-02-10T19:49:31.343777Z","iopub.status.idle":"2022-02-10T19:50:46.080701Z","shell.execute_reply.started":"2022-02-10T19:49:31.343748Z","shell.execute_reply":"2022-02-10T19:50:46.079702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:51:54.573003Z","iopub.execute_input":"2022-02-10T19:51:54.573305Z","iopub.status.idle":"2022-02-10T19:51:54.595826Z","shell.execute_reply.started":"2022-02-10T19:51:54.573267Z","shell.execute_reply":"2022-02-10T19:51:54.594904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:51:30.219102Z","iopub.execute_input":"2022-02-10T19:51:30.219443Z","iopub.status.idle":"2022-02-10T19:51:30.236846Z","shell.execute_reply.started":"2022-02-10T19:51:30.219410Z","shell.execute_reply":"2022-02-10T19:51:30.236341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['perceived_colour_value_name'].iplot(kind='hist',xTitle='perceived_colour_value_name', yTitle='count', title='CountProduct')\narticles['perceived_colour_value_id'].iplot(kind='hist',xTitle='perceived_colour_value_id', yTitle='count', title='CountProduct')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:52:47.210867Z","iopub.execute_input":"2022-02-10T18:52:47.211531Z","iopub.status.idle":"2022-02-10T18:52:50.312785Z","shell.execute_reply.started":"2022-02-10T18:52:47.211494Z","shell.execute_reply":"2022-02-10T18:52:50.311758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['product_type_name'].iplot(kind='hist',xTitle='product_type_name', yTitle='count', title='CountProduct')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:53:08.666019Z","iopub.execute_input":"2022-02-10T18:53:08.666288Z","iopub.status.idle":"2022-02-10T18:53:10.24881Z","shell.execute_reply.started":"2022-02-10T18:53:08.666259Z","shell.execute_reply":"2022-02-10T18:53:10.248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values_count = articles.isnull().sum()\nmissing_values_count","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:53:13.264794Z","iopub.execute_input":"2022-02-10T18:53:13.26509Z","iopub.status.idle":"2022-02-10T18:53:13.426086Z","shell.execute_reply.started":"2022-02-10T18:53:13.265058Z","shell.execute_reply":"2022-02-10T18:53:13.425334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique = articles['detail_desc'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:53:22.210313Z","iopub.execute_input":"2022-02-10T18:53:22.210621Z","iopub.status.idle":"2022-02-10T18:53:22.240874Z","shell.execute_reply.started":"2022-02-10T18:53:22.210592Z","shell.execute_reply":"2022-02-10T18:53:22.240337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles  = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:53:23.909123Z","iopub.execute_input":"2022-02-10T18:53:23.909557Z","iopub.status.idle":"2022-02-10T18:53:24.474889Z","shell.execute_reply.started":"2022-02-10T18:53:23.909519Z","shell.execute_reply":"2022-02-10T18:53:24.474371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = articles.loc[:, [\"product_type_no\", \"perceived_colour_value_id\"]]\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:53:27.225886Z","iopub.execute_input":"2022-02-10T18:53:27.226475Z","iopub.status.idle":"2022-02-10T18:53:27.236311Z","shell.execute_reply.started":"2022-02-10T18:53:27.226437Z","shell.execute_reply":"2022-02-10T18:53:27.23575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = KMeans(n_clusters=6)\nX[\"Cluster\"] = kmeans.fit_predict(X)\nX[\"Cluster\"] = X[\"Cluster\"].astype(\"category\")\n\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:53:32.23753Z","iopub.execute_input":"2022-02-10T18:53:32.238003Z","iopub.status.idle":"2022-02-10T18:53:33.844056Z","shell.execute_reply.started":"2022-02-10T18:53:32.237953Z","shell.execute_reply":"2022-02-10T18:53:33.843428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.relplot(\n    x=\"product_type_no\", y=\"perceived_colour_value_id\", hue=\"Cluster\", data=X, height=6,\n);","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:55:20.764569Z","iopub.execute_input":"2022-02-10T18:55:20.764945Z","iopub.status.idle":"2022-02-10T18:55:28.394492Z","shell.execute_reply.started":"2022-02-10T18:55:20.764896Z","shell.execute_reply":"2022-02-10T18:55:28.393685Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getImagePaths(path):\n    \"\"\"\n    Function to Combine Directory Path with individual Image Paths\n    \n    parameters: path(string) - Path of directory\n    returns: image_names(string) - Full Image Path\n    \"\"\"\n    image_names = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in filenames:\n            fullpath = os.path.join(dirname, filename)\n            image_names.append(fullpath)\n    return image_names\n\ndef display_multiple_img(images_paths, rows, cols):\n    \"\"\"\n    Function to Display Images from Dataset.\n    \n    parameters: images_path(string) - Paths of Images to be displayed\n                rows(int) - No. of Rows in Output\n                cols(int) - No. of Columns in Output\n    \"\"\"\n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8) )\n    for ind,image_path in enumerate(images_paths):\n        image=cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()\n\ndef plot_distribution(x, data, title):\n        fig = px.histogram(\n        data, \n        x = x,\n        width = 800,\n        height = 500,\n        title = title\n        )\n\n        fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:07:28.034655Z","iopub.execute_input":"2022-02-10T19:07:28.035741Z","iopub.status.idle":"2022-02-10T19:07:28.047509Z","shell.execute_reply.started":"2022-02-10T19:07:28.035681Z","shell.execute_reply":"2022-02-10T19:07:28.046855Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def disply_multiple_img_ids(idx, rows, cols):\n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8))\n    for ind, im_id in enumerate(idx):\n\n        image_path = f'{images_dir}/0{str(im_id)[:2]}/0{im_id}.jpg'\n        \n        image=cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:07:45.131368Z","iopub.execute_input":"2022-02-10T19:07:45.131952Z","iopub.status.idle":"2022-02-10T19:07:45.138526Z","shell.execute_reply.started":"2022-02-10T19:07:45.131918Z","shell.execute_reply":"2022-02-10T19:07:45.137583Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = '../input/h-and-m-personalized-fashion-recommendations/images'\ncat_images = [f for f in listdir(images_dir)]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:11:54.754951Z","iopub.execute_input":"2022-02-10T19:11:54.755284Z","iopub.status.idle":"2022-02-10T19:11:54.762751Z","shell.execute_reply.started":"2022-02-10T19:11:54.755249Z","shell.execute_reply":"2022-02-10T19:11:54.761916Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This code was borrowed from https://www.kaggle.com/ishandutta/v7-shopee-indepth-eda-one-stop-for-all-your-needs\ndef getImagePaths(path):\n    \"\"\"\n    Function to Combine Directory Path with individual Image Paths\n    \n    parameters: path(string) - Path of directory\n    returns: image_names(string) - Full Image Path\n    \"\"\"\n    image_names = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in filenames:\n            fullpath = os.path.join(dirname, filename)\n            image_names.append(fullpath)\n    return image_names\n\ndef display_multiple_img(images_paths, rows, cols):\n    \"\"\"\n    Function to Display Images from Dataset.\n    \n    parameters: images_path(string) - Paths of Images to be displayed\n                rows(int) - No. of Rows in Output\n                cols(int) - No. of Columns in Output\n    \"\"\"\n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8) )\n    for ind,image_path in enumerate(images_paths):\n        image=cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()\n\ndef plot_distribution(x, data, title):\n        fig = px.histogram(\n        data, \n        x = x,\n        width = 800,\n        height = 500,\n        title = title\n        )\n\n        fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:12:20.570766Z","iopub.execute_input":"2022-02-10T19:12:20.571422Z","iopub.status.idle":"2022-02-10T19:12:20.581481Z","shell.execute_reply.started":"2022-02-10T19:12:20.571375Z","shell.execute_reply":"2022-02-10T19:12:20.580827Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def disply_multiple_img_ids(idx, rows, cols):\n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8))\n    for ind, im_id in enumerate(idx):\n\n        image_path = f'{images_dir}/0{str(im_id)[:2]}/0{im_id}.jpg'\n        \n        image=cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:12:39.793431Z","iopub.execute_input":"2022-02-10T19:12:39.794058Z","iopub.status.idle":"2022-02-10T19:12:39.801178Z","shell.execute_reply.started":"2022-02-10T19:12:39.794008Z","shell.execute_reply":"2022-02-10T19:12:39.800153Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = getImagePaths(images_dir)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:12:50.882276Z","iopub.execute_input":"2022-02-10T19:12:50.882547Z","iopub.status.idle":"2022-02-10T19:13:12.850711Z","shell.execute_reply.started":"2022-02-10T19:12:50.882522Z","shell.execute_reply":"2022-02-10T19:13:12.850024Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disply_multiple_img_ids(articles.query(\"product_type_name == 'Trousers'\").article_id[:5], 1, 5)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:17:46.456938Z","iopub.execute_input":"2022-02-10T19:17:46.457746Z","iopub.status.idle":"2022-02-10T19:17:47.65394Z","shell.execute_reply.started":"2022-02-10T19:17:46.457696Z","shell.execute_reply":"2022-02-10T19:17:47.653097Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disply_multiple_img_ids(articles.query(\"product_type_name == 'Dress'\").article_id[:2], 1, 2)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:22:23.077519Z","iopub.execute_input":"2022-02-10T19:22:23.078001Z","iopub.status.idle":"2022-02-10T19:22:23.700178Z","shell.execute_reply.started":"2022-02-10T19:22:23.077971Z","shell.execute_reply":"2022-02-10T19:22:23.69938Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disply_multiple_img_ids(articles.query(\"product_type_name == 'T-shirt'\").article_id[:11], 1, 11)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:34:57.347026Z","iopub.execute_input":"2022-02-10T19:34:57.34733Z","iopub.status.idle":"2022-02-10T19:35:00.27898Z","shell.execute_reply.started":"2022-02-10T19:34:57.347285Z","shell.execute_reply":"2022-02-10T19:35:00.278064Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disply_multiple_img_ids(articles.query(\"product_type_name == 'Sweater'\").article_id[:10], 1, 10)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:20:41.811151Z","iopub.execute_input":"2022-02-10T19:20:41.811953Z","iopub.status.idle":"2022-02-10T19:20:44.697346Z","shell.execute_reply.started":"2022-02-10T19:20:41.811914Z","shell.execute_reply":"2022-02-10T19:20:44.696571Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(articles)\ndf[['product_type_name', 'perceived_colour_value_name']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:31:47.198769Z","iopub.execute_input":"2022-02-10T19:31:47.199582Z","iopub.status.idle":"2022-02-10T19:31:47.211263Z","shell.execute_reply.started":"2022-02-10T19:31:47.199548Z","shell.execute_reply":"2022-02-10T19:31:47.210703Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['graphical_appearance_name'].iplot(kind='hist',xTitle='graphical_appearance_name', yTitle='count', title='CountProduct')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:52:48.434654Z","iopub.execute_input":"2022-02-10T19:52:48.434973Z","iopub.status.idle":"2022-02-10T19:52:50.679823Z","shell.execute_reply.started":"2022-02-10T19:52:48.434944Z","shell.execute_reply":"2022-02-10T19:52:50.679170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = articles.graphical_appearance_name.value_counts()\ns_len = s / len(articles.index) * 100\nres = pd.concat([s, s_len], axis=1)\\\n        .set_axis(['TOP 10 - Graphical Appearance Name', '%'], axis=1, inplace=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:56:18.073605Z","iopub.execute_input":"2022-02-10T19:56:18.074064Z","iopub.status.idle":"2022-02-10T19:56:18.096557Z","shell.execute_reply.started":"2022-02-10T19:56:18.074034Z","shell.execute_reply":"2022-02-10T19:56:18.095702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disply_multiple_img_ids(articles.query(\"graphical_appearance_name == 'Solid'\").article_id[:5], 1, 5)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T19:56:20.532499Z","iopub.execute_input":"2022-02-10T19:56:20.532941Z","iopub.status.idle":"2022-02-10T19:56:20.555099Z","shell.execute_reply.started":"2022-02-10T19:56:20.532909Z","shell.execute_reply":"2022-02-10T19:56:20.554248Z"},"trusted":true},"execution_count":null,"outputs":[]}]}