KMeans with alternative distance formula (Coding issue)

Hello everyone, I am learning python from an edX course so my coding knowledge isn’t the best currently.

I am trying to write a code for KMeans with an altered distance formula. I found multiple resources online of people writing KMeans from scratch without sklearns and used their code as a template. For a project I am working on, I am trying normalizing the data to a unit sphere and then compute the thetas between the data points and the clusters. Using the euclidean distance formula, I did not run into any issues. However, with the new distance formula, I am starting to get attribute errors.

As I am new to the forums, I am not sure if this is the place to ask this question. If anyone could provide some insight or knowledge I would greatly appreciate it. Thank you.

This is the right place to ask. However, it’s hard to diagnose the problem like this. There are several things you can do to help:

  • Try to isolate a proper self-contained example of the problem - something that’s directly runnable and shows the problem right away.

  • Show the specific errors you’re getting - i.e. a complete stack trace, that corresponds to such an example.

  • Provide the code as properly formatted text, rather than an image - so that we can copy and paste it to test it out, and copy and paste parts to refer to them for discussion. Please read the pinned thread to understand how.

  • Give more context for the code, and relate it more directly to your description of the problem. For example, I can’t understand what part of your code is intended to correspond to “the euclidean distance formula” or “the new distance formula”, or whether they’re actually shown. I also don’t understand what “altered distance formula” you want to use, so I can’t possibly assess whether it’s mathematically sound to attempt k-means analysis with that formula. (Did you mean that the theta you compute after normalizing to a unit sphere, should be used as the “distance” between the points for clustering purposes? It would help to be a bit more explicit about this.)