简体   繁体   中英

ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). While fitting in model

import pandas as pd
df=pd.read_csv('titanic.csv')
from sklearn.linear_model import LogisticRegression
df['male']=df['Sex']=='male'
X= df[['Pclass','male','Age','SibSp','Parch','Fare']].values
y= df['Survived'].values
model=LogisticRegression()
model.fit(X,y)

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

Please Help me to remove this error

Don't know if it's the same, but in my case that error was due to big numbers, in particular I found those with scientific notation, such as 3.63E+08, 1.25E+09... The solution is to replace those numbers with something smaller: you can either simply replace them with x / 1000 or, the best solution, use a function to scale or normalise the data. After that, you can train your model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM