# sklearn KNN fit throws out error : value too large for dtype('float64')

I have cleaned the data from nan values and infinite values, the only feature which has a large float is the column 8 (it's a sum)

I have no Idea how to fix this last error, I tried all previous solutions which are related to my question but nothing worked.

This is the result of : df.isnull().sum():

I am following this tutorial (because I'm just starting to learn about machine learning supervised algorithm KNN): Tutorial Link

Please help me out!

## 2 Answers

I'm almost certain that you have missing values, check that and fill them beforehand

Assuming you data frame is called df

df.info() will give you that info

Additionally df.describe() is a good method to validate the maxmimum value is not np.inf

• I added the result of df.isnull().sum() – geekys Aug 24 '20 at 22:31
• Does that sum 0 for all the columns? – Julio Jesus Aug 24 '20 at 22:32
• Is this a classification task? If so... is you y matrix only binary-valued? – Julio Jesus Aug 24 '20 at 22:33
• yes, I updated the question, you can see it on the image – geekys Aug 24 '20 at 22:33
• The dataframe datatypes are: int and float only – geekys Aug 24 '20 at 22:34

You should scale your data before training your data. Try something simple:

df = (df - df.min()) / (df.max() - df.min())


If you're using the L2 distance in your knn model this means some values are squared (which is probably why you max out the float64). Btw float64 max is $$2^{31} − 1$$ so check the range of the columns just to be sure there might be an outlier.