MiniBatchKMeans OverflowError: cannot convert float infinity to integer?
<p>I am trying to find the right number of clusters, <code>k</code>, according to silhouette scores using <code>sklearn.cluster.MiniBatchKMeans</code>.</p>

<pre><code>from sklearn.cluster import MiniBatchKMeans
from sklearn.feature_extraction.text import HashingVectorizer

docs = ['hello monkey goodbye thank you', 'goodbye thank you hello', 'i am going home goodbye thanks', 'thank you very much sir', 'good golly i am going home finally']

vectorizer = HashingVectorizer()

X = vectorizer.fit_transform(docs)

for k in range(5):
    model = MiniBatchKMeans(n_clusters = k)
    model.fit(X)
</code></pre>

<p>And I receive this error:</p>

<pre><code>Warning (from warnings module):
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 1279
    0, n_samples - 1, init_size)
DeprecationWarning: This function is deprecated. Please call randint(0, 4 + 1) instead
Traceback (most recent call last):
  File "&lt;pyshell#85&gt;", line 3, in &lt;module&gt;
    model.fit(X)
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 1300, in fit
    init_size=init_size)
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 640, in _init_centroids
    x_squared_norms=x_squared_norms)
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 88, in _k_init
    n_local_trials = 2 + int(np.log(n_clusters))
OverflowError: cannot convert float infinity to integer
</code></pre>

<p>I know the <code>type(k)</code> is <code>int</code>, so I don't know where this issue is coming from. I can run the following just fine, but I can't seem to iterate through integers in a list, even though the <code>type(2)</code> is equal to <code>k = 2; type(k)</code></p>

<pre><code>model = MiniBatchKMeans(n_clusters = 2)
model.fit(X)
</code></pre>

<p>Even running a different <code>model</code> works:</p>

<pre><code>&gt;&gt;&gt; model = KMeans(n_clusters = 2)
&gt;&gt;&gt; model.fit(X)
KMeans(copy_x=True, init='k-means++', max_iter=300, n_clusters=2, n_init=10,
    n_jobs=1, precompute_distances='auto', random_state=None, tol=0.0001,
    verbose=0)
</code></pre>

<p>Let's analyze your code:</p>

<ul>
<li><code>for k in range(5)</code> returns the following sequence:

<ul>
<li><code>0, 1, 2, 3, 4</code></li>
</ul></li>
<li><code>model = MiniBatchKMeans(n_clusters = k)</code> inits model with <code>n_clusters=k</code></li>
<li>Let's look at the first iteration:

<ul>
<li><code>n_clusters=0</code> is used</li>
<li>Within the optimization-code (look at the output):</li>
<li><code>int(np.log(n_clusters))</code></li>
<li>= <code>int(np.log(0))</code></li>
<li>= <code>int(-inf)</code></li>
<li>ERROR: no infinity definition for integers!</li>
<li>-> casting floating-point value of -inf to int not possible!</li>
</ul></li>
</ul>

<p>Setting <code>n_clusters=0</code> does not make sense!</p>

python types scikit-learn infinity