如何处理“ valueerror:数组不得包含infs或nans”。在Python运行回归时

发布于 2025-01-25 05:23:02 字数 589 浏览 2 评论 0原文

我有一个具有生长变量的DF,通常某些初始值为0,在这种情况下,当值从零移动到非均方体时,它会产生无限的值。

即,

.. some variables... var1   var2   var1_growth  var2_growth
                      0      0        NaN          NaN
                      0      1        NaN          inf
                      1      2        inf           1
                     1.5    2.2       0.5          0.1
...

当我运行Panelols时,我会收到一条错误消息,

ValueError: array must not contain infs or NaNs

是否有一种方法可以忽略这些条目继续进行回归,而无需删除它们并创建其他数据集?

如果没有,最好的方法是什么?我应该在两个列中删除带有“ INF”值的应用行?有一个简单的方法吗?谢谢。

I have a df with growth variables and often some initial values are 0, in which case it produces infinite values when the value moves from zero to non-zeros.

i.e.

.. some variables... var1   var2   var1_growth  var2_growth
                      0      0        NaN          NaN
                      0      1        NaN          inf
                      1      2        inf           1
                     1.5    2.2       0.5          0.1
...

when i run PanelOLS, i get an error message

ValueError: array must not contain infs or NaNs

Is there a way to ignore these entries to continue with the regression without having to drop them and create a different dataset?

If not, what would be the best way to proceed? should I drop app rows with 'inf' values in both columns? is there an easy way to do this? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜夜流光相皎洁 2025-02-01 05:23:02

不,您不能忽略这些条目。在训练模型之前,需要处理此问题,如果没有,您将无法训练。

根据您的数据和应用程序,优先采用其他方法来处理这些naninf

df.replace([np.inf, -np.inf], np.nan).dropna(axis=1) # You can replace inf and -inf with NaN, and then select non-null rows.

在这种情况下,我们正在删除所有具有infnan值的行。

No, you can't ignore these entries. This issue need to be handle before training the model, if not, you can not train it.

Depending on your data and application a different method is preferred to handle these NaN and inf. One example of code that is posted in this SO question:

df.replace([np.inf, -np.inf], np.nan).dropna(axis=1) # You can replace inf and -inf with NaN, and then select non-null rows.

In this case, we are removing all rows that have inf or NaN values.

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