使用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.argminnumpy.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 and numpy.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
2022-03-08 19:03

满意答案

您无法在场景编辑器中执行此操作。 你可能根本不应该这样做。 您必须实现自己的序列化代码(以便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) return SCNNode, so you'll be forever casting your results.

Per Apple developers, SCNScene is not meant to be subclassed, and I think that extends to SCNNode. Write an extension instead.

See:

相关问答

更多

使用kineticjs的自定义编辑器(Custom Editor using kineticjs)

您可以使用UTF8的强大功能和• char var text = new Kinetic.Text({ fill : "black", text : "• item 1\n• item 2" }); 演示: http : //jsfiddle.net/lavrton/2QeM5/1/ 更新:您也可以使用html进行图像转换,然后您可以将图像添加到画布。 请参阅: http : //jsfiddle.net/lavrton/sDp6C/ You may use power of U...

我可以使默认的集合编辑器和/或自定义UIEditor为属性设置访问器吗?(Can I make the default collection editor and/or a custom UIEditor envoke the set accessor for a property?)

我不相信XML Serializer会为集合调用set访问器。 相反,它只会调用它从get访问器检索的集合实例的Add方法。 我相信您需要让您的集合类型覆盖Add和Remove方法等,以修改集合,然后将修改后的集合序列化回用户属性。 I don't believe the XML Serializer will ever call the set accessor for a collection. Instead, it will just call the Add method of the ...

kendo-ui网格自定义编辑器多选设置值(kendo-ui grid custom editor multiselect set value)

在网格中实现多选时,您需要考虑几个问题。 值不是单个值而是值数组,因此您需要实现某种方式将其显示为(例如)逗号分隔值。 如果你使用data-bind="value:' + options.field + '"那么这将覆盖value: [ "Classical" ] 。 实际上后者没有意义,因为你想要的是最初显示已经存储在Grid DataSource中的值 There are a couple of questions that you need to consider when implemen...

在AlloYUI Diagram Builder中更改节点的默认编辑器(Change default editor for node in AlloYUI Diagram Builder)

感谢罗伯特弗兰普顿,他在谷歌小组中解答了我的问题,这样做的方法是: model.push({ attributeName: 'customAttr', name: 'Custom Attribute', editor: new Y.DateCellEditor() }); 您必须在构造函数之前添加“new”来创建Y.DateCellEditor对象的新实例。 Thanks to Robert Frampton, who anwered my question in googl...

自定义JComboBox编辑器(Custom JComboBox editor)

我能看到的第一组问题是,您在NewComboEditor定义了一个JTextField和JButton ,但也定义了一个textPanel ,它包含所有这些东西。 但是,不是使用textPane上的textPane ,而是使用新创建的组件(在NewComboEditor ),而不是......实际上,我甚至不确定这是如何工作的,因为您从不初始化这些组件(在NewComboEditor ) ,所以应该有一个NullPointerException ... 如果这没有足够的问题, JTextField...

xCode 4 iOS SDK的xib编辑器中的自定义字体(Custom fonts in xib editor of xCode 4 iOS SDK)

经过很多痛苦之后,答案就是没有。 您无法在笔尖中显示自定义字体。 你可以设置它们虽然有点帮助。 我希望在某些时候苹果会解决这个问题。 After a lot of pain the answer is simply no. You can not display custom fonts in the nib. You can set them though which is a little help. I hope at some point apple will fix this.

无法访问由Umbraco自定义网格编辑器生成的JSON节点(Trouble accessing JSON node generated by Umbraco custom grid editor)

没有人评论,但我想列举一个答案,以防万一它有助于某人。 正如我所料,我只是过于复杂(当然,愚蠢)。 您访问Umbraco属性的方式是通过“mediaItem = Umbraco.Media(id)”,然后是“linkText = @ mediaItem.GetPropertyValue(”linkText)“。 以下是最终(工作)渲染器的外观: @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic> <div class="carousel owl-...

SceneKit编辑器为节点设置自定义类(SceneKit editor set custom class for node)

您无法在场景编辑器中执行此操作。 你可能根本不应该这样做。 您必须实现自己的序列化代码(以便SCNScene加载正常工作)。 并且所有用于搜索节点的方法(按名称,命中测试,在卷中)都会返回SCNNode ,因此您将永远投射结果。 对于Apple开发人员来说, SCNScene并不意味着是子类,我认为它扩展到了SCNNode 。 写一个扩展名。 看到: 麻烦继承SCNScene 子类化SCNNode 子类化SCNScene和SceneKit编辑器 You can't do it in the sce...

如何在SceneKit编辑器中使用操作(How to use actions in SceneKit Editor)

在场景编辑器的下半部分,您可以打开动作编辑器。 选择节点后,您将能够在其时间轴中拖放操作。 您可以在“ SceneKit增强功能 ”会话中找到动作编辑器的演示。 In the lower part of the scene editor you can open the action editor. After selecting an node you'll be able to drag and drop actions in its timeline. You can find a demo...

XamPropertyGrid自定义编辑器(绑定)不起作用(XamPropertyGrid Custom Editor (Binding) not working)

它似乎只是XamPropertyGrid中的一个错误,您需要做的就是更新到Version 2015.2 It seems that it was just a bug in XamPropertyGrid, all you need to do is to update to Version 2015.2

相关文章

更多

Hadoop “Failed to set setXIncludeAware(true) for parser” 错误

Hadoop “Failed to set setXIncludeAware(true) for pa ...

微信支付http://www.cnblogs.com/True_to_me/p/3565039.html

公众号支付有2种支付方式: JS API 支付:是指用户打开图文消息或者扫描二维码,在微信内置浏览器打 ...

关于不定条件写法的问题

大家好: 现在有个问题,麻烦大家帮解答下。假设说有个集合。我现在要过滤这个集合,条件数目是不定的,装 ...

could not find system property or JNDI

Thanks everyone!! Finally got a solution for this p ...

基于Solr的HBase多条件查询测试

背景: 某电信项目中采用HBase来存储用户终端明细数据,供前台页面即时查询。HBase无可置疑拥有 ...

Solr的HBase多条件查询

摘要: 背景: 某电信项目中采用HBase来存储用户终端明细数据,供前台页面即时查询。HBase无可置 ...

solr实现满足指定距离范围条件的搜索

配置schema.xml 1 &lt;? xmlversion=&quot;1.0& ...

(转载)solr实现满足指定距离范围条件的搜索

配置schema.xml &lt;?xml version=&quot;1.0&quot; en ...

科技英语翻译480句 (四) 条件

四、条件(161-200) 161. To the extent that it is real, ...

关于多线程问题,signalAll,await问题

package ThreadTest; import java.util.concurrent.Tim ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本