{"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 matplotlib.pyplot as plt # plotting and visualizing data\nimport cv2\nimport os\n\ntrain_df = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/train.csv')\ntrain_df.head()\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-21T15:07:08.859498Z","iopub.execute_input":"2022-03-21T15:07:08.860228Z","iopub.status.idle":"2022-03-21T15:07:09.406498Z","shell.execute_reply.started":"2022-03-21T15:07:08.860187Z","shell.execute_reply":"2022-03-21T15:07:09.405826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['species'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:09.408061Z","iopub.execute_input":"2022-03-21T15:07:09.408438Z","iopub.status.idle":"2022-03-21T15:07:09.424389Z","shell.execute_reply.started":"2022-03-21T15:07:09.408405Z","shell.execute_reply":"2022-03-21T15:07:09.423702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['species'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:09.425898Z","iopub.execute_input":"2022-03-21T15:07:09.426293Z","iopub.status.idle":"2022-03-21T15:07:09.440019Z","shell.execute_reply.started":"2022-03-21T15:07:09.426251Z","shell.execute_reply":"2022-03-21T15:07:09.439313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['species'] = train_df['species'].str.replace('beluga', 'beluga_whale')\ntrain_df['species'] = train_df['species'].str.replace('kiler_whale', 'killer_whale')\ntrain_df['species'] = train_df['species'].str.replace('globis', 'globis_whale')\ntrain_df['species'] = train_df['species'].str.replace('bottlenose_dolpin', 'bottlenose_dolphin')","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:09.442156Z","iopub.execute_input":"2022-03-21T15:07:09.442548Z","iopub.status.idle":"2022-03-21T15:07:09.620872Z","shell.execute_reply.started":"2022-03-21T15:07:09.442518Z","shell.execute_reply":"2022-03-21T15:07:09.619885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['species'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:09.622085Z","iopub.execute_input":"2022-03-21T15:07:09.622308Z","iopub.status.idle":"2022-03-21T15:07:09.633050Z","shell.execute_reply.started":"2022-03-21T15:07:09.622279Z","shell.execute_reply":"2022-03-21T15:07:09.631812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/happy-whale-and-dolphin/train_images'\ntest_dir = '/kaggle/input/happy-whale-and-dolphin/test_images'\n\n# function to get image paths from train and test directory\n\ndef getImagePaths(path):\n    image_names = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in filenames:\n            fullpath = os.path.join(dirname, filename)\n            image_names.append(fullpath)\n    return image_names\n\ntrain_images_path = getImagePaths(train_dir)\ntest_images_path = getImagePaths(test_dir)","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:09.634404Z","iopub.execute_input":"2022-03-21T15:07:09.634645Z","iopub.status.idle":"2022-03-21T15:07:48.531419Z","shell.execute_reply.started":"2022-03-21T15:07:09.634618Z","shell.execute_reply":"2022-03-21T15:07:48.530240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'] = train_df.species.map(lambda x: 'dolphin' if 'dolphin' in x else 'whale')","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:48.533146Z","iopub.execute_input":"2022-03-21T15:07:48.533392Z","iopub.status.idle":"2022-03-21T15:07:48.553804Z","shell.execute_reply.started":"2022-03-21T15:07:48.533364Z","shell.execute_reply":"2022-03-21T15:07:48.552811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:48.555518Z","iopub.execute_input":"2022-03-21T15:07:48.556098Z","iopub.status.idle":"2022-03-21T15:07:48.572951Z","shell.execute_reply.started":"2022-03-21T15:07:48.556063Z","shell.execute_reply":"2022-03-21T15:07:48.571899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:48.574275Z","iopub.execute_input":"2022-03-21T15:07:48.574526Z","iopub.status.idle":"2022-03-21T15:07:48.591174Z","shell.execute_reply.started":"2022-03-21T15:07:48.574496Z","shell.execute_reply":"2022-03-21T15:07:48.590533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(train_df['label'].value_counts().index, train_df['label'].value_counts().values)","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:48.593398Z","iopub.execute_input":"2022-03-21T15:07:48.594436Z","iopub.status.idle":"2022-03-21T15:07:48.984140Z","shell.execute_reply.started":"2022-03-21T15:07:48.594372Z","shell.execute_reply":"2022-03-21T15:07:48.983290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nplt.barh(train_df['species'].value_counts().index.str.replace('_', ' '), train_df['species'].value_counts().values, color=(0.2, 0.4, 0.6, 0.6))","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:48.985681Z","iopub.execute_input":"2022-03-21T15:07:48.986029Z","iopub.status.idle":"2022-03-21T15:07:49.404846Z","shell.execute_reply.started":"2022-03-21T15:07:48.985981Z","shell.execute_reply":"2022-03-21T15:07:49.403998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def frequency(df, col, freq):\n    n = 10\n    if freq == \"Most\":\n        return df[col].value_counts()[:n].index.tolist()\n    elif freq == \"Least\":\n        return df[col].value_counts()[-n:].index.tolist()\n    \nmost_freq_species = frequency(train_df,\"species\", \"Most\")\nleast_freq_species = frequency(train_df,\"species\", \"Least\")\nmost_freq_ID = frequency(train_df,\"individual_id\", \"Most\")\nleast_freq_ID = frequency(train_df,\"individual_id\", \"Least\")","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:49.406104Z","iopub.execute_input":"2022-03-21T15:07:49.406338Z","iopub.status.idle":"2022-03-21T15:07:49.455375Z","shell.execute_reply.started":"2022-03-21T15:07:49.406310Z","shell.execute_reply":"2022-03-21T15:07:49.454747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def path(df,group,group_type):\n    PATH = \"/kaggle/input/happy-whale-and-dolphin/train_images\"\n    \n    #species\n    if group_type=='sp':\n        z = df['image'][df['species']==group].values \n    \n    #ID\n    if group_type=='id':\n        z = df['image'][df['individual_id']==group].values \n   \n    image_names = []\n    for filename in z:\n        fullpath = os.path.join(PATH, filename)\n        image_names.append(fullpath)\n    return image_names","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:49.456339Z","iopub.execute_input":"2022-03-21T15:07:49.456942Z","iopub.status.idle":"2022-03-21T15:07:49.463285Z","shell.execute_reply.started":"2022-03-21T15:07:49.456910Z","shell.execute_reply":"2022-03-21T15:07:49.462286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_multiple_img(images_paths, rows, cols,title):\n    \n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8))\n    plt.suptitle(title, fontsize=20)\n    for ind,image_path in enumerate(images_paths):\n        image = cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[ind].imshow(image)\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:07:49.464451Z","iopub.execute_input":"2022-03-21T15:07:49.464687Z","iopub.status.idle":"2022-03-21T15:07:49.478084Z","shell.execute_reply.started":"2022-03-21T15:07:49.464657Z","shell.execute_reply":"2022-03-21T15:07:49.477324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_groups(df, group_type, lst):\n    for item in lst:\n        display_multiple_img(path(df,item,group_type)[:9], 3, 3,item)","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:09:52.166325Z","iopub.execute_input":"2022-03-21T15:09:52.166655Z","iopub.status.idle":"2022-03-21T15:09:52.172527Z","shell.execute_reply.started":"2022-03-21T15:09:52.166620Z","shell.execute_reply":"2022-03-21T15:09:52.171866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_groups(train_df,'sp', most_freq_species)","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:10:41.393227Z","iopub.execute_input":"2022-03-21T15:10:41.393445Z","iopub.status.idle":"2022-03-21T15:11:34.902445Z","shell.execute_reply.started":"2022-03-21T15:10:41.393417Z","shell.execute_reply":"2022-03-21T15:11:34.901296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_groups(train_df,'sp', least_freq_species)","metadata":{"execution":{"iopub.status.busy":"2022-03-21T15:09:59.517476Z","iopub.execute_input":"2022-03-21T15:09:59.517836Z","iopub.status.idle":"2022-03-21T15:10:41.391571Z","shell.execute_reply.started":"2022-03-21T15:09:59.517803Z","shell.execute_reply":"2022-03-21T15:10:41.387889Z"},"trusted":true},"execution_count":null,"outputs":[]}]}