I would like to draw boundary lines of multiple groups in a two dimensional plane. I know I can draw a contour line to divide only two groups using SVM, but not for multiple groups.
In the sample code below, there are 500 points in 5 groups. But in my actual project, I have millions of points in thousands of groups.
# 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