{"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\nimport pandas as pd\n#!pip install folium\nimport folium\nfrom branca.element import Figure\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\nimport seaborn as sns; sns.set()\nimport csv\n","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:06:48.484373Z","iopub.execute_input":"2022-04-07T06:06:48.484724Z","iopub.status.idle":"2022-04-07T06:06:50.273054Z","shell.execute_reply.started":"2022-04-07T06:06:48.484634Z","shell.execute_reply":"2022-04-07T06:06:50.272179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gps = '../input/iwildcam2022-fgvc9/metadata/metadata/gps_locations.json'\ngps_pd = pd.read_json(gps)\ndata = gps_pd.transpose()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:06:52.114011Z","iopub.execute_input":"2022-04-07T06:06:52.114324Z","iopub.status.idle":"2022-04-07T06:06:52.236330Z","shell.execute_reply.started":"2022-04-07T06:06:52.114274Z","shell.execute_reply":"2022-04-07T06:06:52.235569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ps_pd = data[['latitude','longitude']]\nlocationlist = ps_pd.values.tolist()\nps_pd.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:06:52.923243Z","iopub.execute_input":"2022-04-07T06:06:52.923548Z","iopub.status.idle":"2022-04-07T06:06:52.945228Z","shell.execute_reply.started":"2022-04-07T06:06:52.923517Z","shell.execute_reply":"2022-04-07T06:06:52.944201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Clustering for better Visualizing \nSince I was unable to visualize all the GPS coordinates with a single GPS anchor I have clustered the GPS coordinates.","metadata":{"execution":{"iopub.status.busy":"2022-04-03T04:09:17.793086Z","iopub.execute_input":"2022-04-03T04:09:17.793408Z","iopub.status.idle":"2022-04-03T04:09:17.797195Z","shell.execute_reply.started":"2022-04-03T04:09:17.793384Z","shell.execute_reply":"2022-04-03T04:09:17.796282Z"}}},{"cell_type":"code","source":"kmeans = KMeans(n_clusters = 6, init ='k-means++')\nkmeans.fit(ps_pd[ps_pd.columns[0:2]]) # Compute k-means clustering.\nps_pd['cluster_label'] = kmeans.fit_predict(ps_pd[ps_pd.columns[0:2]])\ncenters = kmeans.cluster_centers_ # Coordinates of cluster centers.\nlabels = kmeans.predict(ps_pd[ps_pd.columns[0:2]]) # Labels of each point\nps_pd.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:06:54.634019Z","iopub.execute_input":"2022-04-07T06:06:54.634433Z","iopub.status.idle":"2022-04-07T06:06:54.809638Z","shell.execute_reply.started":"2022-04-07T06:06:54.634396Z","shell.execute_reply":"2022-04-07T06:06:54.808599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"centers = kmeans.cluster_centers_\ncenters","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:06:55.244334Z","iopub.execute_input":"2022-04-07T06:06:55.244634Z","iopub.status.idle":"2022-04-07T06:06:55.250983Z","shell.execute_reply.started":"2022-04-07T06:06:55.244604Z","shell.execute_reply":"2022-04-07T06:06:55.250364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing the MAP using Folium Marker","metadata":{}},{"cell_type":"code","source":"map = folium.Map(width=1000, height=500,location=[ 0.38959009,  36.87179589], zoom_start=11)\nfor point in range(0, len(locationlist)):\n    folium.Marker(locationlist[point],popup=folium.Popup(max_width=450).add_child(\n        folium.Vega(data, width=450, height=250)\n    ),icon=folium.Icon(color=\"green\")).add_to(map)\nmap\n","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:06:56.533831Z","iopub.execute_input":"2022-04-07T06:06:56.534418Z","iopub.status.idle":"2022-04-07T06:07:00.037178Z","shell.execute_reply.started":"2022-04-07T06:06:56.534366Z","shell.execute_reply":"2022-04-07T06:07:00.035860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map.save(\"index.html\")","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:07:00.039035Z","iopub.execute_input":"2022-04-07T06:07:00.039954Z","iopub.status.idle":"2022-04-07T06:07:02.974172Z","shell.execute_reply.started":"2022-04-07T06:07:00.039903Z","shell.execute_reply":"2022-04-07T06:07:02.973220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cordinates_counts = ps_pd['cluster_label'].value_counts()\nprint(cordinates_counts)\nax = sns.countplot(x=ps_pd['cluster_label'])","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:07:02.975398Z","iopub.execute_input":"2022-04-07T06:07:02.975641Z","iopub.status.idle":"2022-04-07T06:07:03.238646Z","shell.execute_reply.started":"2022-04-07T06:07:02.975610Z","shell.execute_reply":"2022-04-07T06:07:03.237793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map0 = ps_pd[ps_pd['cluster_label']==0]\nmap1 = ps_pd[ps_pd['cluster_label']==1]\nmap2 = ps_pd[ps_pd['cluster_label']==2]\nmap3 = ps_pd[ps_pd['cluster_label']==3]\nmap4 = ps_pd[ps_pd['cluster_label']==4]\nmap5 = ps_pd[ps_pd['cluster_label']==5]\n","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:07:03.240612Z","iopub.execute_input":"2022-04-07T06:07:03.240845Z","iopub.status.idle":"2022-04-07T06:07:03.250735Z","shell.execute_reply.started":"2022-04-07T06:07:03.240813Z","shell.execute_reply":"2022-04-07T06:07:03.249806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map1.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:07:49.703804Z","iopub.execute_input":"2022-04-07T06:07:49.704110Z","iopub.status.idle":"2022-04-07T06:07:49.713885Z","shell.execute_reply.started":"2022-04-07T06:07:49.704076Z","shell.execute_reply":"2022-04-07T06:07:49.713029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map0_viz = map0[['latitude','longitude']].astype('float')\nmap0['locationlist'] = map0_viz.values.tolist()\nmap0['locationlist']","metadata":{"execution":{"iopub.status.busy":"2022-04-07T06:15:45.582838Z","iopub.execute_input":"2022-04-07T06:15:45.583397Z","iopub.status.idle":"2022-04-07T06:15:45.596792Z","shell.execute_reply.started":"2022-04-07T06:15:45.583357Z","shell.execute_reply":"2022-04-07T06:15:45.595735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}