提供一个如何进行空间近似客户聚类的编码示例,适用于例如搜索多个重心时(即想定位多个仓库时。其逻辑和方法与任何一种基于距离的聚类问题相同。
我将k应用k-means聚类,根据客户的空间距离进行分组。
k-means聚类的算法有很好的解释,比如这篇文章:https://www.datanovia.com/en/lessons/k-means-clustering-in-r-algorith-and-practical-examples/。
首先我定义了一个包含随机经纬度坐标的数据框,代表随机分布的客户。
customer_df <- as.data.frame(matrix(nrow=1000,ncol=2))
colnames(customer_df) <- c("lat","long")
customer_df$lat <- runif(n=1000,min=-90,max=90)
customer_df$long <- runif(n=1000,min=-180,max=180)
这里你可以看到数据框架的头。
head(customer_df)
## lat long
## 1 -42.69660 58.067160
## 2 37.31715 179.655272
## 3 -28.68660 -3.025719
## 4 -76.15463 117.119388
## 5 -14.84898 -162.408406
## 6 54.19468 -128.476143
标准的k-means聚类算法选择k个随机初始点,并将这些点定义为聚类中心。然后算法根据最小距离将数据点分配给每个聚类中心。
在这种情况下,我们以后要使用聚类算法来解决设施定位问题,要考虑多个仓库的定位。因此,我认为选择彼此距离合理的聚类中心更为合适。为此我定义了一个函数,根据空间数据集的经度维度来选择定义的起始中心数量。
initial_centers <- function(customers,centers){
quantiles <- c()
for(i in 1:centers){
quantiles <- c(quantiles,i*as.integer(nrow(customers)/centers))
}
quantiles
}
现在我们可以结合R基础包中的kmeans函数,应用上面的函数。在这个例子中,我推导出四个基于接近性的客户群体。
cluster_obj <- kmeans(customer_df,centers=customer_df[initial_centers(customer_df,4),])
head(cluster_obj)
## $cluster
## [1] 1 4 2 1 3 3 3 1 2 1 3 2 2 4 3 4 4 4 3 4 3 2 3 4 2 3 2 3 1 4 4 4 1 1 4 2 2
## [38] 2 4 4 3 3 1 2 4 1 2 1 3 1 2 2 3 3 4 1 4 2 3 3 2 4 2 2 3 2 1 4 2 2 2 4 4 2
## [75] 4 3 3 4 1 1 1 3 3 2 1 1 3 3 4 4 3 1 2 4 3 1 3 2 2 2 3 2 3 4 4 2 3 3 1 3 1
## [112] 2 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 3 2 2 1 1 2 4 2 3 3 2 1 4 1 4 3 3 3 3 3 3 2 2 2 1 1 2 3 2 1 1 2 1 1 1 1 1
## [630] 2 4 2 1 1 3 1 4 2 4 2 2 1 4 1 2 2 3 1 1 3 1 1 3 4 3 2 4 1 1 1 2 1 1 1 2 3
## [667] 4 3 2 4 4 4 2 4 4 3 2 1 2 2 3 3 3 4 2 4 3 1 2 4 2 3 1 3 3 1 4 3 4 4 1 2 3
## [704] 3 4 4 2 2 1 2 2 1 3 4 1 2 2 3 4 4 2 3 1 2 4 3 1 2 2 2 1 4 1 3 1 4 2 2 1 1
## [741] 2 2 2 2 1 2 4 3 3 3 1 3 4 1 1 3 2 1 4 4 2 4 2 3 2 3 3 4 2 1 2 3 2 1 1 1 3
## [778] 2 3 4 3 2 2 4 1 4 4 2 2 1 1 2 3 1 2 2 2 1 4 3 3 3 1 2 1 3 2 4 2 3 4 1 4 3
## [815] 1 3 2 2 1 1 2 3 4 4 4 3 1 2 4 2 2 2 1 3 4 4 2 2 3 2 4 3 4 2 2 2 3 3 3 4 2
## [852] 3 3 3 1 1 3 1 1 2 2 1 3 3 4 3 3 3 2 2 1 3 2 3 1 4 3 2 4 1 4 3 3 2 3 4 4 1
## [889] 3 1 2 4 4 3 2 1 2 3 2 1 1 2 3 2 1 1 3 3 4 3 3 4 3 3 3 2 2 1 2 3 1 1 1 4 2
## [926] 3 4 2 4 1 4 3 4 4 1 3 2 2 1 2 2 4 2 4 1 4 1 2 1 2 4 3 4 2 4 4 4 2 4 1 2 3
## [963] 3 2 4 4 4 2 4 4 3 2 1 2 4 3 2 3 1 2 3 4 1 4 3 4 2 1 4 1 4 2 3 3 1 1 2 2 2
## [1000] 4
##
## $centers
## lat long
## 1 -44.672042 103.20907
## 2 9.621406 -22.15262
## 3 -4.487789 -127.84173
## 4 48.358322 110.24174
##
## $totss
## [1] 13417586
##
## $withinss
## [1] 557304.2 1006745.4 962130.0 492832.8
##
## $tot.withinss
## [1] 3019012
##
## $betweenss
## [1] 10398574
上面你可以看到kmeans函数返回的结果对象的头。下面我将kmeans对象所包含的聚类指数与客户数据框架结合起来,这样我们现在就有3列。这样我们就可以做gg图等。
result_df <- customer_df
result_df$group <- cluster_obj$cluster
head(result_df)
## lat long group
## 1 -42.69660 58.067160 1
## 2 37.31715 179.655272 4
## 3 -28.68660 -3.025719 2
## 4 -76.15463 117.119388 1
## 5 -14.84898 -162.408406 3
## 6 54.19468 -128.476143 3
我通过在ggplot(使用ggplot2 R包的散点图)中可视化结果来完成这篇文章。对于着色,我使用R中的viridis包。
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
ggplot(result_df) + geom_point(mapping = aes(x=lat,y=long,color=group)) +
xlim(-90,90) + ylim(-180,180) + scale_color_viridis(discrete = FALSE, option = "D") + scale_fill_viridis(discrete = FALSE)

让我们用20个仓库再跑一次测试。
cluster_obj <- kmeans(customer_df,centers=customer_df[initial_centers(customer_df,20),])
result_df$group <- cluster_obj$cluster
ggplot(result_df) + geom_point(mapping = aes(x=lat,y=long,color=group)) +
xlim(-90,90) + ylim(-180,180) + scale_color_viridis(discrete = FALSE, option = "D") + scale_fill_viridis(discrete = FALSE)


专业领域为优化和仿真的工业工程师(R,Python,SQL,VBA)
Leave a Reply