{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Data Loading","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt;","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-15T16:07:01.040399Z","iopub.execute_input":"2024-07-15T16:07:01.041568Z","iopub.status.idle":"2024-07-15T16:07:01.047278Z","shell.execute_reply.started":"2024-07-15T16:07:01.041517Z","shell.execute_reply":"2024-07-15T16:07:01.045488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:04.949652Z","iopub.execute_input":"2024-07-15T15:49:04.950323Z","iopub.status.idle":"2024-07-15T15:49:06.082114Z","shell.execute_reply.started":"2024-07-15T15:49:04.950288Z","shell.execute_reply":"2024-07-15T15:49:06.080984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle_dict: dict = {}\nfor _, (image_id, rle_str) in data.iterrows():\n    if isinstance(rle_str, float):\n        rle_dict[image_id] = []\n    elif image_id in rle_dict:\n        rle_dict[image_id].append(rle_str)\n    else:\n        rle_dict[image_id] = [rle_str]","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:06.083482Z","iopub.execute_input":"2024-07-15T15:49:06.083845Z","iopub.status.idle":"2024-07-15T15:49:18.758850Z","shell.execute_reply.started":"2024-07-15T15:49:06.083816Z","shell.execute_reply":"2024-07-15T15:49:18.757726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for key, value in rle_dict.items():\n    s = 0\n    for rle in value:\n        rles_list = rle.split()\n        for i in range(1, len(rles_list), 2):\n            s+=int(rles_list[i])\n    rle_dict[key] = (s, len(value))\n    ","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:18.761075Z","iopub.execute_input":"2024-07-15T15:49:18.761453Z","iopub.status.idle":"2024-07-15T15:49:20.655163Z","shell.execute_reply.started":"2024-07-15T15:49:18.761421Z","shell.execute_reply":"2024-07-15T15:49:20.654115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Analysis","metadata":{}},{"cell_type":"markdown","source":"## Ship count","metadata":{}},{"cell_type":"markdown","source":"### Min, Max ships per image","metadata":{}},{"cell_type":"code","source":"ship_count = data.groupby(\"ImageId\").count()\nprint(f\"min={ship_count.min().iloc[0]}, max={ship_count.max().iloc[0]}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:20.656506Z","iopub.execute_input":"2024-07-15T15:49:20.656940Z","iopub.status.idle":"2024-07-15T15:49:20.848683Z","shell.execute_reply.started":"2024-07-15T15:49:20.656908Z","shell.execute_reply":"2024-07-15T15:49:20.847551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Ships distribution ","metadata":{}},{"cell_type":"code","source":"ship_count= data.groupby(\"ImageId\", as_index=False).count().groupby(\"EncodedPixels\", as_index=False, ).count()\nship_count.T","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:20.849969Z","iopub.execute_input":"2024-07-15T15:49:20.850279Z","iopub.status.idle":"2024-07-15T15:49:21.053108Z","shell.execute_reply.started":"2024-07-15T15:49:20.850251Z","shell.execute_reply":"2024-07-15T15:49:21.051772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.bar(x=ship_count[\"EncodedPixels\"],\n        height=ship_count[\"ImageId\"],\n        tick_label=ship_count[\"EncodedPixels\"]\n       )","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:21.054735Z","iopub.execute_input":"2024-07-15T15:49:21.055237Z","iopub.status.idle":"2024-07-15T15:49:21.384146Z","shell.execute_reply.started":"2024-07-15T15:49:21.055201Z","shell.execute_reply":"2024-07-15T15:49:21.382563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Non-zero distribution: ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.bar(x=ship_count[\"EncodedPixels\"].iloc[1:],\n        height=ship_count[\"ImageId\"].iloc[1:],\n        tick_label=ship_count[\"EncodedPixels\"].iloc[1:]\n       )","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:21.385611Z","iopub.execute_input":"2024-07-15T15:49:21.386004Z","iopub.status.idle":"2024-07-15T15:49:21.712087Z","shell.execute_reply.started":"2024-07-15T15:49:21.385973Z","shell.execute_reply":"2024-07-15T15:49:21.710754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ship Areas","metadata":{}},{"cell_type":"markdown","source":"### Total Ships Areas per Image","metadata":{}},{"cell_type":"code","source":"areas = pd.DataFrame.from_dict(rle_dict, orient=\"index\",  columns=[\"Area\", \"ShipCount\"])\nareas_grouped = (areas.groupby(\"ShipCount\").sum()/areas.groupby(\"ShipCount\").count()).astype(int)\nareas_grouped.T","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:21.713533Z","iopub.execute_input":"2024-07-15T15:49:21.713922Z","iopub.status.idle":"2024-07-15T15:49:21.877884Z","shell.execute_reply.started":"2024-07-15T15:49:21.713890Z","shell.execute_reply":"2024-07-15T15:49:21.876493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.bar(x=areas_grouped.index, height=areas_grouped[\"Area\"], tick_label=areas_grouped.index)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T15:49:21.880717Z","iopub.execute_input":"2024-07-15T15:49:21.881101Z","iopub.status.idle":"2024-07-15T15:49:22.222132Z","shell.execute_reply.started":"2024-07-15T15:49:21.881069Z","shell.execute_reply":"2024-07-15T15:49:22.220997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Average area of ONE ship in ONE image","metadata":{}},{"cell_type":"code","source":"area_per_single_ship = ((\n    areas.groupby(\"ShipCount\").sum()/areas.groupby(\"ShipCount\").count()\n).iloc[1:].T/(areas.groupby(\"ShipCount\", as_index=False).count()['ShipCount'].iloc[1:])).T\narea_per_single_ship.T","metadata":{"execution":{"iopub.status.busy":"2024-07-15T16:05:44.972565Z","iopub.execute_input":"2024-07-15T16:05:44.972997Z","iopub.status.idle":"2024-07-15T16:05:45.010439Z","shell.execute_reply.started":"2024-07-15T16:05:44.972966Z","shell.execute_reply":"2024-07-15T16:05:45.008952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.bar(x=area_per_single_ship.index, height=area_per_single_ship[\"Area\"], tick_label=area_per_single_ship.index)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T16:05:48.601177Z","iopub.execute_input":"2024-07-15T16:05:48.601565Z","iopub.status.idle":"2024-07-15T16:05:48.930807Z","shell.execute_reply.started":"2024-07-15T16:05:48.601534Z","shell.execute_reply":"2024-07-15T16:05:48.929729Z"},"trusted":true},"execution_count":null,"outputs":[]}]}