'KMeans - OverflowError: cannot convert float infinity to integer

Below are the top 5 rows of the variables, I am using for the KMeans to find the optimal no of clusters-

store_code  PinCode sale_price_after_promo
0   2655    453441.0    55.00
1   2655    999999.0    30.00
2   2655    400064.0    418.95
3   2615    400099.0    70.00
4   2655    474001.0    34.20

This is the error I am getting -

OverflowError                             Traceback (most recent call last)
<ipython-input-62-3802a2b79f71> in <module>()
      2 for i in range(0,11):
      3     kmeans=KMeans(n_clusters=i, init='k-means++', random_state=42)
----> 4     kmeans.fit(X)
      5     wcss.append(kmeans.inertia_)
      6 plt.plot(range(1, 11), wcss)

OverflowError: cannot convert float infinity to integer

How to get rid of this error even I have tried using various combinations of variables but still getting the same error?



Solution 1:[1]

You cannot run k-means with k=0 clusters.

Also, your data seems very inappropriate for k-means.

It absolutely makes no sense to run k-means on identifier attributes such as "storeId" and "pinCode".

Solution 2:[2]

Basically we can't run KMean cluster with k=0

Solution is:-

for i in range(1,11):

I mean use k in range(1 to 11) not in range(0,11)

It will solve your problem.

Solution 3:[3]

I tried for i in range(1,11): but still it did not run properly. But instead mine was solved by using

for i in range(1,12):

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Has QUIT--Anony-Mousse
Solution 2 Sanjiv
Solution 3 Paul Roub