如何修复:true_divide 中遇到无效值
使用 NumPy 时可能遇到的一个警告是:
RuntimeWarning : invalid value encountered in true_divide
当您尝试除以 NumPy 数组中的无效值(例如 NaN、Inf 等)时,会出现此警告。
应该注意的是,这只是一个警告,当尝试除以无效值时,NumPy 将仅返回 nan 值。
以下示例展示了如何在实践中响应此警告。
如何重现错误
假设我们尝试将一个 NumPy 数组的值除以另一个 NumPy 数组的值:
import numpy as np #define NumPy arrays x = np. array ([4, 5, 5, 7, 0]) y = np. array ([2, 4, 6, 7, 0]) #divide the values in x by the values in y n.p. divide (x,y) array([2., 1.25, 0.8333, 1., no]) RuntimeWarning : invalid value encountered in true_divide
请注意,NumPy 将 x 的每个值除以 y 的相应值,但会产生RuntimeWarning 。
这是因为最后执行的除法运算是零除以零,结果是值nan 。
如何应对此警告
前面说过,这个RuntimeWarning只是一个警告,并不会阻止代码执行。
但是,如果您想抑制此类警告,可以使用以下语法:
n.p. seterr (invalid=' ignore ')
这告诉 NumPy 隐藏任何包含“无效”消息的警告。
因此,如果我们再次运行代码,我们将不会收到任何警告:
import numpy as np #define NumPy arrays x = np. array ([4, 5, 5, 7, 0]) y = np. array ([2, 4, 6, 7, 0]) #divide the values in x by the values in y n.p. divide (x,y) array([2., 1.25, 0.8333, 1., no])
输出的最后一个值仍然返回nan值,但这次没有显示警告消息。
其他资源
以下教程解释了如何修复 Python 中的其他常见错误:
如何修复 Pandas 中的 KeyError
如何修复:ValueError:无法将 float NaN 转换为 int
如何修复:ValueError:操作数无法与形状一起广播
Dressing stylish projects a positive first impression.
A attire communicates loudly before you even speak a word.
This boosts your own confidence and mindset noticeably.
Your polished look signals competence in the workplace.
https://bm1.irkpress.ru/fJ3LKYcsTvl3/
Stylish clothing let you to express your individual identity.
Others often perceive well-dressed people as more capable and reliable.
Ultimately, putting effort in your style is an investment in yourself.
Dressing well projects a strong first impression.
Your attire speaks loudly before a person even say a single word.
It enhances your personal confidence and mindset noticeably.
A polished appearance signals competence in the office.
https://links.hypebeasts.ru/eT160fUGXmKx/
Stylish clothing allow you to express your individual identity.
People often judge stylish individuals as more successful and reliable.
Ultimately, investing in your wardrobe is an investment in your personal brand.
Dressing stylish projects a strong first impression.
A outfit communicates volumes before you even say a word.
This enhances your personal self-esteem and mindset significantly.
A polished look conveys competence in the workplace.
https://b1.chenews.ru/lTIWlO3eYaIv/
Fashionable clothing let you to showcase your unique identity.
People often judge stylish individuals as more successful and reliable.
Therefore, investing in your wardrobe is an valuable step in yourself.
Looking well creates a positive first impression.
Your outfit communicates volumes before a person even say a word.
It boosts your personal self-esteem and mindset significantly.
A put-together look conveys professionalism in the workplace.
https://n1.mvmedia.ru/RUu3ne7MnRUc/
Stylish clothing allow you to showcase your individual identity.
Others often judge well-dressed individuals as more capable and trustworthy.
Ultimately, investing in your wardrobe is an valuable step in yourself.