上下文如下:
我的任务是重新集群一家自动售货机公司的自动售货机路线(它们更像是每个服务人员负责的区域)。
我已经对每台自动售货机的 lon/lat 值进行了地理编码,并使用了基本的 k-means 聚类来找到最“数学合理”的聚类。
但是,我必须确保每个集群都满足以下条件:
到目前为止,我对 R 和 ChatGPT 的代码很陌生,以完成此任务。
我现在的代码如下:
# Run k-means clustering on vending machine locations
k <- 8 # Number of clusters to create
set.seed(123)
vm_clusters <- kmeans(vm_data[,c("lon", "lat")], centers = k, nstart = 25)
# Add cluster information to original data
vm_data$cluster <- as.factor(vm_clusters$cluster)
# Visualize clustering results on a map
vm_map <- get_googlemap(center = "Country", zoom = 11)
ggmap(vm_map) +
geom_point(data = vm_data, aes(x = lon, y = lat, color = cluster), size = 1, stroke = 1) +
scale_color_manual(values = c("red", "green", "blue", "orange", "purple", "brown", "grey", "black")) +
labs(title = "Vending Machine Clusters")
# View summary statistics of each cluster
cluster_summary <- vm_data %>%
group_by(cluster) %>%
summarise(num_vms = n(), total_sales = sum(SALES))
print(cluster_summary)
结果:
我最初尝试从重新划分区域的方法来看待问题,但看起来聚类是可行的方法。此外,我已经看过尝试 dbscans,但它不会产生集群(也许我没有很好地调整参数)。
我不确定如何添加上述约束。我感谢所有帮助/建议。