使用NumPy查找条件为True的范围(Find span where condition is True using NumPy)
想象一下,我有一个numpy数组,我需要找到条件为True的跨度/范围。 例如,我有以下数组,其中我试图找到项大于1的跨度:
[0, 0, 0, 2, 2, 0, 2, 2, 2, 0]
我需要找到索引(开始,停止):
(3, 5) (6, 9)
我能够实现的最快的事情是制作一个布尔数组:
truth = data > threshold
然后使用
numpy.argmin
和numpy.argmax
循环遍历数组以查找开始和结束位置。pos = 0 truth = container[RATIO,:] > threshold while pos < len(truth): start = numpy.argmax(truth[pos:]) + pos + offset end = numpy.argmin(truth[start:]) + start + offset if not truth[start]:#nothing more break if start == end:#goes to the end end = len(truth) pos = end
但是对于阵列中的数十亿个位置来说这已经太慢了,而且我发现的跨度通常只是连续几个位置。 有谁知道更快找到这些跨度的方法?
Imagine I have a numpy array and I need to find the spans/ranges where that condition is True. For example, I have the following array in which I'm trying to find spans where items are greater than 1:
[0, 0, 0, 2, 2, 0, 2, 2, 2, 0]
I would need to find indices (start, stop):
(3, 5) (6, 9)
The fastest thing I've been able to implement is making a boolean array of:
truth = data > threshold
and then looping through the array using
numpy.argmin
andnumpy.argmax
to find start and end positions.pos = 0 truth = container[RATIO,:] > threshold while pos < len(truth): start = numpy.argmax(truth[pos:]) + pos + offset end = numpy.argmin(truth[start:]) + start + offset if not truth[start]:#nothing more break if start == end:#goes to the end end = len(truth) pos = end
But this has been too slow for the billions of positions in my arrays and the fact that the spans I'm finding are usually just a few positions in a row. Does anyone know a faster way to find these spans?
原文:https://stackoverflow.com/questions/17150974
满意答案
您无法在场景编辑器中执行此操作。 你可能根本不应该这样做。 您必须实现自己的序列化代码(以便
SCNScene
加载正常工作)。 并且所有用于搜索节点的方法(按名称,命中测试,在卷中)都会返回SCNNode
,因此您将永远投射结果。对于Apple开发人员来说,
SCNScene
并不意味着是子类,我认为它扩展到了SCNNode
。 写一个扩展名。看到:
You can't do it in the scene editor. And you probably shouldn't do it at all. You'll have to implement your own serialization code (so that
SCNScene
loading works correctly). And all of the methods for searching nodes (by name, hit test, within a volume) returnSCNNode
, so you'll be forever casting your results.Per Apple developers,
SCNScene
is not meant to be subclassed, and I think that extends toSCNNode
. Write an extension instead.See:
相关问答
更多使用kineticjs的自定义编辑器(Custom Editor using kineticjs)
我可以使默认的集合编辑器和/或自定义UIEditor为属性设置访问器吗?(Can I make the default collection editor and/or a custom UIEditor envoke the set accessor for a property?)
kendo-ui网格自定义编辑器多选设置值(kendo-ui grid custom editor multiselect set value)
在AlloYUI Diagram Builder中更改节点的默认编辑器(Change default editor for node in AlloYUI Diagram Builder)
自定义JComboBox编辑器(Custom JComboBox editor)
xCode 4 iOS SDK的xib编辑器中的自定义字体(Custom fonts in xib editor of xCode 4 iOS SDK)
无法访问由Umbraco自定义网格编辑器生成的JSON节点(Trouble accessing JSON node generated by Umbraco custom grid editor)
SceneKit编辑器为节点设置自定义类(SceneKit editor set custom class for node)
如何在SceneKit编辑器中使用操作(How to use actions in SceneKit Editor)
XamPropertyGrid自定义编辑器(绑定)不起作用(XamPropertyGrid Custom Editor (Binding) not working)
相关文章
更多Hadoop “Failed to set setXIncludeAware(true) for parser” 错误
微信支付http://www.cnblogs.com/True_to_me/p/3565039.html
关于不定条件写法的问题
could not find system property or JNDI
基于Solr的HBase多条件查询测试
Solr的HBase多条件查询
solr实现满足指定距离范围条件的搜索
(转载)solr实现满足指定距离范围条件的搜索
科技英语翻译480句 (四) 条件
关于多线程问题,signalAll,await问题
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!