NumPy总和沿不相交索引(NumPy sum along disjoint indices)

我有一个应用程序,我需要在3D NumPy数组中对任意索引组进行求和。 内置的NumPy数组求和例程对一个ndarray维度的所有索引进行求和。 相反,我需要总结我的数组中的一个维度的索引范围,并返回一个新的数组。

例如,让我们假设我有一个形状的ndarray (70,25,3) 。 我希望总结沿着特定索引范围的第一个维度并返回一个新的3D数组。 考虑从0:25, 25:5050:75的总和0:25, 25:50这将返回一个形状数组(3,25,3)

是否有一种简单的方法可以在NumPy数组的一维上执行“不相交的和”来产生这个结果?


I have an application where I need to sum across arbitrary groups of indices in a 3D NumPy array. The built-in NumPy array sum routine sums up all indices along one of the dimensions of an ndarray. Instead, I need to sum up ranges of indices along one of the dimensions in my array and return a new array.

For example, let's assume that I have an ndarray with shape (70,25,3). I wish to sum up the first dimension along certain index ranges and return a new 3D array. Consider the sum from 0:25, 25:50 and 50:75 which would return an array of shape (3,25,3).

Is there an easy way to do "disjoint sums" along one dimension of a NumPy array to produce this result?


原文:https://stackoverflow.com/questions/32798519
2022-01-11 15:01

满意答案

有了两个整数,你只需使用位移和|

high = 0x00000000fe000000
low = 0x000000000001009f

result = (high << 32) | low

对于任何其他输入,我建议先将它们转换为int ,然后将结果转换回您想要的任何形式。

要检索原始片段,请反向使用位移

high = result >> 32

&与一个适当的面具:

low = result & 0xffffffff

With two ints, you simply use bit-shifting and |:

high = 0x00000000fe000000
low = 0x000000000001009f

result = (high << 32) | low

For any other input, I suggest converting them to ints first, and converting the result back to whatever form you want.

To retrieve the original pieces, use bit-shifting in reverse

high = result >> 32

and & with an appropriate mask:

low = result & 0xffffffff

相关问答

更多

N位的二进制组合,最多M“1”(Binary combinations of N bits with at most M “1”)

精确为m“1”的二元组合的总数是组合C(n,m) ,所以我认为公式是 C(n,m) + C(n,m-1) + ... + C(n,1) + 1 The total number for binary combination with exactly m "1" is combinatorial C(n,m), so I think the formula is C(n,m) + C(n,m-1) + ... + C(n,1) + 1

在python中将字节操作到二进制级别(manipulate bytes down to binary levels in python)

看一下bitstring模块,它旨在使二进制操作尽可能简单。 from bitstring import BitArray a = BitArray('0xfeed') # 16 bits from hex a += '0b001' # Add three binary bits a.replace('0b111', '0b0') # Search and replace a.count(1) # Count one bits 它有一...

在Python中将整数转换为二进制,并比较这些位(Convert integer to binary in python and compare the bits)

使用bin()函数: >>> bin(5) '0b101' 或str.format : >>> '{0:04b}'.format(5) '0101' Use bin() function: >>> bin(5) '0b101' or str.format: >>> '{0:04b}'.format(5) '0101'

Python中二进制的浮点表示(位不是十六进制)(Float representation of binary in Python (bits not hex))

使用struct.pack和struct.unpack : >>> import struct >>> n = '00111111100000000000000000000000' >>> struct.unpack('f', struct.pack('i', int(n, 2)))[0] 1.0 int(.., 2)将二进制表示转换为int (base 2) struct.pack('i', ..)转换字节( i :32bit int) struct.unpack('f', ...)[0]将字...

Python二进制操作 - 组合高位和低位(Python binary operations - combine higher and lower order bits)

有了两个整数,你只需使用位移和| : high = 0x00000000fe000000 low = 0x000000000001009f result = (high << 32) | low 对于任何其他输入,我建议先将它们转换为int ,然后将结果转换回您想要的任何形式。 要检索原始片段,请反向使用位移 high = result >> 32 和&与一个适当的面具: low = result & 0xffffffff With two ints, you simply use bit...

Python:选择代表二进制数的位数(Python: Choose number of bits to represent binary number)

使用格式字符串语法 : >>> format(1, '#04b') '0b01' >>> format(1, '#05b') '0b001' >>> format(1, '#06b') '0b0001' Use the Format String Syntax: >>> format(1, '#04b') '0b01' >>> format(1, '#05b') '0b001' >>> format(1, '#06b') '0b0001'

如何组合char矢量的二进制第n位(how to combine binary nth bit of char vector)

不要将文件读入std::vector<unsigned char> ,而是使用std::vector<std::bitset<32>> 。 首先,将文件读入std::bitset -s的std::vector 。 因为它只有一个unsigned long的构造函数,所以我们必须这样做: std::vector<std::bitset<32>> batches; std::ifstream fin(<path>, std::ios::binary); uint32_t x; while (fin >...

Python:将一个字节转换为二进制并移位它的位?(Python: convert a byte to binary and shift its bits?)

您可以将字符串转换为一个大整数,然后执行左移(然后将大整数转换回字符串): large_int = bytes2int(mystring) large_int <<= 1 mystring = int2bytes(large_int) 使用例如这种简单的实现: def bytes2int(str): res = ord(str[0]) for ch in str[1:]: res <<= 8 res |= ord(ch) return re...

LAHF(用标志寄存器的低位字节加载AH)(LAHF (load AH with lower order byte of the flag register))

标志SF,ZF,AF,PF和CF分别被复制到AH位7,6,4,2和0。 AH的剩余比特5,3和1的内容通常被认为是未定义的。 如果查看较旧的指令集,例如80386指令集,则会出现这种情况。 然而,最新的IA-32英特尔®架构软件开发人员手册第2卷:指令集参考文件阐明了LAHF的显式位状态: Operation AH ← EFLAGS(SF:ZF:0:AF:0:PF:1:CF); The flags SF, ZF, AF, PF, and CF are copied to AH bits 7, 6...

如何在python中使用XOR进行二进制操作?(How do binary operations with XOR in python work?)

100是十进制100 (二进制1100100 )。 使用0bnnn表单进行二进制表示。 >>> 0b100 4 >>> 100 100 >>> 0b100 == 100 False >>> 0b100 ^ 0b101 1 >>> 0b100 & 0b101 4 >>> bin(0b100 & 0b101) '0b100' >>> '{:b}'.format(0b100 & 0b101) '100' 100 is decimal 100 (1100100 in binary). Use 0bn...

相关文章

更多

求 java生产一个不相同的编号

问题是这样的: 现在需要一个编号 , 编号规则为当天日期+随机号,随机号为从 1 开始到 999自动 ...

求一个group by后面字段顺序影响结果的例子

select a,b ,sum(e) from test group by a,b order by ...

SQL中GROUP BY与HAVING的用法

显示每个地区的总人口数和总面积. SELECT region, SUM(population), SU ...

elasticsearch 口水篇(6) Mapping 定义索引

前面我们感觉ES就想是一个nosql数据库,支持Free Schema。 接触过Lucene、solr ...

elasticsearch RESTful搜索引擎-(java jest 使用[入门])

elasticsearch简称ES jest 好吧下面我介绍下jest(第三方工具),个人认为还是 ...

色子问题

题目分析来自微信公众平台。 刚学习python,联系一下。 原题 n个 ...

solr索引

solr索引 当我们真正进入到Lucene源代码之中的时候,我们会发现: • Lucene的索引过程 ...

.Solr构建索引-查询索引

折腾了一上午终于完整的展示了一下Solr功能 现在总结如下 0.注意事项 &lt;1 ...

solrj索引操作

添加索引 Solr添加文档至索引: http://www.cnblogs.com/dennisit/p ...

Python资源索引 【转载】

原文地址:http://blog.chinaunix.net/uid-25525723-id-3630 ...

最新问答

更多

获取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}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本