{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nfrom pathlib import Path\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-07T15:07:34.098979Z","iopub.execute_input":"2022-02-07T15:07:34.099328Z","iopub.status.idle":"2022-02-07T15:07:34.500890Z","shell.execute_reply.started":"2022-02-07T15:07:34.099247Z","shell.execute_reply":"2022-02-07T15:07:34.500265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Species Distribution\n### Number of Species in this dataset","metadata":{}},{"cell_type":"code","source":"label = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/train.csv')\nlabel.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:34.502270Z","iopub.execute_input":"2022-02-07T15:07:34.502650Z","iopub.status.idle":"2022-02-07T15:07:34.620823Z","shell.execute_reply.started":"2022-02-07T15:07:34.502604Z","shell.execute_reply":"2022-02-07T15:07:34.620302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:08:30.024152Z","iopub.execute_input":"2022-02-07T15:08:30.024527Z","iopub.status.idle":"2022-02-07T15:08:30.031015Z","shell.execute_reply.started":"2022-02-07T15:08:30.024486Z","shell.execute_reply":"2022-02-07T15:08:30.030382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"species = label.species.unique().tolist()\nprint('Number of species in the dataset:', len(species))","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:34.621922Z","iopub.execute_input":"2022-02-07T15:07:34.622298Z","iopub.status.idle":"2022-02-07T15:07:34.636466Z","shell.execute_reply.started":"2022-02-07T15:07:34.622250Z","shell.execute_reply":"2022-02-07T15:07:34.635893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Species Distribution\nLet's look at the species distribution:","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,8))\nlabel.species.value_counts().plot(kind='barh')\nplt.title('Distribution of Species')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:34.638185Z","iopub.execute_input":"2022-02-07T15:07:34.638623Z","iopub.status.idle":"2022-02-07T15:07:35.150143Z","shell.execute_reply.started":"2022-02-07T15:07:34.638521Z","shell.execute_reply":"2022-02-07T15:07:35.149352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Some Paths:","metadata":{}},{"cell_type":"code","source":"ROOT_PATH = Path('/kaggle/input/happy-whale-and-dolphin/train_images/')","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:35.151319Z","iopub.execute_input":"2022-02-07T15:07:35.151560Z","iopub.status.idle":"2022-02-07T15:07:35.155884Z","shell.execute_reply.started":"2022-02-07T15:07:35.151532Z","shell.execute_reply":"2022-02-07T15:07:35.154862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize Samples\nLet's look at one random training image:","metadata":{}},{"cell_type":"code","source":"sample = '002ac5f9ad7c10.jpg'\nimg0 = ROOT_PATH/sample\nimg0 = img.imread(img0)\nplt.imshow(img0)\nplt.title(label.loc[label.image==sample, 'species'].values[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:35.157558Z","iopub.execute_input":"2022-02-07T15:07:35.157852Z","iopub.status.idle":"2022-02-07T15:07:36.541405Z","shell.execute_reply.started":"2022-02-07T15:07:35.157813Z","shell.execute_reply":"2022-02-07T15:07:36.540765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Take a look at first 9 images:","metadata":{}},{"cell_type":"code","source":"fig, axis = plt.subplots(3,3, figsize=(9,9))\nc = 0\nfor i in range(3):\n    for j in range(3):\n        image_id = label.image.iloc[c]\n        image_path = ROOT_PATH/image_id\n        image = img.imread(image_path)\n        \n        image_label = label.species.iloc[c]\n        \n        axis[i][j].imshow(image)\n        axis[i][j].set_title(image_label)\n        axis[i][j].set_xticks([])\n        axis[i][j].set_yticks([])\n        c += 1\nplt.tight_layout()\nplt.show()    ","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:36.542563Z","iopub.execute_input":"2022-02-07T15:07:36.542923Z","iopub.status.idle":"2022-02-07T15:07:42.725496Z","shell.execute_reply.started":"2022-02-07T15:07:36.542875Z","shell.execute_reply":"2022-02-07T15:07:42.724891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next, le's take a look at each species:","metadata":{}},{"cell_type":"code","source":"fig, axis = plt.subplots(5,6, figsize=(12,10))\nc = 0\nfor i in range(5):\n    for j in range(6):\n        image_id = label.drop_duplicates(subset='species', inplace=False).image.iloc[c]\n        image_path = ROOT_PATH/image_id\n        image = img.imread(image_path)\n\n        image_label = label.drop_duplicates(subset='species', inplace=False).species.iloc[c]\n        \n        axis[i][j].imshow(image)\n        axis[i][j].set_title(image_label)\n        axis[i][j].set_xticks([])\n        axis[i][j].set_yticks([])\n        c += 1\nplt.suptitle('Example of Every Species in Training Dataset')\nplt.tight_layout()\nplt.show()    ","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:07:42.726682Z","iopub.execute_input":"2022-02-07T15:07:42.727053Z","iopub.status.idle":"2022-02-07T15:07:55.895047Z","shell.execute_reply.started":"2022-02-07T15:07:42.727009Z","shell.execute_reply":"2022-02-07T15:07:55.894449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Look at Target\nIn this task, we are asked to predict the individual_id. So I want to see how many unique individual_id there are.","metadata":{}},{"cell_type":"code","source":"print('Number of Unique individual_id:',len(label.individual_id.unique()))","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:10:55.921800Z","iopub.execute_input":"2022-02-07T15:10:55.922546Z","iopub.status.idle":"2022-02-07T15:10:55.934921Z","shell.execute_reply.started":"2022-02-07T15:10:55.922500Z","shell.execute_reply":"2022-02-07T15:10:55.934241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot multiple picture for the same individual_id","metadata":{}},{"cell_type":"code","source":"label.value_counts('individual_id')","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:15:29.835521Z","iopub.execute_input":"2022-02-07T15:15:29.835891Z","iopub.status.idle":"2022-02-07T15:15:29.883014Z","shell.execute_reply.started":"2022-02-07T15:15:29.835849Z","shell.execute_reply":"2022-02-07T15:15:29.882201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see individual_id of **37c7aba965a5** has most of the training images. Let's take a look:","metadata":{}},{"cell_type":"code","source":"fig, axis = plt.subplots(3,3, figsize=(10,10))\nc = 0\nfor i in range(3):\n    for j in range(3):\n        image_id = label[label.individual_id=='37c7aba965a5'].image.iloc[c]\n        image_path = ROOT_PATH/image_id\n        image = img.imread(image_path)\n        \n        axis[i][j].imshow(image)\n        axis[i][j].set_xticks([])\n        axis[i][j].set_yticks([])\n        c += 1\nplt.suptitle('Individual_ID: 37c7aba965a5, Species:'+label[label.individual_id=='37c7aba965a5'].species.unique()[0])\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:36:44.184194Z","iopub.execute_input":"2022-02-07T15:36:44.184673Z","iopub.status.idle":"2022-02-07T15:36:52.677197Z","shell.execute_reply.started":"2022-02-07T15:36:44.184622Z","shell.execute_reply":"2022-02-07T15:36:52.676206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Take another look at id **c995c043c353**","metadata":{}},{"cell_type":"code","source":"fig, axis = plt.subplots(3,3, figsize=(10,10))\nc = 0\nfor i in range(3):\n    for j in range(3):\n        image_id = label[label.individual_id=='c995c043c353'].image.iloc[c]\n        image_path = ROOT_PATH/image_id\n        image = img.imread(image_path)\n        \n        # image_label = label[label.individual_id=='37c7aba965a5'].species.iloc[c]\n        \n        axis[i][j].imshow(image)\n        axis[i][j].set_xticks([])\n        axis[i][j].set_yticks([])\n        c += 1\nplt.suptitle('Individual_ID: 37c7aba965a5, Species:'+label[label.individual_id=='c995c043c353'].species.unique()[0])\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T15:34:58.443904Z","iopub.execute_input":"2022-02-07T15:34:58.444451Z","iopub.status.idle":"2022-02-07T15:35:06.673421Z","shell.execute_reply.started":"2022-02-07T15:34:58.444396Z","shell.execute_reply":"2022-02-07T15:35:06.672274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}