11

I've been using scipy's k-means for quite some time now, and I'm pretty happy about the way it works in terms of usability and efficiency. However, now I want to explore different k-means variants, more specifically, I'd like to apply spherical k-means in some of my problems.

Do you know any good Python implementation (i.e. similar to scipy's k-means) of spherical k-means? If not, how hard would it be to modify scipy's source code to adapt its k-means algorithm to be spherical?

Thank you.

3 Answers 3

15

In spherical k-means, you aim to guarantee that the centers are on the sphere, so you could adjust the algorithm to use the cosine distance, and should additionally normalize the centroids of the final result.

When using the Euclidean distance, I prefer to think of the algorithm as projecting the cluster centers onto the unit sphere in each iteration, i.e., the centers should be normalized after each maximization step.

Indeed, when the centers and data points are both normalized, there is a 1-to-1 relationship between the cosine distance and Euclidean distance

|a - b|_2 = 2 * (1 - cos(a,b))

The package jasonlaska/spherecluster modifies scikit-learns's k-means into spherical k-means and also provides another sphere clustering algorithm.

3
  • 1
    I think this answer should be accepted, @oriol-nieto
    – Lutz Büch
    Mar 27, 2019 at 16:19
  • Shouldn't there be a sqrt in the expression? |a - b|_2 = sqrt(2 * (1 - cos(a, b))) This doesn't change the 1-to-1 relationship pointed out. Mar 29, 2021 at 16:55
  • To use jasonlaska/spherecluster as mentioned in the answer, you need a legacy sklearn version == 0.20
    – Jamie
    Jun 28, 2022 at 15:50
3

It looks like the salient feature in the spherical k-means is the use of the cosine distance, instead of the standard Euclidean metric. With that being said, there is a nice pure numpy/scipy adaptation here on SO in another answer:

Is it possible to specify your own distance function using Scikits.Learn K-Means Clustering?

If that doesn't meet what you are looking for you might want to try sklearn.cluster.

2
  • 1
    I might be wrong, but the way I understand spherical k-means is that the learned centroids are located in the surface of the hypersphere defined by the standard deviation of your data. You should be able to learn these centroids (and then cluster new data) with your preferred distance measure, not only cosine. In any case, thank you for your answer. This link is actually really interesting. Oct 7, 2013 at 18:25
  • @urinieto You may be correct, I have no experience with the spherical K-means. I only assumed it came from a cosine metric based by skimming off the linked paper OP posted. I too, would be interested in a answer that corrects this.
    – Hooked
    Oct 7, 2013 at 18:28
1

Here's how you do it if you have polar coordinates on a 3D sphere, such as (lat, lon) pairs:

  1. If your coordinates are (lat, lon) coordinates measured in degrees you can write a function that converts these points into cartesian coordinates, like:

    def cartesian_encoder(coord, r_E=6371):
        """Convert lat/lon to cartesian points on Earth's surface.
    
        Input
        -----
            coord : numpy 2darray (size=(N, 2))
            r_E : radius of Earth
    
        Output
        ------
            out : numpy 2darray (size=(N, 3))
        """
        def _to_rad(deg):
            return deg * np.pi / 180.
    
        theta = _to_rad(coord[:, 0])  # lat [radians]
        phi = _to_rad(coord[:, 1])    # lon [radians]
    
        x = r_E * np.cos(phi) * np.cos(theta)
        y = r_E * np.sin(phi) * np.cos(theta)
        z = r_E * np.sin(theta)
    
        return np.concatenate([x.reshape(-1, 1), y.reshape(-1, 1), z.reshape(-1, 1)], axis=1)
    

    If your coordinates are already in radians, just remove the first 5 lines in that function.

  2. Install the spherecluster package with pip. If your polar data given as rows of (lat, lon) pairs is called X and you want to find 10 cluster in it, the final code for KMeans-clustering spherically will be:

    import numpy as np
    import spherecluster
    
    X_cart = cartesian_encoder(X)
    kmeans_labels = SphericalKMeans(10).fit_predict(X_cart)
    
1
  • I should add, though, that using regular KMeans from sklearn probably will give you the same result in 99% of cases. So just transform to cartesian coordinates and cluster them.
    – Ulf Aslak
    Feb 13, 2018 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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