close

[Solved] Scikit-learn : Input contains NaN, infinity or a value too large for dtype (‘float64’)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Scikit-learn : Input contains NaN, infinity or a value too large for dtype (‘float64’) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Scikit-learn : Input contains NaN, infinity or a value too large for dtype (‘float64’) Error Occurs?

Today I get the following error Scikit-learn : Input contains NaN, infinity or a value too large for dtype (‘float64’) in python.

How To Solve Scikit-learn : Input contains NaN, infinity or a value too large for dtype (‘float64’) Error ?

  1. How To Solve Scikit-learn : Input contains NaN, infinity or a value too large for dtype ('float64') Error ?

    To Solve Scikit-learn : Input contains NaN, infinity or a value too large for dtype ('float64') Error I got the same error message when using sklearn with pandas. My solution is to reset the index of my dataframe df before running any sklearn code:

  2. Scikit-learn : Input contains NaN, infinity or a value too large for dtype ('float64')

    To Solve Scikit-learn : Input contains NaN, infinity or a value too large for dtype ('float64') Error I got the same error message when using sklearn with pandas. My solution is to reset the index of my dataframe df before running any sklearn code:

Solution 1

This might happen inside scikit, and it depends on what you’re doing. I recommend reading the documentation for the functions you’re using. You might be using one which depends e.g. on your matrix being positive definite and not fulfilling that criteria.

EDIT: How could I miss that:

np.isnan(mat.any()) #and gets False
np.isfinite(mat.all()) #and gets True

is obviously wrong. Right would be:

np.any(np.isnan(mat))

and

np.all(np.isfinite(mat))

You want to check wheter any of the element is NaN, and not whether the return value of the any function is a number…

Solution 2

I got the same error message when using sklearn with pandas. My solution is to reset the index of my dataframe df before running any sklearn code:

df = df.reset_index()

I encountered this issue many times when I removed some entries in my df, such as

df = df[df.label=='desired_one']

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read