I'm using the k-means algorithm from the scikit-learn library, and the values I want to cluster are in a pandas dataframe with 3 columns: ID value_1 and value_2.

I want to cluster the information using value_1 and value_2, but I also want to keep the ID associated with it (so I can create a list of IDs in each cluster).

What's the best way of doing this? Currently it clusters using the ID number as well and that's not the intention.

  • 3
    $\begingroup$ I think you'd have better luck asking this on the StackOverflow main site. This looks to be a programming question, rather than an AI question. $\endgroup$ – John Doucette Aug 14 '18 at 15:11
  • 1
    $\begingroup$ @JohnDoucette yeah I will post it there too after the 90 minute time out is up.. I wasn't sure $\endgroup$ – Jessica Chambers Aug 14 '18 at 15:12
  • 1
    $\begingroup$ no worries. (there's a little bit of overlap and we're still trying to work out the SE:AI parameters:) $\endgroup$ – DukeZhou Aug 14 '18 at 19:00
  • 1
    $\begingroup$ I noticed you did receive and accept and answer on Overflow, so closing this question here. (Glad you found the answer you were looking for! Look forward to seeing more robotics related algorithm questions on SE:AI in general.) $\endgroup$ – DukeZhou Aug 22 '18 at 17:29

Assuming your three columns data (data label included) is stored in array "a" (numpy).

1) extract data values only, removing the data label, into new array b:

b=a[:,np.array([1, 2])]

2) classify "b" using KMeans/kmeans.predict/... . Result is an array of class labels, it keeps original order of samples.


3) rejoin original data and class labels


Not the answer you're looking for? Browse other questions tagged or ask your own question.