{"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":"markdown","source":"## 必要なライブラリのインポート","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport os\n\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:02.012036Z","iopub.execute_input":"2022-02-05T09:37:02.012361Z","iopub.status.idle":"2022-02-05T09:37:02.377665Z","shell.execute_reply.started":"2022-02-05T09:37:02.012281Z","shell.execute_reply":"2022-02-05T09:37:02.376785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:02.379240Z","iopub.execute_input":"2022-02-05T09:37:02.379547Z","iopub.status.idle":"2022-02-05T09:37:02.492172Z","shell.execute_reply.started":"2022-02-05T09:37:02.379508Z","shell.execute_reply":"2022-02-05T09:37:02.491230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 個体別に表示","metadata":{}},{"cell_type":"markdown","source":"- まず個体別にみてみます","metadata":{}},{"cell_type":"code","source":"idval_data = train_data.groupby('individual_id')['image'].count().sort_values(ascending=True)\nidval_data","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:02.493665Z","iopub.execute_input":"2022-02-05T09:37:02.494213Z","iopub.status.idle":"2022-02-05T09:37:02.533402Z","shell.execute_reply.started":"2022-02-05T09:37:02.494177Z","shell.execute_reply":"2022-02-05T09:37:02.532597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- 上の結果だけでは分かりにくいので、1枚の画像しかない個体の数、2枚の画像しかない個体の数、3枚から5枚の画像がある個体の数...といった感じで、画像の枚数ごとに分けてみます","metadata":{}},{"cell_type":"code","source":"idval_ctgr_list = [0,1,2,5,20,100,1000]\nidval_ctgr_name = ['1','2','3-5','6-20','21-100','100-']\nidval_ctgr = pd.cut(idval_data, bins=idval_ctgr_list, labels=idval_ctgr_name)\nidval_ctgr","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:02.535967Z","iopub.execute_input":"2022-02-05T09:37:02.536289Z","iopub.status.idle":"2022-02-05T09:37:02.550165Z","shell.execute_reply.started":"2022-02-05T09:37:02.536249Z","shell.execute_reply":"2022-02-05T09:37:02.549278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ほとんどの個体が、学習データとして1枚または2枚の画像しかもたないことが分かります","metadata":{}},{"cell_type":"code","source":"plot_data = idval_ctgr.value_counts().sort_index()\nplt.bar(plot_data.index, plot_data)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:02.551475Z","iopub.execute_input":"2022-02-05T09:37:02.551885Z","iopub.status.idle":"2022-02-05T09:37:02.759488Z","shell.execute_reply.started":"2022-02-05T09:37:02.551854Z","shell.execute_reply":"2022-02-05T09:37:02.758782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 種類別の画像枚数を表示","metadata":{}},{"cell_type":"markdown","source":"- 次にtrain_imagesの中に、各種類ごとに何枚ずつのデータがあるのかを調べてみます","metadata":{}},{"cell_type":"code","source":"species_count = train_data.groupby(\"species\")[\"image\"].count()\nspecies_count = species_count.sort_values(ascending=False)\nprint(species_count)\nspecies_count.plot(kind=\"bar\")","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:02.760407Z","iopub.execute_input":"2022-02-05T09:37:02.760705Z","iopub.status.idle":"2022-02-05T09:37:03.260619Z","shell.execute_reply.started":"2022-02-05T09:37:02.760678Z","shell.execute_reply":"2022-02-05T09:37:03.259757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"species_name_list = species_count.index\nspecies_name_list","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:03.261749Z","iopub.execute_input":"2022-02-05T09:37:03.261979Z","iopub.status.idle":"2022-02-05T09:37:03.268196Z","shell.execute_reply.started":"2022-02-05T09:37:03.261950Z","shell.execute_reply":"2022-02-05T09:37:03.267210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## sample画像の表示","metadata":{}},{"cell_type":"markdown","source":"- train_imagesには全部で30種類の種の画像があります. 各種類の画像を見てみましょう.","metadata":{}},{"cell_type":"code","source":"def display_sample_image(species_num=0):\n    \"\"\"\n    species_numで0から29の数値を指定すると、対応する種の画像がランダムに4枚表示されます。\n    \"\"\"\n    species_num %= 29\n    base_path = '../input/happy-whale-and-dolphin/train_images'\n    fig = plt.figure(figsize=(12,8))\n    plt.title(species_name_list[species_num])\n    plt.yticks([])\n    plt.xticks([])\n    \n    train_species_data = train_data[train_data.species==species_name_list[species_num]].reset_index(drop=True)\n    for i in range(4):\n        index = np.random.randint(len(train_species_data))\n        image_path = os.path.join(base_path, train_species_data.loc[index][\"image\"])\n        sample_pic = cv2.imread(image_path)\n        \n        ax = fig.add_subplot(2,2,i+1)\n        ax.imshow(sample_pic)\n#     plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:03.269605Z","iopub.execute_input":"2022-02-05T09:37:03.269946Z","iopub.status.idle":"2022-02-05T09:37:03.281949Z","shell.execute_reply.started":"2022-02-05T09:37:03.269906Z","shell.execute_reply":"2022-02-05T09:37:03.281005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(0)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:03.283002Z","iopub.execute_input":"2022-02-05T09:37:03.283453Z","iopub.status.idle":"2022-02-05T09:37:09.010230Z","shell.execute_reply.started":"2022-02-05T09:37:03.283418Z","shell.execute_reply":"2022-02-05T09:37:09.009450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(1)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:09.012438Z","iopub.execute_input":"2022-02-05T09:37:09.013234Z","iopub.status.idle":"2022-02-05T09:37:10.238436Z","shell.execute_reply.started":"2022-02-05T09:37:09.013180Z","shell.execute_reply":"2022-02-05T09:37:10.237696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:10.239850Z","iopub.execute_input":"2022-02-05T09:37:10.240298Z","iopub.status.idle":"2022-02-05T09:37:15.829524Z","shell.execute_reply.started":"2022-02-05T09:37:10.240261Z","shell.execute_reply":"2022-02-05T09:37:15.828704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:15.830899Z","iopub.execute_input":"2022-02-05T09:37:15.831165Z","iopub.status.idle":"2022-02-05T09:37:17.991919Z","shell.execute_reply.started":"2022-02-05T09:37:15.831133Z","shell.execute_reply":"2022-02-05T09:37:17.990959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(4)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:17.993302Z","iopub.execute_input":"2022-02-05T09:37:17.993605Z","iopub.status.idle":"2022-02-05T09:37:20.935755Z","shell.execute_reply.started":"2022-02-05T09:37:17.993557Z","shell.execute_reply":"2022-02-05T09:37:20.934920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(5)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:20.936941Z","iopub.execute_input":"2022-02-05T09:37:20.937142Z","iopub.status.idle":"2022-02-05T09:37:22.528797Z","shell.execute_reply.started":"2022-02-05T09:37:20.937115Z","shell.execute_reply":"2022-02-05T09:37:22.527895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(6)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:22.530067Z","iopub.execute_input":"2022-02-05T09:37:22.530362Z","iopub.status.idle":"2022-02-05T09:37:23.834751Z","shell.execute_reply.started":"2022-02-05T09:37:22.530326Z","shell.execute_reply":"2022-02-05T09:37:23.833922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(7)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:23.835978Z","iopub.execute_input":"2022-02-05T09:37:23.836659Z","iopub.status.idle":"2022-02-05T09:37:25.915786Z","shell.execute_reply.started":"2022-02-05T09:37:23.836617Z","shell.execute_reply":"2022-02-05T09:37:25.915010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(8)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:25.916863Z","iopub.execute_input":"2022-02-05T09:37:25.917073Z","iopub.status.idle":"2022-02-05T09:37:30.882644Z","shell.execute_reply.started":"2022-02-05T09:37:25.917047Z","shell.execute_reply":"2022-02-05T09:37:30.881788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(9)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:30.883763Z","iopub.execute_input":"2022-02-05T09:37:30.883969Z","iopub.status.idle":"2022-02-05T09:37:35.849496Z","shell.execute_reply.started":"2022-02-05T09:37:30.883944Z","shell.execute_reply":"2022-02-05T09:37:35.848753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(10)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:35.850554Z","iopub.execute_input":"2022-02-05T09:37:35.850765Z","iopub.status.idle":"2022-02-05T09:37:40.792593Z","shell.execute_reply.started":"2022-02-05T09:37:35.850740Z","shell.execute_reply":"2022-02-05T09:37:40.791994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(11)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:40.793591Z","iopub.execute_input":"2022-02-05T09:37:40.794250Z","iopub.status.idle":"2022-02-05T09:37:47.011868Z","shell.execute_reply.started":"2022-02-05T09:37:40.794212Z","shell.execute_reply":"2022-02-05T09:37:47.010995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(12)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:47.013276Z","iopub.execute_input":"2022-02-05T09:37:47.013699Z","iopub.status.idle":"2022-02-05T09:37:49.257533Z","shell.execute_reply.started":"2022-02-05T09:37:47.013649Z","shell.execute_reply":"2022-02-05T09:37:49.256739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(13)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:49.258840Z","iopub.execute_input":"2022-02-05T09:37:49.259387Z","iopub.status.idle":"2022-02-05T09:37:55.541229Z","shell.execute_reply.started":"2022-02-05T09:37:49.259340Z","shell.execute_reply":"2022-02-05T09:37:55.540439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(14)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:55.542589Z","iopub.execute_input":"2022-02-05T09:37:55.543052Z","iopub.status.idle":"2022-02-05T09:37:59.252260Z","shell.execute_reply.started":"2022-02-05T09:37:55.543009Z","shell.execute_reply":"2022-02-05T09:37:59.251564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(15)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:37:59.253394Z","iopub.execute_input":"2022-02-05T09:37:59.253786Z","iopub.status.idle":"2022-02-05T09:38:00.702282Z","shell.execute_reply.started":"2022-02-05T09:37:59.253756Z","shell.execute_reply":"2022-02-05T09:38:00.701605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(16)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:00.703423Z","iopub.execute_input":"2022-02-05T09:38:00.703817Z","iopub.status.idle":"2022-02-05T09:38:05.913345Z","shell.execute_reply.started":"2022-02-05T09:38:00.703772Z","shell.execute_reply":"2022-02-05T09:38:05.912592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(17)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:05.914465Z","iopub.execute_input":"2022-02-05T09:38:05.914718Z","iopub.status.idle":"2022-02-05T09:38:09.555275Z","shell.execute_reply.started":"2022-02-05T09:38:05.914687Z","shell.execute_reply":"2022-02-05T09:38:09.554649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(18)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:09.556246Z","iopub.execute_input":"2022-02-05T09:38:09.556938Z","iopub.status.idle":"2022-02-05T09:38:12.558411Z","shell.execute_reply.started":"2022-02-05T09:38:09.556901Z","shell.execute_reply":"2022-02-05T09:38:12.557579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(19)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:12.562909Z","iopub.execute_input":"2022-02-05T09:38:12.563345Z","iopub.status.idle":"2022-02-05T09:38:18.490359Z","shell.execute_reply.started":"2022-02-05T09:38:12.563298Z","shell.execute_reply":"2022-02-05T09:38:18.489430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(20)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:18.491592Z","iopub.execute_input":"2022-02-05T09:38:18.491905Z","iopub.status.idle":"2022-02-05T09:38:21.211025Z","shell.execute_reply.started":"2022-02-05T09:38:18.491870Z","shell.execute_reply":"2022-02-05T09:38:21.210077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(21)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:21.212266Z","iopub.execute_input":"2022-02-05T09:38:21.212494Z","iopub.status.idle":"2022-02-05T09:38:25.966709Z","shell.execute_reply.started":"2022-02-05T09:38:21.212465Z","shell.execute_reply":"2022-02-05T09:38:25.965927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(22)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:25.967762Z","iopub.execute_input":"2022-02-05T09:38:25.967958Z","iopub.status.idle":"2022-02-05T09:38:31.697585Z","shell.execute_reply.started":"2022-02-05T09:38:25.967934Z","shell.execute_reply":"2022-02-05T09:38:31.696855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(23)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:31.698691Z","iopub.execute_input":"2022-02-05T09:38:31.699488Z","iopub.status.idle":"2022-02-05T09:38:35.023562Z","shell.execute_reply.started":"2022-02-05T09:38:31.699441Z","shell.execute_reply":"2022-02-05T09:38:35.022788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(24)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:35.024614Z","iopub.execute_input":"2022-02-05T09:38:35.024858Z","iopub.status.idle":"2022-02-05T09:38:36.440604Z","shell.execute_reply.started":"2022-02-05T09:38:35.024829Z","shell.execute_reply":"2022-02-05T09:38:36.439846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(25)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:36.441769Z","iopub.execute_input":"2022-02-05T09:38:36.442571Z","iopub.status.idle":"2022-02-05T09:38:40.045066Z","shell.execute_reply.started":"2022-02-05T09:38:36.442525Z","shell.execute_reply":"2022-02-05T09:38:40.044205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(26)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:40.046341Z","iopub.execute_input":"2022-02-05T09:38:40.046634Z","iopub.status.idle":"2022-02-05T09:38:45.924058Z","shell.execute_reply.started":"2022-02-05T09:38:40.046600Z","shell.execute_reply":"2022-02-05T09:38:45.923425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(27)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:45.924931Z","iopub.execute_input":"2022-02-05T09:38:45.925627Z","iopub.status.idle":"2022-02-05T09:38:47.057585Z","shell.execute_reply.started":"2022-02-05T09:38:45.925593Z","shell.execute_reply":"2022-02-05T09:38:47.056939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(28)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:47.058501Z","iopub.execute_input":"2022-02-05T09:38:47.059008Z","iopub.status.idle":"2022-02-05T09:38:48.846029Z","shell.execute_reply.started":"2022-02-05T09:38:47.058942Z","shell.execute_reply":"2022-02-05T09:38:48.845266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_sample_image(29)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T09:38:48.847617Z","iopub.execute_input":"2022-02-05T09:38:48.848150Z","iopub.status.idle":"2022-02-05T09:38:54.485503Z","shell.execute_reply.started":"2022-02-05T09:38:48.848110Z","shell.execute_reply":"2022-02-05T09:38:54.484832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"もし役に立ったと思ったら、upvoteしていただけると励みになります！","metadata":{}}]}