<h4 dir="auto">Description</h4>

<p dir="auto">the result of kmeans.labels_ when n_jobs=1 is different from n_jobs &gt; 1</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>

<p dir="auto">from sklearn.cluster import KMeans<br>
import numpy as np<br>
X = np.array([[1, 2], [1, 4], [1, 0],<br>
[4, 2], [4, 4], [4, 0]])<br>
kmeans = KMeans(n_clusters=2, random_state=0, n_jobs=1).fit(X)<br>
kmeans.labels_</p>
<blockquote>
<p dir="auto">array([0, 0, 0, 1, 1, 1], dtype=int32)</p>
</blockquote>
<p dir="auto">kmeans = KMeans(n_clusters=2, random_state=0, n_jobs=2).fit(X)<br>
kmeans.labels_</p>
<blockquote>
<p dir="auto">array([1, 0, 1, 0, 0, 1], dtype=int32)</p>
</blockquote>
<h4 dir="auto">Expected Results</h4>

<p dir="auto">array([0, 0, 0, 1, 1, 1], dtype=int32)</p>
<h4 dir="auto">Actual Results</h4>

<p dir="auto">array([1, 0, 1, 0, 0, 1], dtype=int32)</p>
<h4 dir="auto">Versions</h4>

<blockquote>
<blockquote>
<blockquote>
<p dir="auto">import platform; print(platform.platform())<br>
Darwin-16.6.0-x86_64-i386-64bit<br>
import sys; print("Python", sys.version)<br>
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)<br>
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]<br>
import numpy; print("NumPy", numpy.<strong>version</strong>)<br>
NumPy 1.13.0<br>
import scipy; print("SciPy", scipy.<strong>version</strong>)<br>
SciPy 0.19.1<br>
import sklearn; print("Scikit-Learn", sklearn.<strong>version</strong>)<br>
Scikit-Learn 0.19.dev0</p>
</blockquote>
</blockquote>
</blockquote>

<p dir="auto">I found the issues maybe because the radom_state for n_job= 1 is different from n_jobs &gt; 1.<br>
I have a proposal to solve this if you think this issue needs to be fixed.<br>
Thank you.</p>
<h4 dir="auto">Description</h4>

<p dir="auto">I noticed that <code class="notranslate">cluster.KMeans</code> gives a slightly different result depending on if <code class="notranslate">n_jobs=1</code> or <code class="notranslate">n_jobs&gt;1</code>.</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>

<p dir="auto">Below is the code I used to run the same <code class="notranslate">KMeans</code> clustering on a varying number of jobs.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

# Generate some data
X, y = make_blobs(n_samples=10000, centers=10, n_features=2, random_state=2)

# Run KMeans with various n_jobs values
for n_jobs in range(1, 5):
    kmeans = KMeans(n_clusters=10, random_state=2, n_jobs=n_jobs)
    kmeans.fit(X)
    print(f'(n_jobs={n_jobs}) kmeans.inertia_ = {kmeans.inertia_}')"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">cluster</span> <span class="pl-k">import</span> <span class="pl-v">KMeans</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">datasets</span> <span class="pl-k">import</span> <span class="pl-s1">make_blobs</span>

<span class="pl-c"># Generate some data</span>
<span class="pl-v">X</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-en">make_blobs</span>(<span class="pl-s1">n_samples</span><span class="pl-c1">=</span><span class="pl-c1">10000</span>, <span class="pl-s1">centers</span><span class="pl-c1">=</span><span class="pl-c1">10</span>, <span class="pl-s1">n_features</span><span class="pl-c1">=</span><span class="pl-c1">2</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">2</span>)

<span class="pl-c"># Run KMeans with various n_jobs values</span>
<span class="pl-k">for</span> <span class="pl-s1">n_jobs</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">1</span>, <span class="pl-c1">5</span>):
    <span class="pl-s1">kmeans</span> <span class="pl-c1">=</span> <span class="pl-v">KMeans</span>(<span class="pl-s1">n_clusters</span><span class="pl-c1">=</span><span class="pl-c1">10</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">2</span>, <span class="pl-s1">n_jobs</span><span class="pl-c1">=</span><span class="pl-s1">n_jobs</span>)
    <span class="pl-s1">kmeans</span>.<span class="pl-en">fit</span>(<span class="pl-v">X</span>)
    <span class="pl-en">print</span>(<span class="pl-s">f'(n_jobs=<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">n_jobs</span><span class="pl-kos">}</span></span>) kmeans.inertia_ = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">kmeans</span>.<span class="pl-s1">inertia_</span><span class="pl-kos">}</span></span>'</span>)</pre></div>
<h4 dir="auto">Expected Results</h4>

<p dir="auto">Should expect the the clustering result (e.g. the inertia) to be the same regardless of how many jobs are run in parallel.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(n_jobs=1) kmeans.inertia_ = 17815.060435554242
(n_jobs=2) kmeans.inertia_ = 17815.060435554242
(n_jobs=3) kmeans.inertia_ = 17815.060435554242
(n_jobs=4) kmeans.inertia_ = 17815.060435554242"><pre class="notranslate"><code class="notranslate">(n_jobs=1) kmeans.inertia_ = 17815.060435554242
(n_jobs=2) kmeans.inertia_ = 17815.060435554242
(n_jobs=3) kmeans.inertia_ = 17815.060435554242
(n_jobs=4) kmeans.inertia_ = 17815.060435554242
</code></pre></div>
<h4 dir="auto">Actual Results</h4>

<p dir="auto">The <code class="notranslate">n_jobs=1</code> case has a (slightly) different inertia than the parallel cases.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(n_jobs=1) kmeans.inertia_ = 17815.004991244623
(n_jobs=2) kmeans.inertia_ = 17815.060435554242
(n_jobs=3) kmeans.inertia_ = 17815.060435554242
(n_jobs=4) kmeans.inertia_ = 17815.060435554242"><pre class="notranslate"><code class="notranslate">(n_jobs=1) kmeans.inertia_ = 17815.004991244623
(n_jobs=2) kmeans.inertia_ = 17815.060435554242
(n_jobs=3) kmeans.inertia_ = 17815.060435554242
(n_jobs=4) kmeans.inertia_ = 17815.060435554242
</code></pre></div>
<h4 dir="auto">Versions</h4>

<p dir="auto">Darwin-16.7.0-x86_64-i386-64bit<br>
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)<br>
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]<br>
NumPy 1.13.1<br>
SciPy 0.19.1<br>
Scikit-Learn 0.20.dev0</p>