<h4 dir="auto">Description</h4>
<p dir="auto">Clustering with MiniBatchKMeans produces indexing error.</p>
<p dir="auto">I have a crontab doing some clustering every hour. This error manifests around 4-5 times in a day. I assume the kmeans initialization is stochastic?</p>
<p dir="auto">Unfortunately right now I can't find a dataset for reproducing because the error happens stochastically on a dataset that is changing every hour. If I manage to find a reproducible dataset I will attach it to the issue.</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 MiniBatchKMeans
clusterobj = MiniBatchKMeans(n_clusters=1000)
clusterobj.fit(datconcat)"><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">MiniBatchKMeans</span>
<span class="pl-s1">clusterobj</span> <span class="pl-c1">=</span> <span class="pl-v">MiniBatchKMeans</span>(<span class="pl-s1">n_clusters</span><span class="pl-c1">=</span><span class="pl-c1">1000</span>)
<span class="pl-s1">clusterobj</span>.<span class="pl-en">fit</span>(<span class="pl-s1">datconcat</span>)</pre></div>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">No error</p>
<h4 dir="auto">Actual Results</h4>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;metricdata.py&quot;, line 160, in cluster
    clusterobj.fit(datconcat)
  File &quot;/shared/sdoerr/Software/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py&quot;, line 1382, in fit
    init_size=init_size)
  File &quot;/shared/sdoerr/Software/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py&quot;, line 683, in _init_centroids
    x_squared_norms=x_squared_norms)
  File &quot;/shared/sdoerr/Software/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py&quot;, line 113, in _k_init
    X[candidate_ids], X, Y_norm_squared=x_squared_norms, squared=True)
IndexError: index 2772 is out of bounds for axis 0 with size 2772"><pre class="notranslate">Traceback (most recent call last):
  File <span class="pl-s">"metricdata.py"</span>, line <span class="pl-c1">160</span>, in <span class="pl-en">cluster</span>
    clusterobj.fit(datconcat)
  File <span class="pl-s">"/shared/sdoerr/Software/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py"</span>, line <span class="pl-c1">1382</span>, in <span class="pl-en">fit</span>
    init_size<span class="pl-k">=</span>init_size)
  File <span class="pl-s">"/shared/sdoerr/Software/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py"</span>, line <span class="pl-c1">683</span>, in <span class="pl-en">_init_centroids</span>
    x_squared_norms<span class="pl-k">=</span>x_squared_norms)
  File <span class="pl-s">"/shared/sdoerr/Software/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py"</span>, line <span class="pl-c1">113</span>, in <span class="pl-en">_k_init</span>
    X[candidate_ids], X, Y_norm_squared<span class="pl-k">=</span>x_squared_norms, squared<span class="pl-k">=</span><span class="pl-c1">True</span>)
<span class="pl-en">IndexError</span>: <span class="pl-s">index 2772 is out of bounds for axis 0 with size 2772</span></pre></div>
<h4 dir="auto">Versions</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Linux-3.10.0-327.13.1.el7.x86_64-x86_64-with-centos-7.2.1511-Core
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
NumPy 1.11.2
SciPy 0.18.1
Scikit-Learn 0.18"><pre class="notranslate"><code class="notranslate">Linux-3.10.0-327.13.1.el7.x86_64-x86_64-with-centos-7.2.1511-Core
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
NumPy 1.11.2
SciPy 0.18.1
Scikit-Learn 0.18
</code></pre></div>
<h4 dir="auto">Description</h4>
<p dir="auto">Fitting a (20242848, 5) numpy array using any more than 4 clusters results in IndexError</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sklearn.cluster import KMeans
src = gdal.Open(fname)
img = src.ReadAsArray()
X = img.transpose(1,2,0).reshape(-1, img.shape[0])
print X.shape"><pre class="notranslate"><code class="notranslate">from sklearn.cluster import KMeans
src = gdal.Open(fname)
img = src.ReadAsArray()
X = img.transpose(1,2,0).reshape(-1, img.shape[0])
print X.shape
</code></pre></div>
<p dir="auto">(20242848, 5)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="k_means = KMeans(n_clusters=4, n_jobs=-1, random_state=10)
k_means.fit(X)
labels = k_means.labels_

print labels.shape"><pre class="notranslate"><code class="notranslate">k_means = KMeans(n_clusters=4, n_jobs=-1, random_state=10)
k_means.fit(X)
labels = k_means.labels_

print labels.shape
</code></pre></div>
<p dir="auto">(20242848,)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="k_means = KMeans(n_clusters=5, n_jobs=-1, random_state=10)
k_means.fit(X)
labels = k_means.labels_

print labels.shape"><pre class="notranslate"><code class="notranslate">k_means = KMeans(n_clusters=5, n_jobs=-1, random_state=10)
k_means.fit(X)
labels = k_means.labels_

print labels.shape
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
JoblibIndexError                          Traceback (most recent call last)
&lt;ipython-input-22-379757258e42&gt; in &lt;module&gt;()
      1 k_means = KMeans(n_clusters=5, n_jobs=-1, random_state=10)
----&gt; 2 k_means.fit(X)
      3 labels = k_means.labels_
      4 
      5 print labels.shape

[SHORTENED]

IndexError: index 20242848 is out of bounds for axis 0 with size 20242848
___________________________________________________________________________"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
JoblibIndexError                          Traceback (most recent call last)
&lt;ipython-input-22-379757258e42&gt; in &lt;module&gt;()
      1 k_means = KMeans(n_clusters=5, n_jobs=-1, random_state=10)
----&gt; 2 k_means.fit(X)
      3 labels = k_means.labels_
      4 
      5 print labels.shape

[SHORTENED]

IndexError: index 20242848 is out of bounds for axis 0 with size 20242848
___________________________________________________________________________
</code></pre></div>

<h4 dir="auto">Expected Results</h4>

<p dir="auto">(20242848, )</p>
<h4 dir="auto">Actual Results</h4>

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

<p dir="auto">Windows-7-6.1.7601-SP1<br>
('Python', '2.7.11 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)]')<br>
('NumPy', '1.11.2')<br>
('SciPy', '0.18.1')<br>
('Scikit-Learn', '0.18')</p>