如何返回多个列的视图numpy的结构数组 [英] How to return a view of several columns in numpy structured array

查看:224
本文介绍了如何返回多个列的视图numpy的结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到几个栏(字段)用列表索引在一个 numpy的结构阵列一次字段名称,例如:

 导入numpy的是NP一个= np.array([(1.5,2.5,(1.0,2.0)),(3,4,(4,5)),(1,3,(2,6)。) ]
        DTYPE = [('X',浮),('Y',浮),('值',浮动,(2,2)))打印[['X','Y']
#[(1.5,2.5)(3.0,4.0)(1.0,3.0)]打印[['X','Y']。DTYPE
#[('X','< F4')('Y','< F4')])

但问题是,它似乎是一个副本,而不是一个观点:

  B = A [['X','Y']
B [0] =(9,9)。印片b
#[(9.0,9.0)(3.0,4.0)(1.0,3.0)]打印[['X','Y']
#[(1.5,2.5)(3.0,4.0)(1.0,3.0)]

如果我只能选择一列,这是一个观点:

  C =×〔'Y']
C [0] = 99。打印ç
#[99 4. 3.]打印['Y']
#[99 4. 3.]

有什么办法,我可以立刻得到多个列视图的行为?

我有两个解决方法,一个是刚刚遍历列,另一个是创建分层 DTYPE ,这样一列实际上返回一个结构数组我想这两个(或更多)的字段。不幸的是,拉链也返回一个副本,所以我不能做的:

  X = A ['X']; Y = A ['Y']
Z =拉链(X,Y)
Z [0] =(9,9)。


解决方案

您可以创建一个DTYPE对象只包含所需的字段,并使用 numpy.ndarray()创建原始数组的一个观点:

 导入numpy的是NP
STRC = np.zeros(3 DTYPE = [('X',INT),('Y',浮),('Z',INT),(T,I8)])高清fields_view(ARR,字段):
    dtype2 = np.dtype({名称:arr.dtype.fields [名]的名称字段})
    返回np.ndarray(arr.shape,dtype2,编曲,0,arr.strides)V1 = fields_view(STRC,[×,Z])
V1 [0] = 10,100V2 = fields_view(STRC,[Y,Z])
v2的[1:] = [(3.14,7)]V3 = fields_view(STRC,[X,T])V3 [1:] = [(1000,2 ** 16)]打印STRC

下面是输出:

  [(10,0.0,100,0L)(1000,3.14,7,65536L)(1000,3.14,7,65536L)

I can see several columns (fields) at once in a numpy structured array by indexing with a list of the field names, for example

import numpy as np

a = np.array([(1.5, 2.5, (1.0,2.0)), (3.,4.,(4.,5.)), (1.,3.,(2.,6.))],
        dtype=[('x',float), ('y',float), ('value',float,(2,2))])

print a[['x','y']]
#[(1.5, 2.5) (3.0, 4.0) (1.0, 3.0)]

print a[['x','y']].dtype
#[('x', '<f4') ('y', '<f4')])

But the problem is that it seems to be a copy rather than a view:

b = a[['x','y']]
b[0] = (9.,9.)

print b
#[(9.0, 9.0) (3.0, 4.0) (1.0, 3.0)]

print a[['x','y']]
#[(1.5, 2.5) (3.0, 4.0) (1.0, 3.0)]

If I only select one column, it's a view:

c = x['y']
c[0] = 99.

print c
#[ 99.  4.   3. ]

print a['y']
#[ 99.  4.   3. ]

Is there any way I can get the view behavior for more than one column at once?

I have two workarounds, one is to just loop through the columns, the other is to create a hierarchical dtype, so that the one column actually returns a structured array with the two (or more) fields that I want. Unfortunately, zip also returns a copy, so I can't do:

x = a['x']; y = a['y']
z = zip(x,y)
z[0] = (9.,9.)

解决方案

You can create a dtype object contains only the fields that you want, and use numpy.ndarray() to create a view of original array:

import numpy as np
strc = np.zeros(3, dtype=[('x', int), ('y', float), ('z', int), ('t', "i8")])

def fields_view(arr, fields):
    dtype2 = np.dtype({name:arr.dtype.fields[name] for name in fields})
    return np.ndarray(arr.shape, dtype2, arr, 0, arr.strides)

v1 = fields_view(strc, ["x", "z"])
v1[0] = 10, 100

v2 = fields_view(strc, ["y", "z"])
v2[1:] = [(3.14, 7)]

v3 = fields_view(strc, ["x", "t"])

v3[1:] = [(1000, 2**16)]

print strc

here is the output:

[(10, 0.0, 100, 0L) (1000, 3.14, 7, 65536L) (1000, 3.14, 7, 65536L)]

这篇关于如何返回多个列的视图numpy的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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