{"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":"from PIL import Image\nimport math\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-03-25T09:43:21.229528Z","iopub.execute_input":"2022-03-25T09:43:21.229806Z","iopub.status.idle":"2022-03-25T09:43:21.234052Z","shell.execute_reply.started":"2022-03-25T09:43:21.229778Z","shell.execute_reply":"2022-03-25T09:43:21.232996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_IMAGE_PATH = '../input/hm-fashion-images-squared-224/images_224x224'\n\ndef image_path_for_article(article_id):\n    id = f'{article_id:010d}'\n    return(f'{ROOT_IMAGE_PATH}/{id[:3]}/{id}.jpg')   ","metadata":{"execution":{"iopub.status.busy":"2022-03-25T09:43:19.569378Z","iopub.execute_input":"2022-03-25T09:43:19.570255Z","iopub.status.idle":"2022-03-25T09:43:19.575651Z","shell.execute_reply.started":"2022-03-25T09:43:19.570211Z","shell.execute_reply":"2022-03-25T09:43:19.574727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_image_grid(image_paths, size=150, max_cols=5, background_color=(255, 255, 255)):\n    image_count = len(image_paths)\n    cols = min(max_cols, image_count)\n    rows = 0 if cols == 0 else math.ceil(image_count/cols)\n\n    result_image = Image.new('RGB', (cols*size, rows*size), color=background_color)\n    for idx, image_path in enumerate(image_paths):\n        image = Image.open(image_path).resize((size, size)) if image_path!='' else Image.new('RGB', (size, size))\n        (row, col) = divmod(idx, max_cols)\n        result_image.paste(image, (col*size, row*size))\n    return(result_image)","metadata":{"execution":{"iopub.status.busy":"2022-03-25T09:42:39.705483Z","iopub.execute_input":"2022-03-25T09:42:39.706146Z","iopub.status.idle":"2022-03-25T09:42:39.714427Z","shell.execute_reply.started":"2022-03-25T09:42:39.706101Z","shell.execute_reply":"2022-03-25T09:42:39.713327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_articles(article_id, size=150, max_cols=5):\n    if hasattr(article_id, '__iter__'):\n        image_paths = [image_path_for_article(id) for id in article_id]\n        image_paths = [image_path if os.path.isfile(image_path) else '' for image_path in image_paths]                \n        return(create_image_grid(image_paths, size, max_cols))\n    else:\n        return Image.open(image_path_for_article(article_id)).resize((size, size))","metadata":{"execution":{"iopub.status.busy":"2022-03-25T09:48:25.8402Z","iopub.execute_input":"2022-03-25T09:48:25.840495Z","iopub.status.idle":"2022-03-25T09:48:25.846661Z","shell.execute_reply.started":"2022-03-25T09:48:25.840462Z","shell.execute_reply":"2022-03-25T09:48:25.846023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_articles(653551001, size=200)","metadata":{"execution":{"iopub.status.busy":"2022-03-25T09:50:19.703041Z","iopub.execute_input":"2022-03-25T09:50:19.704015Z","iopub.status.idle":"2022-03-25T09:50:19.722257Z","shell.execute_reply.started":"2022-03-25T09:50:19.703975Z","shell.execute_reply":"2022-03-25T09:50:19.72165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nsales = pd.read_parquet('../input/hm-fashion-recommendation-parquet/sales.parquet')\ncustomers = pd.read_parquet('../input/hm-fashion-recommendation-parquet/customers.parquet')","metadata":{"execution":{"iopub.status.busy":"2022-03-25T09:48:26.259532Z","iopub.execute_input":"2022-03-25T09:48:26.260129Z","iopub.status.idle":"2022-03-25T09:48:29.441355Z","shell.execute_reply.started":"2022-03-25T09:48:26.260081Z","shell.execute_reply":"2022-03-25T09:48:29.440725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_ids = customers[customers.age.between(40, 50)].merge(sales).groupby('article_id').size().nlargest(20).index\nshow_articles(article_ids, size=150, max_cols=5)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-03-25T09:50:59.434591Z","iopub.execute_input":"2022-03-25T09:50:59.434937Z","iopub.status.idle":"2022-03-25T09:51:02.63588Z","shell.execute_reply.started":"2022-03-25T09:50:59.434898Z","shell.execute_reply":"2022-03-25T09:51:02.635031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}