{"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\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-05-09T22:54:40.404510Z","iopub.execute_input":"2022-05-09T22:54:40.404953Z","iopub.status.idle":"2022-05-09T22:54:40.410715Z","shell.execute_reply.started":"2022-05-09T22:54:40.404903Z","shell.execute_reply":"2022-05-09T22:54:40.409917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Exploration**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns; sns.set()\nimport PIL\nimport pathlib\nfrom pathlib import Path\n\nfrom collections import Counter, defaultdict\nfrom PIL import Image\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:40.507787Z","iopub.execute_input":"2022-05-09T22:54:40.508540Z","iopub.status.idle":"2022-05-09T22:54:40.515878Z","shell.execute_reply.started":"2022-05-09T22:54:40.508504Z","shell.execute_reply":"2022-05-09T22:54:40.515241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/h-and-m-personalized-fashion-recommendations/images/'\ntrain_dir = pathlib.Path(train_dir)\narticles1 = list(train_dir.glob('011/*'))\nPIL.Image.open(str(articles1[2]))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:40.642480Z","iopub.execute_input":"2022-05-09T22:54:40.642908Z","iopub.status.idle":"2022-05-09T22:54:41.766202Z","shell.execute_reply.started":"2022-05-09T22:54:40.642870Z","shell.execute_reply":"2022-05-09T22:54:41.765186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIL.Image.open(str(articles1[1]))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:41.768146Z","iopub.execute_input":"2022-05-09T22:54:41.769057Z","iopub.status.idle":"2022-05-09T22:54:42.240499Z","shell.execute_reply.started":"2022-05-09T22:54:41.768977Z","shell.execute_reply":"2022-05-09T22:54:42.239421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIL.Image.open(str(articles1[8]))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:42.242101Z","iopub.execute_input":"2022-05-09T22:54:42.242397Z","iopub.status.idle":"2022-05-09T22:54:42.895438Z","shell.execute_reply.started":"2022-05-09T22:54:42.242359Z","shell.execute_reply":"2022-05-09T22:54:42.894566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles= pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\narticles","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:42.897740Z","iopub.execute_input":"2022-05-09T22:54:42.898266Z","iopub.status.idle":"2022-05-09T22:54:43.782628Z","shell.execute_reply.started":"2022-05-09T22:54:42.898223Z","shell.execute_reply":"2022-05-09T22:54:43.781786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in articles.columns:\n    print(col)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:43.784309Z","iopub.execute_input":"2022-05-09T22:54:43.784604Z","iopub.status.idle":"2022-05-09T22:54:43.792989Z","shell.execute_reply.started":"2022-05-09T22:54:43.784563Z","shell.execute_reply":"2022-05-09T22:54:43.792381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers= pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ncustomers","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:43.793952Z","iopub.execute_input":"2022-05-09T22:54:43.794215Z","iopub.status.idle":"2022-05-09T22:54:47.727453Z","shell.execute_reply.started":"2022-05-09T22:54:43.794186Z","shell.execute_reply":"2022-05-09T22:54:47.726505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction= pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\",nrows=100000)\ntransaction","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:47.728983Z","iopub.execute_input":"2022-05-09T22:54:47.729306Z","iopub.status.idle":"2022-05-09T22:54:47.905678Z","shell.execute_reply.started":"2022-05-09T22:54:47.729264Z","shell.execute_reply":"2022-05-09T22:54:47.904843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission= pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:47.907111Z","iopub.execute_input":"2022-05-09T22:54:47.907423Z","iopub.status.idle":"2022-05-09T22:54:51.275208Z","shell.execute_reply.started":"2022-05-09T22:54:47.907383Z","shell.execute_reply":"2022-05-09T22:54:51.274350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.merge(transaction, articles, on=\"article_id\", how=\"left\")\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:51.276582Z","iopub.execute_input":"2022-05-09T22:54:51.276896Z","iopub.status.idle":"2022-05-09T22:54:51.662118Z","shell.execute_reply.started":"2022-05-09T22:54:51.276854Z","shell.execute_reply":"2022-05-09T22:54:51.661246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df_train.columns:\n    print(col)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:51.664316Z","iopub.execute_input":"2022-05-09T22:54:51.664540Z","iopub.status.idle":"2022-05-09T22:54:51.672541Z","shell.execute_reply.started":"2022-05-09T22:54:51.664513Z","shell.execute_reply":"2022-05-09T22:54:51.670616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['prod_name'].value_counts()\ntype(df_train['prod_name'].value_counts())\ndf_article = df_train['prod_name'].value_counts().reset_index()\ndf_article.columns = ['Article', 'Count']\ndf_article","metadata":{"execution":{"iopub.status.busy":"2022-05-09T22:54:51.673909Z","iopub.execute_input":"2022-05-09T22:54:51.674234Z","iopub.status.idle":"2022-05-09T22:54:51.736372Z","shell.execute_reply.started":"2022-05-09T22:54:51.674195Z","shell.execute_reply":"2022-05-09T22:54:51.735800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train1 = df_train.groupby(['customer_id','article_id'])['article_id'].sum()\ndf_train1","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:23:51.544885Z","iopub.execute_input":"2022-05-09T23:23:51.545211Z","iopub.status.idle":"2022-05-09T23:23:51.656844Z","shell.execute_reply.started":"2022-05-09T23:23:51.545177Z","shell.execute_reply":"2022-05-09T23:23:51.655958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train2 = df_train.groupby(['article_id','customer_id'])['article_id'].sum()\nprint(df_train2)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:25:44.701223Z","iopub.execute_input":"2022-05-09T23:25:44.701501Z","iopub.status.idle":"2022-05-09T23:25:44.811122Z","shell.execute_reply.started":"2022-05-09T23:25:44.701472Z","shell.execute_reply":"2022-05-09T23:25:44.810252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_merge=pd.merge(df_train2,df_train1, on=\"customer_id\", how = \"right\")\ndf_train_merge_dataframe = pd.DataFrame(df_train_merge)\ndf_train_merge_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:25:48.343816Z","iopub.execute_input":"2022-05-09T23:25:48.344982Z","iopub.status.idle":"2022-05-09T23:25:48.459384Z","shell.execute_reply.started":"2022-05-09T23:25:48.344922Z","shell.execute_reply":"2022-05-09T23:25:48.458531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_final = df_train_merge_dataframe.drop(columns=['article_id_y'])\ndf_train_final","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:25:59.835052Z","iopub.execute_input":"2022-05-09T23:25:59.835582Z","iopub.status.idle":"2022-05-09T23:25:59.856654Z","shell.execute_reply.started":"2022-05-09T23:25:59.835529Z","shell.execute_reply":"2022-05-09T23:25:59.855390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = df_train_final.groupby('customer_id')['article_id_x'].agg(list)\nsubmission.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:26:34.171081Z","iopub.execute_input":"2022-05-09T23:26:34.171393Z","iopub.status.idle":"2022-05-09T23:26:34.686881Z","shell.execute_reply.started":"2022-05-09T23:26:34.171359Z","shell.execute_reply":"2022-05-09T23:26:34.685979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.DataFrame(submission)\ndf_submission_renamed = df_submission.rename(columns = {'article_id_x':'prediction'})\ndf_submission_reindexed = df_submission_renamed.reset_index()\ndf_submission_reindexed['prediction'] = df_submission_reindexed['prediction'].astype(str).str.replace(r'\\[|\\]|,', '')\ndf_submission_reindexed","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:26:37.471114Z","iopub.execute_input":"2022-05-09T23:26:37.471591Z","iopub.status.idle":"2022-05-09T23:26:37.890692Z","shell.execute_reply.started":"2022-05-09T23:26:37.471539Z","shell.execute_reply":"2022-05-09T23:26:37.889922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission=pd.merge(df_submission_reindexed,customers, on=\"customer_id\", how = \"right\")\ndf_submission","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:34:37.566829Z","iopub.execute_input":"2022-05-09T23:34:37.567571Z","iopub.status.idle":"2022-05-09T23:34:39.692964Z","shell.execute_reply.started":"2022-05-09T23:34:37.567520Z","shell.execute_reply":"2022-05-09T23:34:39.690869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission_final = df_submission.drop(columns=['FN','Active','club_member_status','fashion_news_frequency','age','postal_code'])\ndf_submission_final","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:37:46.483230Z","iopub.execute_input":"2022-05-09T23:37:46.484035Z","iopub.status.idle":"2022-05-09T23:37:46.958458Z","shell.execute_reply.started":"2022-05-09T23:37:46.483952Z","shell.execute_reply":"2022-05-09T23:37:46.957141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission_final.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T23:37:58.267688Z","iopub.execute_input":"2022-05-09T23:37:58.268532Z","iopub.status.idle":"2022-05-09T23:38:04.421462Z","shell.execute_reply.started":"2022-05-09T23:37:58.268486Z","shell.execute_reply":"2022-05-09T23:38:04.420147Z"},"trusted":true},"execution_count":null,"outputs":[]}]}