ConfusedUngaBunga
ConfusedUngaBunga

Reputation: 11

Grouping Set of Points to a Pre Defined Point

I'm looking to create a model that classifies a set of points that are near a pre-defined point.

For example, let's say I have points:

X Y
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
6 6
8 7
8 5
9 3
10 7

My goal is to identify which points are closest to predefined point (2,2) and ideally output which points those are.

I tried using KNN, but I could not figure out how to get the KNN model to train results near (2,2). Any guidance to how I may accomplish this would be awesome. :)

Plot of Points

df <- data.frame( x = c(1,1,1,2,2,2,3,3,3,6,8,8,9,10), y = c(1,2,3,1,2,3,1,2,3,6,7,5,3,7))
df

goal_point <- c(x=2,y=2)
goal_point

Upvotes: 1

Views: 34

Answers (1)

Jon Spring
Jon Spring

Reputation: 66425

You might approach this by calculating distance from goal as a feature.

df$dist = sqrt((df$x - goal_point["x"])^2 + 
               (df$y - goal_point["y"])^2)
df$clust = kmeans(df, 2)$cluster

library(ggplot2)
ggplot(df, aes(x, y, color = clust)) +
  geom_point()

enter image description here

In this case kmeans is using x, y, and distance from goal. You could also use just distance from goal by using df$clust = kmeans(df[,3], 2)$cluster, which would lead here to the same clustering.

Upvotes: 1

Related Questions