将一维数组堆叠到结构化数组中

问题描述 投票:5回答:5

我正在Python 2.7中运行Numpy 1.6,并从其他模块获取了一些一维数组。我想将这些数组打包成结构化数组,以便按名称索引原始一维数组。我在弄清楚如何将一维数组转换为二维数组并使dtype访问正确的数据时遇到麻烦。我的MWE如下:

>>> import numpy as np
>>> 
>>> x = np.random.randint(10,size=3)
>>> y = np.random.randint(10,size=3)
>>> z = np.random.randint(10,size=3)
>>> x
array([9, 4, 7])
>>> y
array([5, 8, 0])
>>> z
array([2, 3, 6])
>>> 
>>> w = np.array([x,y,z])
>>> w.dtype=[('x','i4'),('y','i4'),('z','i4')]
>>> w
array([[(9, 4, 7)],
       [(5, 8, 0)],
       [(2, 3, 6)]], 
      dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')])
>>> w['x']
array([[9],
       [5],
       [2]])
>>> 
>>> u = np.vstack((x,y,z))
>>> u.dtype=[('x','i4'),('y','i4'),('z','i4')]
>>> u
array([[(9, 4, 7)],
       [(5, 8, 0)],
       [(2, 3, 6)]],    
      dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')]) 

>>> u['x']
array([[9],
       [5],
       [2]])

>>> v = np.column_stack((x,y,z))
>>> v
array([[(9, 4, 7), (5, 8, 0), (2, 3, 6)]], 
      dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')])

>>> v.dtype=[('x','i4'),('y','i4'),('z','i4')]
>>> v['x']
array([[9, 5, 2]])

如您所见,当我的原始x数组包含[9,4,7]时,我没有试图堆叠数组,然后按'x'索引返回原始x数组的方法。有没有办法做到这一点,或者我做错了吗?

python numpy
5个回答
10
投票

一种方法是

wtype=np.dtype([('x',x.dtype),('y',y.dtype),('z',z.dtype)])
w=np.empty(len(x),dtype=wtype)
w['x']=x
w['y']=y
w['z']=z

注意,randint返回的每个数字的大小取决于您的平台,因此,在我的计算机上,我使用的是int64而不是int32,即'i4'。这另一种方式更可移植。


3
投票

您想使用np.column_stack

np.column_stack

0
投票

您可能希望为此目的查看numpy的记录数组:

“ Numpy提供了强大的功能来创建结构或记录的数组。这些数组允许人们通过结构或结构的字段来操纵数据。”

以下是有关记录数组的文档:import numpy as np x = np.random.randint(10,size=3) y = np.random.randint(10,size=3) z = np.random.randint(10,size=3) w = np.column_stack((x, y, z)) w = w.ravel().view([('x', x.dtype), ('y', y.dtype), ('z', z.dtype)]) >>> w array([(5, 1, 8), (8, 4, 9), (4, 2, 6)], dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')]) >>> x array([5, 8, 4]) >>> y array([1, 4, 2]) >>> z array([8, 9, 6]) >>> w['x'] array([5, 8, 4]) >>> w['y'] array([1, 4, 2]) >>> w['z'] array([8, 9, 6])

您可以将变量名用作字段名。


0
投票

要基于所选答案,您可以使此过程动态化:

  • 您首先遍历数组(可以是单列)
  • 然后您遍历列以获取数据类型
  • 您使用这些数据类型创建空数组
  • 然后我们重复这些循环以填充数组

SETUP

http://docs.scipy.org/doc/numpy/user/basics.rec.html

解决问题

# First, let's build a structured array
rows = [
    ("A", 1),
    ("B", 2),
    ("C", 3),
]
dtype = [
    ("letter", str, 1),
    ("number", int, 1),
]
arr = np.array(rows, dtype=dtype)

# Then, let's create a standalone column, of the same length:
rows = [
    1.0,
    2.0,
    3.0,
]
dtype = [
    ("float", float, 1)
]
new_col = np.array(rows, dtype=dtype)

希望有帮助


-1
投票

使用字典

# Now, we dynamically create an empty array with the dtypes from our structured array and our new column:
dtypes = []
for array in [arr, new_col]:
    for name in array.dtype.names:
        dtype = (name, array[name].dtype)
        dtypes.append(dtype)
new_arr = np.empty(len(new_col), dtype=dtypes)

# Finally, put your data in the empty array:
for array in [arr, new_col]:
    for name in array.dtype.names:
        new_arr[name] = array[name]
© www.soinside.com 2019 - 2024. All rights reserved.