-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotspot_analysis.py
65 lines (51 loc) · 1.83 KB
/
hotspot_analysis.py
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
56
57
58
59
60
61
62
63
64
65
import folium
import pandas as pd
from folium.plugins import HeatMap
from sklearn.cluster import DBSCAN, KMeans
from sklearn.preprocessing import StandardScaler
def get_clusters(df, algorithm="dbscan"):
scaler = StandardScaler()
data_scaled = scaler.fit_transform(df[["LATITUDE", "LONGITUDE"]])
match algorithm:
case "dbscan":
algorithm = DBSCAN(eps=0.1, min_samples=10)
case "kmeans":
algorithm = KMeans(n_clusters=5, random_state=42)
df["Cluster"] = algorithm.fit_predict(data_scaled)
# Filtrar apenas clusters válidos (remover ruído, que geralmente é marcado como -1)
return df[df["Cluster"] != -1]
def gen_hotspot_map(df, coords, file_name) -> None:
fmap = folium.Map(location=coords, zoom_start=11)
for _, row in df.iterrows():
folium.CircleMarker(
location=[row["LATITUDE"], row["LONGITUDE"]],
radius=3,
color="red",
fill=True,
fill_color="red",
fill_opacity=0.6,
).add_to(fmap)
fmap.save(file_name)
print(f" Mapa Hotspot salvo como '{file_name}'.")
def gen_hotspot_heatmap(df, coords, file_name) -> None:
fmap = folium.Map(location=coords, zoom_start=11)
heat_data = [[row["LATITUDE"], row["LONGITUDE"]] for _, row in df.iterrows()]
HeatMap(heat_data).add_to(fmap)
fmap.save(file_name)
print(f" Mapa de calor Hotspot salvo como '{file_name}'.")
def hotspot_analysis(xlsx_file, save_to, algorithm="kmeans") -> None:
chicago_coords = [41.8781, -87.6298]
df = pd.read_excel(xlsx_file)
df = df[["LATITUDE", "LONGITUDE"]].dropna()
file_name = f"output/mapas/{save_to}/hotspot_"
clusters = get_clusters(df, algorithm=algorithm)
gen_hotspot_map(
df=clusters,
coords=chicago_coords,
file_name=file_name + "map.html",
)
gen_hotspot_heatmap(
df=clusters,
coords=chicago_coords,
file_name=file_name + "heatmap.html",
)