Python “在 double_scalars 中遇到无效值”警告,可能是 numpy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3767409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'invalid value encountered in double_scalars' warning, possibly numpy
提问by Theodor
As I run my code I get these warnings, always in groups of four, sporadically. I have tried to locate the source by placing debug messages before and after certain statements to pin-point its origin.
当我运行我的代码时,我会偶尔收到这些警告,总是四人一组。我试图通过在某些语句前后放置调试消息来定位源。
Warning: invalid value encountered in double_scalars
Warning: invalid value encountered in double_scalars
Warning: invalid value encountered in double_scalars
Warning: invalid value encountered in double_scalars
Is this is a Numpy warning, and what is a double scalar?
这是一个 Numpy 警告,什么是双标量?
From Numpy I use
从 Numpy 我使用
min(), argmin(), mean() and random.randn()
I also use Matplotlib
我也使用 Matplotlib
采纳答案by eumiro
It looks like a floating-point calculation error. Check the numpy.seterrfunction to get more information about where it happens.
看起来像是浮点计算错误。检查numpy.seterr函数以获取有关发生位置的更多信息。
回答by Jeff
Sometimes NaNs or null values in data will generate this error with Numpy. If you are ingesting data from say, a CSV file or something like that, and then operating on the data using numpy arrays, the problem could have originated with your data ingest. You could try feeding your code a small set of data with known values, and see if you get the same result.
有时,数据中的 NaN 或空值会使用 Numpy 生成此错误。如果您从 CSV 文件或类似文件中摄取数据,然后使用 numpy 数组对数据进行操作,则问题可能源于您的数据摄取。您可以尝试为您的代码提供一小组具有已知值的数据,然后查看是否得到相同的结果。
回答by Volod
In my case, I found out it was division by zero.
就我而言,我发现它被零除。
回答by Dave
Zero-size array passed to numpy.meanraises this warning (as indicated in several comments).
传递给的零大小数组会numpy.mean引发此警告(如几条评论所示)。
For some other candidates:
对于其他一些候选人:
medianalso raises this warning on zero-sized array.
median也会在零大小的数组上引发此警告。
other candidates do not raise this warning:
其他候选人没有提出这个警告:
min,argminboth raiseValueErroron empty arrayrandntakes*arg; usingrandn(*[])returns a single random numberstd,varreturnnanon an empty array
min,argmin都ValueError在空数组上引发randn需要*arg; usingrandn(*[])返回一个随机数std,var返回nan一个空数组
回答by S_Dhungel
I ran into similar problem - Invalid value encountered in ... After spending a lot of time trying to figure out what is causing this error I believe in my case it was due to NaN in my dataframe. Check out working with missing data in pandas.
我遇到了类似的问题 - 在...中遇到无效值在花了很多时间试图找出导致此错误的原因之后,我相信在我的情况下,这是由于我的数据帧中的 NaN 造成的。查看如何处理熊猫中的缺失数据。
None == None True
无 == 无 真
np.nan == np.nan False
np.nan == np.nan 错误
When NaN is not equal to NaN then arithmetic operations like division and multiplication causes it throw this error.
当 NaN 不等于 NaN 时,除法和乘法等算术运算会导致抛出此错误。
Couple of things you can do to avoid this problem:
您可以采取以下措施来避免此问题:
Use pd.set_option to set number of decimal to consider in your analysis so an infinitesmall number does not trigger similar problem - ('display.float_format', lambda x: '%.3f' % x).
Use df.round() to round the numbers so Panda drops the remaining digits from analysis. And most importantly,
Set NaN to zero df=df.fillna(0). Be careful if Filling NaN with zero does not apply to your data sets because this will treat the record as zero so N in the mean, std etc also changes.
使用 pd.set_option 设置分析中要考虑的小数位数,因此无穷小的数字不会触发类似的问题 - ('display.float_format', lambda x: '%.3f' % x)。
使用 df.round() 对数字进行四舍五入,以便 Panda 从分析中删除剩余的数字。而最重要的是,
将 NaN 设置为零 df=df.fillna(0)。如果用零填充 NaN 不适用于您的数据集,请小心,因为这会将记录视为零,因此平均值、标准等中的 N 也会发生变化。
回答by Abhinav Bangia
Whenever you are working with csv imports, try to use df.dropna() to avoid all such warnings or errors.
每当您使用 csv 导入时,请尝试使用 df.dropna() 来避免所有此类警告或错误。
回答by u9884248
I encount this while I was calculating np.var(np.array([])). np.varwill divide size of the array which is zero in this case.
我在计算时遇到了这个np.var(np.array([]))。np.var将划分数组的大小,在这种情况下为零。

