0
$\begingroup$

I've coded a small clustering algorithm for time signals using kmeans, which works ok (gives acceptable results).

However, kmeans uses the sum of squared differences. I would like to be able to input instead my own measure of difference, but there doesn't seem to be a way provided by the library to do that.

What would be the easiest way to achieve this? Any other python library which may provide me some way to input instead my own function to define the distance? Or I guess I could instead re-implement the algorithm myself, but I'd rather keep the sci-kit one (since they provide functionalities I want to use such as parallel processing).

$\endgroup$
2
$\begingroup$

K-means cannot optimize arbitrary measures.

The mean optimizes squared errors. It does not optimize, e.g., Euclidean distances, Manhattan distances, etc. It won't crash, but the solution will not be optimal (not even locally) because the centers are not well placed.

So it makes little sense to add support for another "inertia" inside k-means (nor other distances), as it can't optimize that.

If you want to optimize other distances, there is for example the PAM algorithm, and k-medians (for Manhattan).

There is nothing wrong with computing such a quality afterwards though, but then it belongs into the evaluation package, not in the KMeans class.

$\endgroup$
  • $\begingroup$ I think you are right. I need to reformulate my approach - actually the more I re-read your answer the closer I get to a facepalm. And no, I don't want it as a quality afterward. The issue being that straight-up kmean-ing on a per-point basis of time signals gets iffy as they can be slightly off on the time axis, etc... and similiarity may be reduced for 2 signals which actually should cluster toghether. $\endgroup$ – Francis Vachon Mar 3 '19 at 17:24
  • $\begingroup$ Okay - but then if I have a distance matrix, would it be possible for me to kmean that instead? I've started another question [another question] [datascience.stackexchange.com/questions/46590/… $\endgroup$ – Francis Vachon Mar 3 '19 at 17:49
  • $\begingroup$ No, that creates subtle bias. If you have a distance matrix, use PAM instead. $\endgroup$ – Has QUIT--Anony-Mousse Mar 3 '19 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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