{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":false},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd \nimport os\nimport PIL\n\nfrom PIL import ImageOps\nfrom tqdm import tqdm\n\ntqdm.pandas()","execution_count":1,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":false},"cell_type":"code","source":"df_train = pd.read_csv('../input/train.csv')\nprint('Train.csv loaded!')","execution_count":2,"outputs":[{"output_type":"stream","text":"Train.csv loaded!\n","name":"stdout"}]},{"metadata":{"trusted":true,"_kg_hide-input":false},"cell_type":"code","source":"def get_img_size(img_id, path):      \n    img = PIL.Image.open(f'{path}{img_id}.png')\n    return img.size","execution_count":3,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":false},"cell_type":"code","source":"train_path = '../input/train/'\ndf_train['shape'] = df_train['id'].progress_apply(lambda x : get_img_size(x, train_path))\ndf_train['width'] = [width for width,_ in df_train['shape'].values]\ndf_train['height'] = [height for _,height in df_train['shape'].values]\ndf_train['aspect_ratio'] = df_train.height/df_train.width\ndf_train['std_width'] = (df_train.width - np.mean(df_train.width)) / np.std(df_train.width)\ndf_train['std_height'] = (df_train.height - np.mean(df_train.height)) / np.std(df_train.height)\ndf_train['std_aspect_ratio'] = (df_train.aspect_ratio - np.mean(df_train.aspect_ratio)) / np.std(df_train.aspect_ratio)\ndf_train.to_csv('train_with_wh.csv', index=False)","execution_count":4,"outputs":[{"output_type":"stream","text":"100%|██████████| 109237/109237 [04:38<00:00, 392.91it/s]\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":5,"outputs":[{"output_type":"execute_result","execution_count":5,"data":{"text/plain":"                 id       ...        std_aspect_ratio\n0  1000483014d91860       ...               -0.462639\n1  1000fe2e667721fe       ...               -0.796301\n2  1001614cb89646ee       ...               -0.582326\n3  10041eb49b297c08       ...                0.122864\n4  100501c227f8beea       ...                1.198770\n\n[5 rows x 9 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>attribute_ids</th>\n      <th>shape</th>\n      <th>width</th>\n      <th>height</th>\n      <th>aspect_ratio</th>\n      <th>std_width</th>\n      <th>std_height</th>\n      <th>std_aspect_ratio</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1000483014d91860</td>\n      <td>147 616 813</td>\n      <td>(339, 300)</td>\n      <td>339</td>\n      <td>300</td>\n      <td>0.884956</td>\n      <td>-0.179729</td>\n      <td>-0.582173</td>\n      <td>-0.462639</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1000fe2e667721fe</td>\n      <td>51 616 734 813</td>\n      <td>(423, 300)</td>\n      <td>423</td>\n      <td>300</td>\n      <td>0.709220</td>\n      <td>0.337904</td>\n      <td>-0.582173</td>\n      <td>-0.796301</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1001614cb89646ee</td>\n      <td>776</td>\n      <td>(365, 300)</td>\n      <td>365</td>\n      <td>300</td>\n      <td>0.821918</td>\n      <td>-0.019509</td>\n      <td>-0.582173</td>\n      <td>-0.582326</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>10041eb49b297c08</td>\n      <td>51 671 698 813 1092</td>\n      <td>(300, 358)</td>\n      <td>300</td>\n      <td>358</td>\n      <td>1.193333</td>\n      <td>-0.420058</td>\n      <td>-0.133053</td>\n      <td>0.122864</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>100501c227f8beea</td>\n      <td>13 404 492 903 1093</td>\n      <td>(300, 528)</td>\n      <td>300</td>\n      <td>528</td>\n      <td>1.760000</td>\n      <td>-0.420058</td>\n      <td>1.183333</td>\n      <td>1.198770</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.read_csv('../input/sample_submission.csv')","execution_count":6,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_path = '../input/test/'\ndf_test['shape'] = df_test['id'].progress_apply(lambda x : get_img_size(x, test_path))\ndf_test['width'] = [width for width,_ in df_test['shape'].values]\ndf_test['height'] = [height for _,height in df_test['shape'].values]\ndf_test['aspect_ratio'] = df_test.height/df_test.width\ndf_test['std_width'] = (df_test.width - np.mean(df_test.width)) / np.std(df_test.width)\ndf_test['std_height'] = (df_test.height - np.mean(df_test.height)) / np.std(df_test.height)\ndf_test['std_aspect_ratio'] = (df_test.aspect_ratio - np.mean(df_test.aspect_ratio)) / np.std(df_test.aspect_ratio)\ndf_test.to_csv('test_with_wh.csv', index=False)","execution_count":7,"outputs":[{"output_type":"stream","text":"100%|██████████| 7443/7443 [00:17<00:00, 437.22it/s]\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test.head()","execution_count":8,"outputs":[{"output_type":"execute_result","execution_count":8,"data":{"text/plain":"                 id       ...        std_aspect_ratio\n0  10023b2cc4ed5f68       ...                1.321055\n1  100fbe75ed8fd887       ...               -1.480735\n2  101b627524a04f19       ...               -0.740811\n3  10234480c41284c6       ...               -0.617821\n4  1023b0e2636dcea8       ...               -0.737010\n\n[5 rows x 9 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>attribute_ids</th>\n      <th>shape</th>\n      <th>width</th>\n      <th>height</th>\n      <th>aspect_ratio</th>\n      <th>std_width</th>\n      <th>std_height</th>\n      <th>std_aspect_ratio</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>10023b2cc4ed5f68</td>\n      <td>0 1 2</td>\n      <td>(300, 533)</td>\n      <td>300</td>\n      <td>533</td>\n      <td>1.776667</td>\n      <td>-0.410398</td>\n      <td>1.348411</td>\n      <td>1.321055</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>100fbe75ed8fd887</td>\n      <td>0 1 2</td>\n      <td>(781, 300)</td>\n      <td>781</td>\n      <td>300</td>\n      <td>0.384123</td>\n      <td>2.499366</td>\n      <td>-0.602236</td>\n      <td>-1.480735</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>101b627524a04f19</td>\n      <td>0 1 2</td>\n      <td>(399, 300)</td>\n      <td>399</td>\n      <td>300</td>\n      <td>0.751880</td>\n      <td>0.188493</td>\n      <td>-0.602236</td>\n      <td>-0.740811</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>10234480c41284c6</td>\n      <td>0 1 2</td>\n      <td>(369, 300)</td>\n      <td>369</td>\n      <td>300</td>\n      <td>0.813008</td>\n      <td>0.007011</td>\n      <td>-0.602236</td>\n      <td>-0.617821</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1023b0e2636dcea8</td>\n      <td>0 1 2</td>\n      <td>(398, 300)</td>\n      <td>398</td>\n      <td>300</td>\n      <td>0.753769</td>\n      <td>0.182444</td>\n      <td>-0.602236</td>\n      <td>-0.737010</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}