-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKmean_algo.py
More file actions
55 lines (45 loc) · 1.72 KB
/
Kmean_algo.py
File metadata and controls
55 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import geopandas as gpd
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from shapely.geometry import Point
import folium
outbreaks = gpd.read_file("export.geojson")
districts = gpd.read_file("gadm41_IND_2.json")
outbreaks = outbreaks.to_crs(epsg=4326)
outbreak_coords = outbreaks.geometry.apply(lambda p: (p.y, p.x)).tolist()
coords_df = pd.DataFrame(outbreak_coords, columns=["lat", "lon"])
kmeans = KMeans(n_clusters=5, random_state=42)
kmeans.fit(coords_df)
outbreaks["cluster"] = kmeans.labels_
m = folium.Map(location=[coords_df["lat"].mean(), coords_df["lon"].mean()], zoom_start=5)
folium.GeoJson(districts, name="Districts").add_to(m)
colors = ['red', 'blue', 'green', 'purple', 'orange', 'pink', 'cadetblue', 'darkred', 'darkblue', 'darkgreen']
for i, row in outbreaks.iterrows():
cluster_id = row["cluster"]
lat, lon = row.geometry.y, row.geometry.x
color = colors[cluster_id % len(colors)]
folium.CircleMarker(
location=[lat, lon],
radius=5,
color=color,
fill=True,
fill_color=color,
fill_opacity=0.8,
popup=f"Cluster {cluster_id}"
).add_to(m)
def compute_centroid(group):
return pd.Series({
'lat': group.geometry.y.mean(),
'lon': group.geometry.x.mean()
})
valid_clusters = outbreaks.groupby("cluster")
deployment_locations = valid_clusters.apply(compute_centroid)
for cluster_id, row in deployment_locations.iterrows():
folium.Marker(
location=[row['lat'], row['lon']],
icon=folium.Icon(color='darkgreen', icon='plus-square', prefix='fa'),
popup=f"Health Unit for Cluster {cluster_id}"
).add_to(m)
m.save("kmeans_clusters_map.html")
print("Map saved as kmeans_clusters_map.html")