Numpy中没有结构化数组的二进制运算符? [英] No binary operators for structured arrays in Numpy?

查看:79
本文介绍了Numpy中没有结构化数组的二进制运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,因此,在看完numpy的结构化数组的教程之后,我可以创建一些简单的示例:

Okay, so after going through the tutorials on numpy's structured arrays I am able to create some simple examples:

from numpy import array, ones
names=['scalar', '1d-array', '2d-array']
formats=['float64', '(3,)float64', '(2,2)float64']
my_dtype = dict(names=names, formats=formats)
struct_array1 = ones(1, dtype=my_dtype)
struct_array2 = array([(42., [0., 1., 2.], [[5., 6.],[4., 3.]])], dtype=my_dtype)

(我的预期用例将包含三个以上的条目,并使用非常长的1d数组.)因此,一切顺利,直到我们尝试执行一些基本数学运算为止.我收到以下所有错误:

(My intended use case would have more than three entries and would use very long 1d-arrays.) So, all goes well until we try to perform some basic math. I get errors for all of the following:

struct_array1 + struct_array2
struct_array1 * struct_array2
1.0 + struct_array1
2.0 * struct_array2

显然,即使是最简单的结构化数组,也不支持简单的运算符(+,-,*,/).还是我错过了什么?我应该看看其他包装吗(不要说Pandas,因为这完全太过分了)?这似乎是一项显而易见的功能,所以我有点傻眼了.但是,很难在网上找到关于此的chat不休.这不是严重限制了结构化数组的用处吗?为什么有人会使用结构数组而不是包装在字典中的数组?是否出于技术上的原因使之难以处理?或者,如果正确的解决方案是执行繁重的工作,那么在保持操作快速的同时又如何做到呢?

Apparently, simple operators (+, -, *, /) are not supported for even the simplest structured arrays. Or am I missing something? Should I be looking at some other package (and don't say Pandas, because it is total overkill for this)? This seems like an obvious capability, so I'm a little dumbfounded. But it's difficult to find any chatter about this on the net. Doesn't this severely limit the usefulness of structured arrays? Why would anyone use a structure array rather than arrays packed into a dict? Is there a technical reason why this might be intractable? Or, if the correct solution is to perform the arduous work of overloading, then how is that done while keeping the operations fast?

推荐答案

对整个数组进行操作的另一种方法是使用文档中描述的'union'dtype.在您的示例中,您可以通过添加联合"字段并指定重叠的偏移量"来扩展dtype:

Another way to operate on the whole array is to use the 'union' dtype described in the documentation. In your example, you could expand your dtype by adding a 'union' field, and specifying overlapping 'offsets':

from numpy import array, ones, zeros

names=['scalar', '1d-array', '2d-array', 'union']
formats=['float64', '(3,)float64', '(2,2)float64', '(8,)float64']
offsets=[0, 8, 32, 0]
my_dtype = dict(names=names, formats=formats, offsets=offsets)
struct_array3=zeros((4,), dtype=my_dtype)

['union']现在可以作为(n,8)数组访问所有数据

['union'] now gives access to all the data as a (n,8) array

struct_array3['union'] # == struct_array3.view('(8,)f8')
struct_array3['union'].shape  # (4,8)

您可以在联合"或任何其他字段上进行操作:

You can operate on 'union' or any other fields:

struct_array3['union'] += 2
struct_array3['scalar']= 1

联合"字段可以是另一个兼容的形状,例如'(2,4)float64'.这样的数组的行"可能看起来像:

The 'union' field could another compatible shape, such as '(2,4)float64'. A 'row' of such an array might look like:

array([ (3.0, [0.0, 0.0, 0.0], [[2.0, 2.0], [0.0, 0.0]], 
      [[3.0, 0.0, 0.0, 0.0], [2.0, 2.0, 0.0, 0.0]])], 
      dtype={'names':['scalar','1d-array','2d-array','union'], 
             'formats':['<f8',('<f8', (3,)),('<f8', (2, 2)),('<f8', (2, 4))], 
             'offsets':[0,8,32,0], 
             'itemsize':64})

这篇关于Numpy中没有结构化数组的二进制运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆