“输入包含 NaN、无穷大或对于 dtype(‘float64’) 来说太大的值”
我正在尝试训练模型,但收到此错误
输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值。
这是我的代码的一部分,我该如何解决这个问题?
from sklearn.model_selection import train_test_split
a = clean_df.drop('AQI_calculated', axis = 1).values
b = clean_df.loc[:, 'AQI_calculated'].values
a_train, a_test, b_train, b_test = train_test_split(a, b, test_size = 0.3, random_state = 42)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(a_train, b_train)
I am trying to train a model, but I am getting this error
Input contains NaN, infinity or a value too large for dtype('float64').
Here's part of my code, how can I fix this?
from sklearn.model_selection import train_test_split
a = clean_df.drop('AQI_calculated', axis = 1).values
b = clean_df.loc[:, 'AQI_calculated'].values
a_train, a_test, b_train, b_test = train_test_split(a, b, test_size = 0.3, random_state = 42)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(a_train, b_train)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须检查数据中是否基本上有
NaN
值。 如果存在一些 NaN、无穷大或过大的值,则无法训练模型(如错误所示)。要检查,我建议您使用以下代码:
如果您想知道这些
NaN
值在哪一列,您可以这样做:一旦您知道
NaN
值在哪里,你应该和他们打交道。您可以按照@kelvt在评论中的建议简单地删除、填充或替换。You have to check if in your data you have
NaN
values basically. A model can't be trained if there are some NaN, infinity or a value to large (as the error says).To check I reccomend you using this code:
If you want to know in which column these
NaN
values are, you can do it this way:Once you know where
NaN
values are, you should have to deal with them. You can simple remove, fill or replace as @kelvt suggest in the comment.