{"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\nimport gc\nfrom tqdm import tqdm\nimport sys\n\ntqdm.pandas()\nsys.path.append('../')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-09T01:27:00.720241Z","iopub.execute_input":"2022-09-09T01:27:00.720622Z","iopub.status.idle":"2022-09-09T01:27:00.735980Z","shell.execute_reply.started":"2022-09-09T01:27:00.720536Z","shell.execute_reply":"2022-09-09T01:27:00.735025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Attempting to extract Gender as a Feature and view the distribution of Gender (using ladieswear and menswear product types to distinguish, cannot distinguish is left untouched (0 is not split/divided)","metadata":{}},{"cell_type":"code","source":"item = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:27:00.737463Z","iopub.execute_input":"2022-09-09T01:27:00.737979Z","iopub.status.idle":"2022-09-09T01:27:02.136928Z","shell.execute_reply.started":"2022-09-09T01:27:00.737946Z","shell.execute_reply":"2022-09-09T01:27:02.135729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_gender_flg(x):\n    female_pro_types = [\n        \"Bra\",\n        \"Underwear Tights\",\n        \"Leggings/Tights\",\n        \"Hair clip\",\n        \"Hair string\",\n        \"Hair/alice band\",\n        \"Bikini top\",\n        \"Skirt\",\n        \"Dress\",\n        \"Earring\",\n        \"Alice band\",\n        \"Straw hat\",\n        \"Necklace\",\n        \"Ballerinas\",\n        \"Blouse\",\n        \"Beanie\",\n        \"Giftbox\",\n        \"Pumps\",\n        \"Bootie\",\n        \"Heeled sandals\",\n        \"Nipple covers\",\n        \"Hair ties\",\n        \"Underwear corset\",\n        \"Bra extender\",\n        \"Underdress\",\n        \"Underwear set\",\n        \"Sarong\",\n        \"Leg warmers\",\n        \"Hairband\",\n        \"Tote bag\",\n        \"Earrings\",\n        \"Flat shoes\",\n        \"Heels\",\n        \"Cap\",\n        \"Shoulder bag\",\n        \"Headband\",\n        \"Baby Bib\",\n        \"Cross-body bag\",\n        \"Bumbag\",\n    ]\n    \n    x[\"gender\"] = 0 # * 0 for not divided, 1 for male, 2 for female\n    if x[\"index_group_name\"] == \"Ladieswear\":\n        x[\"gender\"] = 2\n    elif x[\"index_group_name\"] == \"Menswear\":\n        x[\"gender\"] = 1\n    else:\n        if (\n            \"boy\" in x[\"department_name\"].lower()\n            or \"men\" in x[\"department_name\"].lower()\n        ):\n            x[\"gender\"] = 1\n        if (\n            \"girl\" in x[\"department_name\"].lower()\n            or \"ladies\" in x[\"department_name\"].lower()\n            or x[\"product_type_name\"] in female_pro_types\n        ):\n            x[\"gender\"] = 2\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:27:02.138977Z","iopub.execute_input":"2022-09-09T01:27:02.139349Z","iopub.status.idle":"2022-09-09T01:27:02.149496Z","shell.execute_reply.started":"2022-09-09T01:27:02.139315Z","shell.execute_reply":"2022-09-09T01:27:02.148502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item = item.progress_apply(set_gender_flg, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:29:27.126174Z","iopub.execute_input":"2022-09-09T01:29:27.127445Z","iopub.status.idle":"2022-09-09T01:30:50.694524Z","shell.execute_reply.started":"2022-09-09T01:29:27.127395Z","shell.execute_reply":"2022-09-09T01:30:50.693390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntrans['t_dat'] = pd.to_datetime(trans['t_dat'])\ntrans['YYYY_MM'] = trans['t_dat'].dt.year.astype(str) + '_' + trans['t_dat'].dt.month.astype(str)\n# trans['week'] = (trans['t_dat'] - trans['t_dat'].min()).dt.days // 7\ntrans = pd.merge(trans, item[['article_id','gender','product_type_name']], on='article_id', how='left')\ndel item\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:30:50.696780Z","iopub.execute_input":"2022-09-09T01:30:50.697158Z","iopub.status.idle":"2022-09-09T01:32:33.849906Z","shell.execute_reply.started":"2022-09-09T01:30:50.697111Z","shell.execute_reply":"2022-09-09T01:32:33.848635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntrans = trans.merge(user[['customer_id','postal_code']], on='customer_id', how='left')\ndel user\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:32:33.851932Z","iopub.execute_input":"2022-09-09T01:32:33.852384Z","iopub.status.idle":"2022-09-09T01:33:03.187560Z","shell.execute_reply.started":"2022-09-09T01:32:33.852340Z","shell.execute_reply":"2022-09-09T01:33:03.186331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttl_cnt = trans.groupby(['customer_id']).size().reset_index(name='ttl_cnt')\ngender_sale = trans.groupby(['customer_id','gender']).size().reset_index(name='cnt')\ngender_sale = gender_sale.merge(ttl_cnt, on=['customer_id'], how='left')\ngender_sale['ratio'] = gender_sale['cnt'] / gender_sale['ttl_cnt']","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:03.189821Z","iopub.execute_input":"2022-09-09T01:33:03.190283Z","iopub.status.idle":"2022-09-09T01:33:32.744070Z","shell.execute_reply.started":"2022-09-09T01:33:03.190250Z","shell.execute_reply":"2022-09-09T01:33:32.742782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_sale = pd.pivot_table(gender_sale, values='ratio', index='customer_id', columns=['gender'])\ngender_sale = gender_sale.reset_index()\ngender_sale = gender_sale.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:32.745823Z","iopub.execute_input":"2022-09-09T01:33:32.746232Z","iopub.status.idle":"2022-09-09T01:33:38.607493Z","shell.execute_reply.started":"2022-09-09T01:33:32.746193Z","shell.execute_reply":"2022-09-09T01:33:38.606347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_sale['gender'] = 0\ngender_sale.loc[gender_sale[1]>=0.8, 'gender'] = 1\ngender_sale.loc[gender_sale[2]>=0.8, 'gender'] = 2\n","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:38.608950Z","iopub.execute_input":"2022-09-09T01:33:38.609325Z","iopub.status.idle":"2022-09-09T01:33:38.651687Z","shell.execute_reply.started":"2022-09-09T01:33:38.609290Z","shell.execute_reply":"2022-09-09T01:33:38.650455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_sale['gender'].hist()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:38.652979Z","iopub.execute_input":"2022-09-09T01:33:38.653350Z","iopub.status.idle":"2022-09-09T01:33:38.959488Z","shell.execute_reply.started":"2022-09-09T01:33:38.653313Z","shell.execute_reply":"2022-09-09T01:33:38.958071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Exploring sales trend for different product category types across the 2 year time period","metadata":{}},{"cell_type":"code","source":"ttl_sale = trans.groupby('product_type_name').size().reset_index(name='ttl_sale')\nmonth_sale = trans.groupby(['YYYY_MM','product_type_name']).size().reset_index(name='month_sale')\nmonth_sale = month_sale.merge(ttl_sale, on=['product_type_name'], how='left')\nmonth_sale['sale_ratio'] = month_sale['month_sale'] / month_sale['ttl_sale']\n# week_sale = trans.groupby(['week','product_type_name']).size().reset_index(name='week_sale')\n# week_sale = week_sale.merge(ttl_sale, on=['product_type_name'], how='left')\n# week_sale['sale_ratio'] = week_sale['week_sale'] / week_sale['ttl_s","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:38.960996Z","iopub.execute_input":"2022-09-09T01:33:38.961377Z","iopub.status.idle":"2022-09-09T01:33:50.050892Z","shell.execute_reply.started":"2022-09-09T01:33:38.961342Z","shell.execute_reply":"2022-09-09T01:33:50.049715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nsns.set_theme(style=\"whitegrid\")","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:50.052553Z","iopub.execute_input":"2022-09-09T01:33:50.052947Z","iopub.status.idle":"2022-09-09T01:33:50.823830Z","shell.execute_reply.started":"2022-09-09T01:33:50.052912Z","shell.execute_reply":"2022-09-09T01:33:50.822522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name in month_sale['product_type_name'].unique():\n    tmp = month_sale[month_sale['product_type_name']==name]\n    plt.figure(figsize=(12,6))\n    sns.lineplot(x='YYYY_MM', y='sale_ratio', data=tmp)\n    month = [\n        \"2018_9\",\n        \"2018_10\",\n        \"2018_11\",\n        \"2018_12\",\n        \"2019_1\",\n        \"2019_2\",\n        \"2019_3\",\n        \"2019_4\",\n        \"2019_5\",\n        \"2019_6\",\n        \"2019_7\",\n        \"2019_8\",\n        \"2019_9\",\n        \"2019_10\",\n        \"2019_11\",\n        \"2019_12\",\n        \"2020_1\",\n        \"2020_2\",\n        \"2020_3\",\n        \"2020_4\",\n        \"2020_5\",\n        \"2020_6\",\n        \"2020_7\",\n        \"2020_8\",\n        \"2020_9\",\n    ]\n    \n    plt.xticks(range(len(month)), month, rotation=60)\n    plt.title(name)\n    plt.show()\n# for name in week_sale['product_type_name'].unique():\n#     tmp = week_sale[week_sale['product_type_name']==name]\n#     plt.figure(figsize=(12,6))\n#     sns.lineplot(x='week', y='sale_ratio', data=tmp)\n#     plt.xticks(rotation=60)\n#     plt.title(name)\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T01:33:50.826669Z","iopub.execute_input":"2022-09-09T01:33:50.827053Z","iopub.status.idle":"2022-09-09T01:34:38.908363Z","shell.execute_reply.started":"2022-09-09T01:33:50.827017Z","shell.execute_reply":"2022-09-09T01:34:38.906980Z"},"trusted":true},"execution_count":null,"outputs":[]}]}