# tidymodels kmeans example

Regarding to this example:

how do we get x1 and x2 from real live data?

I dont understand your question, you want to know how to import data that you have ?
... In what form do you have it ?

Providing any matrix-input kmeans allows.

x1 and x2 is used to plot the first plot in section EXPLORATORY CLUSTERING. Unfortunately it's artificial/simulated data in the example. And it's not clear, to do the plots using any compatible data.

Artificial data set...

``````centers <- tibble(
cluster = factor(1:3),
num_points = c(100, 150, 50),  # number points in each cluster
x1 = c(5, 0, -3),              # x1 coordinate of cluster center
x2 = c(-1, 1, -2)              # x2 coordinate of cluster center
)

labelled_points <-
centers %>%
mutate(
x1 = map2(num_points, x1, rnorm),
x2 = map2(num_points, x2, rnorm)
) %>%
``````

As Iar as I understand I have to prepare labeled data points first.
That's a little bit counter-intuitive, since I already have the kmeans-analysis (input any matrix), but can't plot it. I guess the needed data is already prepared, but not mapped the right way to plot it.

Solution:

In order to follow the tut. with our own real live data, we have to augment it with coordinates... but I'm still not sure, if this is not already done by the analysis itself.

``````tagCoordinates<-function(df){
idx <- which(df>0, arr.ind=T)
idx<-as.data.frame(idx)
for (gg in 1:nrow(idx)){
myRefRow<-idx[gg,1]
myRefCol<-idx[gg,2]
idx[['myNo']][gg]<-as.numeric(df[myRefRow,myRefCol])
}
idx <- idx[order(idx[['row']],decreasing=F),]
rownames(idx)<-NULL
return(idx)
}
``````
``````type or paste code here
``````

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.