<h4 dir="auto">Description</h4>
<p dir="auto">When there are duplicated input points to Kmeans resulting to number of unique points &lt; number of requested clusters, there is no error thrown. Instead, clustering continues to (seemingly) produce the number of clusters requested, but some of them are exactly the same, so the cluster labels produced for the input points do not go all the way to number of requested clusters.</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sklearn.cluster import KMeans
import numpy as np

# some input points here are identical, so that n_total=17, n_unique=9
x2d = np.array([(1086, 348), (1087, 347), (1190, 244), (1190, 244), (1086, 348), (1185, 249), (1193, 241), (1185, 249), (1087, 347), (1188, 247), (1187, 233), (26, 111), (26, 111), (26, 110), (26, 110), (26, 110), (26, 110)])
kmeans = KMeans(n_clusters=10) # n_clusters &gt; n_unique
c_labels = kmeans.fit_predict(x2d)
c_centers = kmeans.cluster_centers_"><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">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-c"># some input points here are identical, so that n_total=17, n_unique=9</span>
<span class="pl-s1">x2d</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([(<span class="pl-c1">1086</span>, <span class="pl-c1">348</span>), (<span class="pl-c1">1087</span>, <span class="pl-c1">347</span>), (<span class="pl-c1">1190</span>, <span class="pl-c1">244</span>), (<span class="pl-c1">1190</span>, <span class="pl-c1">244</span>), (<span class="pl-c1">1086</span>, <span class="pl-c1">348</span>), (<span class="pl-c1">1185</span>, <span class="pl-c1">249</span>), (<span class="pl-c1">1193</span>, <span class="pl-c1">241</span>), (<span class="pl-c1">1185</span>, <span class="pl-c1">249</span>), (<span class="pl-c1">1087</span>, <span class="pl-c1">347</span>), (<span class="pl-c1">1188</span>, <span class="pl-c1">247</span>), (<span class="pl-c1">1187</span>, <span class="pl-c1">233</span>), (<span class="pl-c1">26</span>, <span class="pl-c1">111</span>), (<span class="pl-c1">26</span>, <span class="pl-c1">111</span>), (<span class="pl-c1">26</span>, <span class="pl-c1">110</span>), (<span class="pl-c1">26</span>, <span class="pl-c1">110</span>), (<span class="pl-c1">26</span>, <span class="pl-c1">110</span>), (<span class="pl-c1">26</span>, <span class="pl-c1">110</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-c"># n_clusters &gt; n_unique</span>
<span class="pl-s1">c_labels</span> <span class="pl-c1">=</span> <span class="pl-s1">kmeans</span>.<span class="pl-en">fit_predict</span>(<span class="pl-s1">x2d</span>)
<span class="pl-s1">c_centers</span> <span class="pl-c1">=</span> <span class="pl-s1">kmeans</span>.<span class="pl-s1">cluster_centers_</span></pre></div>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">Either an error thrown, or the cluster labels produced should match the unique clusters only (i.e. no identical cluster centres)</p>
<h4 dir="auto">Actual Results</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; c_labels  # note there's no entry for cluster 9
array([7, 2, 6, 6, 7, 5, 4, 5, 2, 1, 3, 8, 8, 0, 0, 0, 0], dtype=int32)
&gt;&gt;&gt; c_centers # two of these 10 clusters have identical centers, so only 9 of them are unique
array([[   26.,   110.],
       [ 1188.,   247.],
       [ 1087.,   347.],
       [ 1187.,   233.],
       [ 1193.,   241.],
       [ 1185.,   249.],
       [ 1190.,   244.],
       [ 1086.,   348.],
       [   26.,   111.],
       [   26.,   110.]]) "><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">c_labels</span>  <span class="pl-c"># note there's no entry for cluster 9</span>
<span class="pl-en">array</span>([<span class="pl-c1">7</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">6</span>, <span class="pl-c1">7</span>, <span class="pl-c1">5</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>, <span class="pl-c1">3</span>, <span class="pl-c1">8</span>, <span class="pl-c1">8</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int32</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">c_centers</span> <span class="pl-c"># two of these 10 clusters have identical centers, so only 9 of them are unique</span>
<span class="pl-en">array</span>([[   <span class="pl-c1">26.</span>,   <span class="pl-c1">110.</span>],
       [ <span class="pl-c1">1188.</span>,   <span class="pl-c1">247.</span>],
       [ <span class="pl-c1">1087.</span>,   <span class="pl-c1">347.</span>],
       [ <span class="pl-c1">1187.</span>,   <span class="pl-c1">233.</span>],
       [ <span class="pl-c1">1193.</span>,   <span class="pl-c1">241.</span>],
       [ <span class="pl-c1">1185.</span>,   <span class="pl-c1">249.</span>],
       [ <span class="pl-c1">1190.</span>,   <span class="pl-c1">244.</span>],
       [ <span class="pl-c1">1086.</span>,   <span class="pl-c1">348.</span>],
       [   <span class="pl-c1">26.</span>,   <span class="pl-c1">111.</span>],
       [   <span class="pl-c1">26.</span>,   <span class="pl-c1">110.</span>]]) </pre></div>
<h4 dir="auto">Versions</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Darwin-16.7.0-x86_64-i386-64bit
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
NumPy 1.13.1
SciPy 0.19.1
Scikit-Learn 0.18.2"><pre class="notranslate"><span class="pl-v">Darwin</span><span class="pl-c1">-</span><span class="pl-c1">16.7</span>.<span class="pl-c1">0</span><span class="pl-c1">-</span><span class="pl-s1">x86_64</span><span class="pl-c1">-</span><span class="pl-s1">i386</span><span class="pl-c1">-</span><span class="pl-c1">64</span><span class="pl-s1">bit</span>
<span class="pl-v">Python</span> <span class="pl-c1">3.6</span><span class="pl-c1">.1</span> <span class="pl-c1">|</span><span class="pl-v">Continuum</span> <span class="pl-v">Analytics</span>, <span class="pl-v">Inc</span>.<span class="pl-s1"></span><span class="pl-c1">|</span> (<span class="pl-s1">default</span>, <span class="pl-v">May</span> <span class="pl-c1">11</span> <span class="pl-c1">2017</span>, <span class="pl-c1">13</span>:<span class="pl-c1">04</span>:<span class="pl-c1">09</span>)
[<span class="pl-v">GCC</span> <span class="pl-c1">4.2</span><span class="pl-c1">.1</span> <span class="pl-v">Compatible</span> <span class="pl-v">Apple</span> <span class="pl-v">LLVM</span> <span class="pl-c1">6.0</span> (<span class="pl-s1">clang</span><span class="pl-c1">-</span><span class="pl-c1">600.0</span>.<span class="pl-c1">57</span>)]
<span class="pl-v">NumPy</span> <span class="pl-c1">1.13</span><span class="pl-c1">.1</span>
<span class="pl-v">SciPy</span> <span class="pl-c1">0.19</span><span class="pl-c1">.1</span>
<span class="pl-v">Scikit</span><span class="pl-c1">-</span><span class="pl-v">Learn</span> <span class="pl-c1">0.18</span><span class="pl-c1">.2</span></pre></div>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">PCA fit_transform() gives different (and wrong) results with fit() first and then transform() on the same data, and doing two separately yields the correct results.</p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sklearn.decomposition import PCA

test_encoding=np.matrix([[0., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1.],
        [0., 0., 0., 0., 0., 1.]])

# two steps
pca = PCA(n_components=2)
pca.fit(test_encoding)
print(pca.transform(test_encoding)) # correct results

# one step
pca = PCA(n_components=2)
print(pca.fit_transform(test_encoding)) # wrong results, because the first 3 rows should be the same"><pre class="notranslate"><code class="notranslate">from sklearn.decomposition import PCA

test_encoding=np.matrix([[0., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1.],
        [0., 0., 0., 0., 0., 1.]])

# two steps
pca = PCA(n_components=2)
pca.fit(test_encoding)
print(pca.transform(test_encoding)) # correct results

# one step
pca = PCA(n_components=2)
print(pca.fit_transform(test_encoding)) # wrong results, because the first 3 rows should be the same
</code></pre></div>
<h3 dir="auto">Expected Results</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="array([[-5.65685425e-01, -8.88178420e-17],
       [-5.65685425e-01, -8.88178420e-17],
       [-5.65685425e-01, -8.88178420e-17],
       [ 8.48528137e-01,  1.33226763e-16],
       [ 8.48528137e-01,  1.33226763e-16]])
"><pre class="notranslate"><code class="notranslate">array([[-5.65685425e-01, -8.88178420e-17],
       [-5.65685425e-01, -8.88178420e-17],
       [-5.65685425e-01, -8.88178420e-17],
       [ 8.48528137e-01,  1.33226763e-16],
       [ 8.48528137e-01,  1.33226763e-16]])

</code></pre></div>
<h3 dir="auto">Actual Results</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="array([[-5.65685425e-01,  6.23316960e-17],
       [-5.65685425e-01, -5.02181828e-17],
       [-5.65685425e-01, -5.02181828e-17],
       [ 8.48528137e-01, -1.27015565e-17],
       [ 8.48528137e-01, -1.27015565e-17]])"><pre class="notranslate"><code class="notranslate">array([[-5.65685425e-01,  6.23316960e-17],
       [-5.65685425e-01, -5.02181828e-17],
       [-5.65685425e-01, -5.02181828e-17],
       [ 8.48528137e-01, -1.27015565e-17],
       [ 8.48528137e-01, -1.27015565e-17]])
</code></pre></div>
<h3 dir="auto">Versions</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import sklearn
sklearn.__version__ # '0.23.2'"><pre class="notranslate"><code class="notranslate">import sklearn
sklearn.__version__ # '0.23.2'
</code></pre></div>