joshlk / k-means-constrained

K-Means clustering - constrained with minimum and maximum cluster size. Documentation: https://joshlk.github.io/k-means-constrained
https://github.com/joshlk/k-means-constrained
BSD 3-Clause "New" or "Revised" License
192 stars 43 forks source link

Wrong clustering with pre_computed adjacency matrix #35

Closed mosikh closed 1 year ago

mosikh commented 2 years ago

Hello, I used k-means-constrained library to cluster some points with a precomputed adjacency matrix. But I faced a wrong clustering. The project and further explanations are available in this link.

https://github.com/mosikh/k-means-constrained

I appreciate any help to improve the result.

Best regards, Mostafa

joshlk commented 2 years ago

Hi Mostafa,

Thank you for the example. I wouldn’t consider the provided example a minimum workable example. Stackoverflow have a good article on how to produce this. Importantly the example should contain the least amount of code and data to reproduce the result you want to present. Ideally you should be able to paste it as few lines of code and include only a few datapoints of data within a GitHub comment.

I also don’t understand the problem? Why is the desired output not what you expected and why should the algorithm not give this output?

I know the above is more difficult to produce but it really helps me understand the issue. Otherwise it’s very hard for me to debug it 🙂

Thanks, Josh

mosikh commented 2 years ago

Hi Josh, Thanks a lot for your answer.

You are right. My codes were long. Now, I changed the code in the repository and now we have 5 lines of cods and a CSV which is the adjacency matrix. You need just the python code (5 lines) and also the input CSV file to debug. Please check the repository once again.

As I explained in the readme file, the ESRI Shapefiles are there just in case that somebody needs to visualize. But they are not necessary to debug. So, you can skip them.

About your question: I have uploaded two pictures: clustering_output.jpeg which is the result of the algorithm and library. The other picture whose name is desired_output.jpeg is my favorite mood of clustering, but the algorithm doesn't provide such a result. And I really don't know what is the problem that the library makes mistake. I hope you can help me improve the result and the repository is ready to debug and sorry for the inconvenience.

Thanks, Mostafa

mosikh commented 1 year ago

Hi Josh, I think K-Means clustering generally is not the best method to cluster based on pre-computed matrix. It shows some inaccuracies even in the normal cases without constraint and without maximum cluster size limitation.

I believe AgglomerativeClustering is the best method to cluster based on pre-computed matrices. So, it is worth to try constraint methods on this clustering approach. I think the result will be much better than K-means.

Best regards, Mostafa