当你做这样的事情时, future 的警告会发生:
>>> numpy.asarray([1,2,3,None]) == None
目前返回 False,但我知道在 Numpy 的 future 版本中将返回一个包含 [False,False,False,True] 的数组。
讨论 on the numpy discussion list解决这个问题的方法是测试a is None。
让我感到困惑的是 in 关键字与一维数组相比列表的这种行为:
>>> None in [1,2,3,None]
True
>>> None in numpy.asarray([1,2,3,None])
__main__:1: FutureWarning: comparison to 'None' will result in an elementwise
object comparison in the future
False
>>> 1 in numpy.asarray([1,2,3,None])
True
编辑(见评论)——实际上有两个不同的问题:
FutureWarning - None in numpy.asarray(...) 的 future 行为与现在相比会怎样?in 与 list 的行为不同;我可以在不将其转换为列表或使用 for 循环的情况下测试我的数组是否包含 None 吗? Numpy 版本为 1.9.1,Python 3.4.1
最佳答案
The future warning happens when you do something like this:
numpy.asarray([1,2,3,4]) == NoneWhich currently returns
False, but I understand will return an array containing[False,False,False,True]in a future version of Numpy.
正如我在评论中提到的,您的示例不正确。 numpy 的 future 版本将返回 [False ,False, False, False],即对于数组中不等于 None 的每个元素返回 False >。这与当前与其他标量值的逐元素比较的工作方式更加一致,例如:
In [1]: np.array([1, 2, 3, 4]) == 1
Out[1]: array([ True, False, False, False], dtype=bool)
In [2]: np.array(['a', 'b', 'c', 'd']) == 'b'
Out[2]: array([False, True, False, False], dtype=bool)
What confuses me is this behaviour of the
inkeyword with a 1D array compared to a list
当您测试 x in y 时,您正在调用 y.__contains__(x)。当 y 是一个列表时,__contains__ 基本上按照以下方式做一些事情:
for item in y:
if (item is x) or (item == x):
return True
return False
据我所知,np.ndarray.__contains__(x) 执行与此等效的操作:
if any(y == x):
return True
else:
return False
也就是说,它首先测试整个数组的元素相等性(y == x 将是一个大小为 y 的 bool 数组)。由于在您的情况下您正在测试是否 y == None,因此出于上述原因,这将引发 FutureWarning。
在评论中你也想知道为什么
np.nan in np.array([1, 2, 3, np.nan])
返回False,但是
np.nan in [1, 2, 3, np.nan]
返回 True。第一部分很容易解释为 np.nan != np.nan ( see here for the rationale behind this )。要理解为什么第二种情况返回 True,请记住 list.__contains__() 首先检查身份(is),然后再检查相等性(==).由于 np.nan 是 np.nan,第二种情况将返回 True。
关于python - 为什么 "None in numpy.asarray(...)"会导致 future 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337085/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput