{"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":"import pickle\nimport pandas as pd\nfrom sklearn.neighbors import KNeighborsClassifier as KNN\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport sklearn","metadata":{"execution":{"iopub.status.busy":"2022-05-14T06:44:19.424942Z","iopub.execute_input":"2022-05-14T06:44:19.42535Z","iopub.status.idle":"2022-05-14T06:44:19.430357Z","shell.execute_reply.started":"2022-05-14T06:44:19.425301Z","shell.execute_reply":"2022-05-14T06:44:19.429268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('../input/product-similarity-with-image-data/embeds.pickle', 'rb')\npaths = open('../input/product-similarity-with-image-data/paths.pickle', 'rb')\npaths = pickle.load(paths)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-14T06:00:23.063212Z","iopub.execute_input":"2022-05-14T06:00:23.06352Z","iopub.status.idle":"2022-05-14T06:00:23.102988Z","shell.execute_reply.started":"2022-05-14T06:00:23.06348Z","shell.execute_reply":"2022-05-14T06:00:23.101908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = np.array([path[-14:-4] for path in paths])","metadata":{"execution":{"iopub.status.busy":"2022-05-14T06:33:47.283829Z","iopub.execute_input":"2022-05-14T06:33:47.284774Z","iopub.status.idle":"2022-05-14T06:33:47.340801Z","shell.execute_reply.started":"2022-05-14T06:33:47.284732Z","shell.execute_reply":"2022-05-14T06:33:47.339668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeds = np.array([pickle.load(f)[0] for i, path in zip(range(105100), paths)])","metadata":{"execution":{"iopub.status.busy":"2022-05-14T06:03:12.133579Z","iopub.execute_input":"2022-05-14T06:03:12.133873Z","iopub.status.idle":"2022-05-14T06:03:18.060127Z","shell.execute_reply.started":"2022-05-14T06:03:12.133834Z","shell.execute_reply":"2022-05-14T06:03:18.059143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(embeds)\ndf['article_id'] = ids","metadata":{"execution":{"iopub.status.busy":"2022-05-14T06:33:54.905959Z","iopub.execute_input":"2022-05-14T06:33:54.907182Z","iopub.status.idle":"2022-05-14T06:33:54.926482Z","shell.execute_reply.started":"2022-05-14T06:33:54.907119Z","shell.execute_reply":"2022-05-14T06:33:54.925533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df.drop('article_id', axis=1)\ny = df.article_id","metadata":{"execution":{"iopub.status.busy":"2022-05-14T06:33:57.256616Z","iopub.execute_input":"2022-05-14T06:33:57.256891Z","iopub.status.idle":"2022-05-14T06:33:57.559184Z","shell.execute_reply.started":"2022-05-14T06:33:57.256862Z","shell.execute_reply":"2022-05-14T06:33:57.5584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNN(20, metric='cosine')","metadata":{"execution":{"iopub.status.busy":"2022-05-14T07:03:02.667602Z","iopub.execute_input":"2022-05-14T07:03:02.668346Z","iopub.status.idle":"2022-05-14T07:03:02.67411Z","shell.execute_reply.started":"2022-05-14T07:03:02.668289Z","shell.execute_reply":"2022-05-14T07:03:02.672832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T07:03:03.707171Z","iopub.execute_input":"2022-05-14T07:03:03.70748Z","iopub.status.idle":"2022-05-14T07:03:04.171426Z","shell.execute_reply.started":"2022-05-14T07:03:03.707446Z","shell.execute_reply":"2022-05-14T07:03:04.170519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = knn.kneighbors()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"similar = open('top_20.pickle', 'wb')\npickle.dump(scores, similar)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_items(items):\n    path = \"../input/h-and-m-personalized-fashion-recommendations/images\"\n\n    k = len(items)\n    fig = plt.figure(figsize=(15, 10))\n    for item, i in zip(items, range(1, k+1)):\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(1, k, i)\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T06:53:47.873412Z","iopub.execute_input":"2022-05-14T06:53:47.873754Z","iopub.status.idle":"2022-05-14T06:53:47.881013Z","shell.execute_reply.started":"2022-05-14T06:53:47.87371Z","shell.execute_reply":"2022-05-14T06:53:47.880062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nsample = knn.kneighbors([x.iloc[0]])\nrcmnds = ids[sample[1][0]][:7]\nplot_items(rcmnds)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T07:05:35.319918Z","iopub.execute_input":"2022-05-14T07:05:35.320256Z","iopub.status.idle":"2022-05-14T07:05:38.389592Z","shell.execute_reply.started":"2022-05-14T07:05:35.320224Z","shell.execute_reply":"2022-05-14T07:05:38.388627Z"},"trusted":true},"execution_count":null,"outputs":[]}]}