{"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 pandas as pd\nimport numpy as np\nfrom tqdm.notebook import tqdm\nimport time\nfrom scipy.sparse import coo_matrix\nfrom collections import defaultdict\nimport gc\nfrom scipy import sparse\nimport csv\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics.pairwise import cosine_similarity\nfrom sklearn.preprocessing import LabelEncoder\ntqdm.pandas()\nimport matplotlib.image as mpimg\n\npd.set_option('display.max_columns', 500)\npd.set_option('display.width', 1000)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-06T00:01:00.592028Z","iopub.execute_input":"2023-03-06T00:01:00.592522Z","iopub.status.idle":"2023-03-06T00:01:00.601479Z","shell.execute_reply.started":"2023-03-06T00:01:00.592478Z","shell.execute_reply":"2023-03-06T00:01:00.600219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_path = '/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv'\nc_path = '/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv'\na_path ='/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv'\n\n%time transactions = pd.read_csv(t_path)\n%time customers = pd.read_csv(c_path)\n%time articles = pd.read_csv(a_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:22:12.474437Z","iopub.execute_input":"2023-03-05T23:22:12.475321Z","iopub.status.idle":"2023-03-05T23:23:50.423183Z","shell.execute_reply.started":"2023-03-05T23:22:12.475273Z","shell.execute_reply":"2023-03-05T23:23:50.422170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reduce transactions memory  \ncustomer_encoder = LabelEncoder()\ncustomer_encoder.fit(customers['customer_id'])\ncustomers['customer_id'] = customer_encoder.transform(customers['customer_id'])\n\narticle_encoder = LabelEncoder()\narticle_encoder.fit(articles['article_id'])\narticles['article_id'] = article_encoder.transform(articles['article_id'])\n\n%time transactions['customer_id'] = customer_encoder.transform(transactions['customer_id']).astype('int32')\n%time transactions['article_id'] = article_encoder.transform(transactions['article_id']).astype('int32')\n%time transactions['t_dat'] = pd.to_datetime(transactions['t_dat'])\n\ntransactions = transactions[['t_dat','customer_id','article_id']]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:23:50.425084Z","iopub.execute_input":"2023-03-05T23:23:50.426165Z","iopub.status.idle":"2023-03-05T23:24:28.474940Z","shell.execute_reply.started":"2023-03-05T23:23:50.426115Z","shell.execute_reply":"2023-03-05T23:24:28.473284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp2 = transactions.groupby(['customer_id', 'article_id'])['t_dat'].count().reset_index().rename(columns={'t_dat':'count'})\nmatrix = coo_matrix((tmp2['count'], (tmp2['article_id'], tmp2['customer_id'])))\n%time similarity_matrix = cosine_similarity(matrix, dense_output=False) # 6.9GB","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:24:28.476794Z","iopub.execute_input":"2023-03-05T23:24:28.477213Z","iopub.status.idle":"2023-03-05T23:25:46.143776Z","shell.execute_reply.started":"2023-03-05T23:24:28.477159Z","shell.execute_reply":"2023-03-05T23:25:46.142484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dct = defaultdict(list)\ntop_n = 12\nfor i in tqdm(range(100000,similarity_matrix.shape[0]+1)):\n    row = similarity_matrix[i].toarray()[0]\n    dct[i] = np.argsort(row)[::-1][1:top_n+1]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:36:54.399538Z","iopub.execute_input":"2023-03-05T23:36:54.399959Z","iopub.status.idle":"2023-03-05T23:37:11.661365Z","shell.execute_reply.started":"2023-03-05T23:36:54.399924Z","shell.execute_reply":"2023-03-05T23:37:11.659232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w = csv.writer(open(\"o11.csv\", \"w\"))\nfor article_id, similar_articles in dct.items():\n    w.writerow([article_id, similar_articles])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:37:26.036330Z","iopub.execute_input":"2023-03-05T23:37:26.036713Z","iopub.status.idle":"2023-03-05T23:37:26.404500Z","shell.execute_reply.started":"2023-03-05T23:37:26.036680Z","shell.execute_reply":"2023-03-05T23:37:26.403198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del dct\n# del tmp2\n# del matrix","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:37:27.408936Z","iopub.execute_input":"2023-03-05T23:37:27.409375Z","iopub.status.idle":"2023-03-05T23:37:27.418414Z","shell.execute_reply.started":"2023-03-05T23:37:27.409337Z","shell.execute_reply":"2023-03-05T23:37:27.416791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['o' + str(i) + '.csv' for i in list(range(1,12))]\ndfs = []\nfor fname in fnames:\n    dfs.append(pd.read_csv(fname, header=None).rename(columns={0:'article_id', 1:'similar_articles'}))","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:40:53.755235Z","iopub.execute_input":"2023-03-05T23:40:53.756451Z","iopub.status.idle":"2023-03-05T23:40:53.914058Z","shell.execute_reply.started":"2023-03-05T23:40:53.756407Z","shell.execute_reply":"2023-03-05T23:40:53.912930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sim = pd.concat([df.set_index('article_id') for df in dfs], axis=0)\ndf_sim = df_sim['similar_articles'].str.strip('[]').str.split(' ')\\\n    .apply(lambda lst: pd.Series(i for i in lst if i != ''))\nfor col in df_sim.columns:\n    df_sim[col] = df_sim[col].apply(lambda x: str(x).strip(' \\n\\t[]'))\ndf_sim = df_sim.astype('int32')\ndf_sim","metadata":{"execution":{"iopub.status.busy":"2023-03-05T23:48:32.833450Z","iopub.execute_input":"2023-03-05T23:48:32.833890Z","iopub.status.idle":"2023-03-05T23:48:55.595160Z","shell.execute_reply.started":"2023-03-05T23:48:32.833854Z","shell.execute_reply":"2023-03-05T23:48:55.593789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 5\nlst = np.random.randint(0,100000, size=n)\n\na = article_encoder.inverse_transform(df_sim.index.values)[lst]\nm1 = article_encoder.inverse_transform(df_sim[0])[lst]\nm2 = article_encoder.inverse_transform(df_sim[1])[lst]\nm3 = article_encoder.inverse_transform(df_sim[2])[lst]","metadata":{"execution":{"iopub.status.busy":"2023-03-06T00:13:32.447516Z","iopub.execute_input":"2023-03-06T00:13:32.447903Z","iopub.status.idle":"2023-03-06T00:13:32.504326Z","shell.execute_reply.started":"2023-03-06T00:13:32.447870Z","shell.execute_reply":"2023-03-06T00:13:32.503092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(n, 4, figsize=(30,15))\nroot = '/kaggle/input/h-and-m-personalized-fashion-recommendations/images/'\n\nfor idx in range(len(a)):\n    aname = '0' + str(a[idx])\n    m1name = '0' + str(m1[idx])\n    m2name = '0' + str(m2[idx])\n    m3name = '0' + str(m3[idx])\n    \n    ax[idx, 0].imshow(mpimg.imread(root + aname[:3] + '/' + aname + '.jpg'))\n    ax[idx, 1].imshow(mpimg.imread(root + m1name[:3] + '/' + m1name + '.jpg'))\n    ax[idx, 2].imshow(mpimg.imread(root + m2name[:3] + '/' + m2name + '.jpg'))\n    ax[idx, 3].imshow(mpimg.imread(root + m3name[:3] + '/' + m3name + '.jpg'))","metadata":{"execution":{"iopub.status.busy":"2023-03-06T00:13:32.596553Z","iopub.execute_input":"2023-03-06T00:13:32.597006Z","iopub.status.idle":"2023-03-06T00:13:43.954550Z","shell.execute_reply.started":"2023-03-06T00:13:32.596956Z","shell.execute_reply":"2023-03-06T00:13:43.952611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sim.to_csv('df_sim.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-06T00:14:34.561742Z","iopub.execute_input":"2023-03-06T00:14:34.562266Z","iopub.status.idle":"2023-03-06T00:14:35.035059Z","shell.execute_reply.started":"2023-03-06T00:14:34.562181Z","shell.execute_reply":"2023-03-06T00:14:35.033385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}