{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":29955,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# packages\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport folium","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-23T18:58:08.596352Z","iopub.execute_input":"2024-04-23T18:58:08.597039Z","iopub.status.idle":"2024-04-23T18:58:08.602196Z","shell.execute_reply.started":"2024-04-23T18:58:08.596991Z","shell.execute_reply":"2024-04-23T18:58:08.601331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# file overview\n!ls -l '../input/birdclef-2024/'","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:08.603868Z","iopub.execute_input":"2024-04-23T18:58:08.604439Z","iopub.status.idle":"2024-04-23T18:58:09.723209Z","shell.execute_reply.started":"2024-04-23T18:58:08.604400Z","shell.execute_reply":"2024-04-23T18:58:09.721817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read metedata file\ndf = pd.read_csv('../input/birdclef-2024/train_metadata.csv')","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-04-23T18:58:09.725252Z","iopub.execute_input":"2024-04-23T18:58:09.725658Z","iopub.status.idle":"2024-04-23T18:58:09.853241Z","shell.execute_reply.started":"2024-04-23T18:58:09.725615Z","shell.execute_reply":"2024-04-23T18:58:09.852098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# First glance","metadata":{}},{"cell_type":"code","source":"# preview of data\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:09.857481Z","iopub.execute_input":"2024-04-23T18:58:09.858034Z","iopub.status.idle":"2024-04-23T18:58:09.883392Z","shell.execute_reply.started":"2024-04-23T18:58:09.857963Z","shell.execute_reply":"2024-04-23T18:58:09.881999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# structure details\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:09.886844Z","iopub.execute_input":"2024-04-23T18:58:09.887416Z","iopub.status.idle":"2024-04-23T18:58:09.927572Z","shell.execute_reply.started":"2024-04-23T18:58:09.887364Z","shell.execute_reply":"2024-04-23T18:58:09.926263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### A few lat/lon values are missing.","metadata":{}},{"cell_type":"code","source":"# first simple plot of locations\nplt.figure(figsize=(12,6))\nsns.scatterplot(x='longitude', y='latitude', \n                data=df,\n                color='darkblue')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:09.929872Z","iopub.execute_input":"2024-04-23T18:58:09.930374Z","iopub.status.idle":"2024-04-23T18:58:10.215538Z","shell.execute_reply.started":"2024-04-23T18:58:09.930333Z","shell.execute_reply":"2024-04-23T18:58:10.214273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# eval frequencies\nbird_freq = df.primary_label.value_counts()\nbird_freq","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:10.217246Z","iopub.execute_input":"2024-04-23T18:58:10.217639Z","iopub.status.idle":"2024-04-23T18:58:10.235883Z","shell.execute_reply.started":"2024-04-23T18:58:10.217599Z","shell.execute_reply":"2024-04-23T18:58:10.234436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# secondary labels\ndf.secondary_labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:10.237620Z","iopub.execute_input":"2024-04-23T18:58:10.238032Z","iopub.status.idle":"2024-04-23T18:58:10.259525Z","shell.execute_reply.started":"2024-04-23T18:58:10.237964Z","shell.execute_reply":"2024-04-23T18:58:10.258128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ratings\nplt.figure(figsize=(10,4))\ndf.rating.value_counts().sort_index().plot(kind='bar', color='darkblue')\nplt.title('Ratings')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:10.261254Z","iopub.execute_input":"2024-04-23T18:58:10.261678Z","iopub.status.idle":"2024-04-23T18:58:10.480045Z","shell.execute_reply.started":"2024-04-23T18:58:10.261638Z","shell.execute_reply":"2024-04-23T18:58:10.478806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select first 10 categories and plot in color\ndf_select = df[df.primary_label.isin(bird_freq[0:9+1].index)]\nplt.figure(figsize=(12,6))\nsns.scatterplot(x='longitude', y='latitude', hue='primary_label', data=df_select, palette='colorblind')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) # move legend out of the plot area\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:10.481761Z","iopub.execute_input":"2024-04-23T18:58:10.482154Z","iopub.status.idle":"2024-04-23T18:58:10.932961Z","shell.execute_reply.started":"2024-04-23T18:58:10.482117Z","shell.execute_reply":"2024-04-23T18:58:10.931864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select next 10 categories and plot in color\ndf_select = df[df.primary_label.isin(bird_freq[10:19+1].index)]\nplt.figure(figsize=(12,6))\nsns.scatterplot(x='longitude', y='latitude', hue='primary_label', data=df_select, palette='colorblind')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:10.934562Z","iopub.execute_input":"2024-04-23T18:58:10.934927Z","iopub.status.idle":"2024-04-23T18:58:11.365247Z","shell.execute_reply.started":"2024-04-23T18:58:10.934891Z","shell.execute_reply":"2024-04-23T18:58:11.363850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select next 10 categories and plot in color\ndf_select = df[df.primary_label.isin(bird_freq[20:29+1].index)]\nplt.figure(figsize=(12,6))\nsns.scatterplot(x='longitude', y='latitude', hue='primary_label', data=df_select, palette='colorblind')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:11.367193Z","iopub.execute_input":"2024-04-23T18:58:11.367700Z","iopub.status.idle":"2024-04-23T18:58:11.777919Z","shell.execute_reply.started":"2024-04-23T18:58:11.367648Z","shell.execute_reply":"2024-04-23T18:58:11.776785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select next 10 categories and plot in color\ndf_select = df[df.primary_label.isin(bird_freq[30:39+1].index)]\nplt.figure(figsize=(12,6))\nsns.scatterplot(x='longitude', y='latitude', hue='primary_label', data=df_select, palette='colorblind')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:58:11.779369Z","iopub.execute_input":"2024-04-23T18:58:11.779737Z","iopub.status.idle":"2024-04-23T18:58:12.153516Z","shell.execute_reply.started":"2024-04-23T18:58:11.779704Z","shell.execute_reply":"2024-04-23T18:58:12.152478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Look at only one bird category and provide interactive map","metadata":{}},{"cell_type":"code","source":"my_bird = 'asbfly'\ndf_example = df[df.primary_label.isin([my_bird])]\ndf_example.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:59:09.991382Z","iopub.execute_input":"2024-04-23T18:59:09.991828Z","iopub.status.idle":"2024-04-23T18:59:10.001972Z","shell.execute_reply.started":"2024-04-23T18:59:09.991785Z","shell.execute_reply":"2024-04-23T18:59:10.000955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for missing coordinates\nprint('Missing latitudes:', df_example.latitude.isna().sum())\nprint('Missing longitudes:', df_example.longitude.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:59:10.877221Z","iopub.execute_input":"2024-04-23T18:59:10.877725Z","iopub.status.idle":"2024-04-23T18:59:10.887802Z","shell.execute_reply.started":"2024-04-23T18:59:10.877680Z","shell.execute_reply":"2024-04-23T18:59:10.886293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove rows with missings\ndf_example = df_example.dropna(axis=0, subset=['latitude','longitude'])\ndf_example.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:59:12.660696Z","iopub.execute_input":"2024-04-23T18:59:12.661159Z","iopub.status.idle":"2024-04-23T18:59:12.672545Z","shell.execute_reply.started":"2024-04-23T18:59:12.661116Z","shell.execute_reply":"2024-04-23T18:59:12.671329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# interactive map\nzoom_factor = 1.9\nmy_map_1 = folium.Map(location=[0,0], zoom_start=zoom_factor)\n\nfor i in range(0,df_example.shape[0]):\n    folium.Circle(\n        location=[df_example.iloc[i]['latitude'], df_example.iloc[i]['longitude']],\n        radius=np.sqrt(df_example.iloc[i]['rating'])*25000,\n        color='blue',\n        weight=1,\n        popup='label: ' + df_example.iloc[i]['primary_label'] + '<br>' +\n              'sec_labels: ' + df_example.iloc[i]['secondary_labels'] + '<br>' +\n              'type: ' + df_example.iloc[i]['type'] + '<br>' +\n              'URL: ' + df_example.iloc[i]['url'],\n        fill=True,\n        fill_color='blue').add_to(my_map_1)\n\nmy_map_1 # display","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:59:14.414321Z","iopub.execute_input":"2024-04-23T18:59:14.415245Z","iopub.status.idle":"2024-04-23T18:59:14.798223Z","shell.execute_reply.started":"2024-04-23T18:59:14.415184Z","shell.execute_reply":"2024-04-23T18:59:14.797117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Add another one","metadata":{}},{"cell_type":"code","source":"my_bird = 'greegr'\ndf_example = df[df.primary_label.isin([my_bird])]\ndf_example = df_example.dropna(axis=0, subset=['latitude','longitude'])\n\n# interactive map\nzoom_factor = 1.9\nmy_map_2 = folium.Map(location=[0,0], zoom_start=zoom_factor)\n\nfor i in range(0,df_example.shape[0]):\n    folium.Circle(\n        location=[df_example.iloc[i]['latitude'], df_example.iloc[i]['longitude']],\n        radius=np.sqrt(df_example.iloc[i]['rating'])*25000,\n        color='red',\n        weight=1,\n        popup='label: ' + df_example.iloc[i]['primary_label'] + '<br>' +\n              'sec_labels: ' + df_example.iloc[i]['secondary_labels'] + '<br>' +\n              'type: ' + df_example.iloc[i]['type'] + '<br>' +\n              'URL: ' + df_example.iloc[i]['url'],\n        fill=True,\n        fill_color='red').add_to(my_map_1)\n\nmy_map_1 # display","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:59:29.786496Z","iopub.execute_input":"2024-04-23T18:59:29.787228Z","iopub.status.idle":"2024-04-23T18:59:30.945441Z","shell.execute_reply.started":"2024-04-23T18:59:29.787175Z","shell.execute_reply":"2024-04-23T18:59:30.944322Z"},"trusted":true},"execution_count":null,"outputs":[]}]}