Skip to content

KMeans: precompute_distances differences accros machines #7193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tomek-kuchta opened this issue Aug 16, 2016 · 7 comments
Closed

KMeans: precompute_distances differences accros machines #7193

tomek-kuchta opened this issue Aug 16, 2016 · 7 comments

Comments

@tomek-kuchta
Copy link

Description

When using KMeans, setting precomputer_distances to True gives different cluster assignments for the same data set on different machines.

Steps/Code to Reproduce

Example:

kmeans = KMeans(init="k-means++", precompute_distances = True, n_clusters = num_clusters, random_state=get_prime(), n_jobs=-2)

get_prime() returns a prime number in a deterministic way (it's got an array of primes that it iterates over)

Expected Results

The same cluster assignment on different machines.

Actual Results

Different cluster assignments on different machines. I realized that by comparing the silhouette score. It's worth noting that if I repeat the clustering on the same machine the results are identical (clustering is the same).

Versions

Machine A:

import platform; print(platform.platform())
Linux-3.13.0-91-generic-x86_64-with-Ubuntu-14.04-trusty
import sys; print("Python", sys.version)
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4]
import numpy; print("NumPy", numpy.version)
NumPy 1.8.2
import scipy; print("SciPy", scipy.version)
SciPy 0.13.3
import sklearn; print("Scikit-Learn", sklearn.version)
Scikit-Learn 0.17.1

Machine B:

import platform; print(platform.platform())
Linux-3.16.0-34-generic-x86_64-with-Ubuntu-14.04-trusty
import sys; print("Python", sys.version)
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4]
import numpy; print("NumPy", numpy.version)
NumPy 1.10.4
import scipy; print("SciPy", scipy.version)
SciPy 0.13.3
import sklearn; print("Scikit-Learn", sklearn.version)
Scikit-Learn 0.17.1

The NumPy version and the kernel version differs.

@jnothman
Copy link
Member

Could you give an example of a random state that yields different results on different machines? I assume you find that the results are replicable on a single machine.

@tomek-kuchta
Copy link
Author

When I re-run KMeans on Machine A and Machine B they are repeatable. The problem is that there is the difference across machines. I will try to come up with a sample data set to illustrate that.

@jnothman
Copy link
Member

Thanks. Yes, the lack of dataset is a problem. First: could you please check whether you can replicate the difference by installing numpy 1.8.2 on Machine B?

@tomek-kuchta
Copy link
Author

tomek-kuchta commented Aug 16, 2016

I did something slightly different. I checked on Machine A and B whether clustering is different when precompute_distances is True and False and it seems to be different on both versions of numpy (and also across versions when it's True, as reported in the original issue). I will try to prepare an example to illustrate it, but unfortunately this might take some time.

@tomek-kuchta
Copy link
Author

OK, I've created an example script here:
https://gist.github.com/tomek-kuchta/cdff8dde0929c570b3c81f369318da5f

To reproduce the problem, it needs to be run twice, once with precompute_distances set to True and once set to False. The output is logged to test.log, so it's the best to store it under some temporary file after the first run and use a diff tool to compare. One of the silhouette scores differs, which is a result of a different clustering.

Hope that helps.

@nabilEM
Copy link

nabilEM commented Jun 26, 2018

Which distances k-means computes if set precompute_distances to True?

@jeremiedbb
Copy link
Member

precompute_distances is deprecated and unused anymore (#11950). Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants