Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Input contains NaN, infinity or a value too large for dtype('float64')

I am new on Python. I am trying to use sklearn.cluster. Here is my code:

from sklearn.cluster import MiniBatchKMeans

kmeans=MiniBatchKMeans(n_clusters=2)
kmeans.fit(df)

But I get the following error:

     50             and not np.isfinite(X).all()):
     51         raise ValueError("Input contains NaN, infinity"
---> 52                          " or a value too large for %r." % X.dtype)

 ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

I checked that the there is no Nan or infinity value. So there is only one option left. However, my data info tells me that all variables are float64, so I don't understand where the problem comes from.

df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 362358 entries, 135 to 4747145
Data columns (total 8 columns):
User         362358 non-null float64
Hour         362352 non-null float64
Minute       362352 non-null float64
Day          362352 non-null float64
Month        362352 non-null float64
Year         362352 non-null float64
Latitude     362352 non-null float64
Longitude    362352 non-null float64
dtypes: float64(8)
memory usage: 24.9 MB

Thanks a lot,

like image 215
Mitch Avatar asked Oct 18 '22 19:10

Mitch


1 Answers

By looking at your df.info(), it appears that there are 6 more non-null Users values than there are values of any other column. This would indicate that you have 6 nulls in each of the other columns, and that is the reason for the error.

<class 'pandas.core.frame.DataFrame'>
Int64Index: 362358 entries, 135 to 4747145
Data columns (total 8 columns):
User         362358 non-null float64
Hour         362352 non-null float64
Minute       362352 non-null float64
Day          362352 non-null float64
Month        362352 non-null float64
Year         362352 non-null float64
Latitude     362352 non-null float64
Longitude    362352 non-null float64
dtypes: float64(8)
memory usage: 24.9 MB
like image 107
David Maust Avatar answered Oct 21 '22 22:10

David Maust