我想在二维平面中绘制多个组的边界线。我知道我可以绘制一条轮廓线,只使用SVM划分两组,但不能用于多组。
在下面的示例代码中,5组中有500个点。但在我的实际项目中,我在数千个小组中拥有数百万分。
# random 500 points
set.seed(1)
lenPoints = 500
dfPoints = data.frame(
"x" = runif(n = lenPoints, min = 1, max = 10),
"y" = runif(n = lenPoints, min = 1, max = 10),
"grp" = NA
)
# set the first 5 elements as groups 1,2,3,4,5
maxGroup = 5
dfPoints$grp[seq_len(maxGroup)] = seq_len(maxGroup)
dfOrigins = dfPoints[seq_len(maxGroup),]
# assign all the points in one of the 5 groups
for(ctDummy in seq_len(lenPoints - maxGroup) ){
(iNa = which(is.na(dfPoints$grp))[1])
vecTempDist = sqrt( (dfOrigins$x - dfPoints$x[iNa])^2 + (dfOrigins$y - dfPoints$y[iNa])^2 )
iMin = which.min(vecTempDist)
dfPoints$grp[iNa] = iMin
}
plot(dfPoints$x, dfPoints$y, col=dfPoints$grp)
# end