“输入包含 NaN、无穷大或对于 dtype(‘float64’) 来说太大的值”

发布于 2025-01-09 02:34:41 字数 508 浏览 0 评论 0原文

我正在尝试训练模型,但收到此错误

输入包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2025-01-16 02:34:42

您必须检查数据中是否基本上有 NaN 值。 如果存在一些 NaN、无穷大或过大的值,则无法训练模型(如错误所示)。

要检查,我建议您使用以下代码:

df.isnull().any().any()  #This code tells you if you have some NaN value in you dataframe

如果您想知道这些 NaN 值在哪一列,您可以这样做:

df.isnull().any()

一旦您知道 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:

df.isnull().any().any()  #This code tells you if you have some NaN value in you dataframe

If you want to know in which column these NaN values are, you can do it this way:

df.isnull().any()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文