繁体   English   中英

如何将许多 2d numpy arrays 组合成带填充的 3d 数组

[英]how to combine many 2d numpy arrays into 3d array with padding

我有几个 numpy 2D arrays 包含坐标。 我想将所有这些 2D arrays 组合成一个 3D 数组(坐标列表),所有缺失的坐标都用 [0,0] 填充以使每个列表大小相同。 我有一些工作使用 np.pad 将 1D arrays 组合到 2D,但无法让它从 2D 到 go 到 3D。

例如:

[[0.1,0.1], [0.2,0.2], [0.3,0.3]]

现在,2d arrays 具有不同的大小,即持有不同数量的坐标。 上面是一个 (2,3) 数组。 我可能有一个(2,5)和一个(2,8)和一个(2,6)。

所以以上面的例子,以及下面的两个 arrays 为例:

[[0.2,0.2], [0.2,0.2], [0.2,0.2], [0.2,0.2], [0.2,0.2]]
[[0.3,0.3], [0.3,0.3], [0.3,0.3], [0.3,0.3]]

结果将是(为清楚起见添加空格):

[
[[0.1,0.1], [0.2,0.2], [0.3,0.3], [0.0,0.0], [0.0,0.0]]
[[0.2,0.2], [0.2,0.2], [0.2,0.2], [0.2,0.2], [0.2,0.2]]
[[0.3,0.3], [0.3,0.3], [0.3,0.3], [0.3,0.3], [0.0,0.0]]
]

注意最终的形状是 (2,5,3),第一行切片填充了 2 [0,0],第三行切片填充了 1 [0,0]

感谢任何帮助!

也许有人会提供很棒的 NumPy 魔法来直接执行此操作,但与此同时,您可以填充 Python 循环并在之后形成一个数组:

# the padding element
pad = [0.0, 0.0]

# the coords
data = [
    [[0.1,0.1], [0.2,0.2], [0.3,0.3]],
    [[0.2,0.2], [0.2,0.2], [0.2,0.2], [0.2,0.2], [0.2,0.2]],
    [[0.3,0.3], [0.3,0.3], [0.3,0.3], [0.3,0.3]],
]

# do the padding
maxl = max(len(v) for v in data)
for v in data:
    npad = maxl - len(v)
    v.extend([pad] * npad)

# convert to a NumPy array
data = np.array(data)

# data is now (3,5,2), you can reshape as needed:

array([[[0.1, 0.1],
        [0.2, 0.2],
        [0.3, 0.3],
        [0. , 0. ],
        [0. , 0. ]],

       [[0.2, 0.2],
        [0.2, 0.2],
        [0.2, 0.2],
        [0.2, 0.2],
        [0.2, 0.2]],

       [[0.3, 0.3],
        [0.3, 0.3],
        [0.3, 0.3],
        [0.3, 0.3],
        [0. , 0. ]]])

通过找到最大数组长度,我们可以根据该长度填充 arrays只需填充 NumPy

a = np.array([[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]])
b = np.array([[0.2, 0.2], [0.2, 0.2], [0.2, 0.2], [0.2, 0.2], [0.2, 0.2]])
c = np.array([[0.3, 0.3], [0.3, 0.3], [0.3, 0.3], [0.3, 0.3]])

max_len = max(a.shape[0], b.shape[0], c.shape[0])
pad_fill = np.array([0.0, 0.0])

A_pad = np.pad(a, ((0, max_len - a.shape[0]), (0, 0)), constant_values=pad_fill)
B_pad = np.pad(b, ((0, max_len - b.shape[0]), (0, 0)), constant_values=pad_fill)
C_pad = np.pad(c, ((0, max_len - c.shape[0]), (0, 0)), constant_values=pad_fill)

# A_pad:  [[0.1 0.1] [0.2 0.2] [0.3 0.3] [0.  0. ] [0.  0. ]]
# B_pad:  [[0.2 0.2] [0.2 0.2] [0.2 0.2] [0.2 0.2] [0.2 0.2]]
# C_pad:  [[0.3 0.3] [0.3 0.3] [0.3 0.3] [0.3 0.3] [0.  0. ]]

然后使用像np.concotenate这样的代码根据需要组合它们:

np.concatenate((A_pad, B_pad, C_pad), axis=0).reshape(3, 5, 2)

# [[[0.1 0.1] [0.2 0.2] [0.3 0.3] [0.  0. ] [0.  0. ]]
#  [[0.2 0.2] [0.2 0.2] [0.2 0.2] [0.2 0.2] [0.2 0.2]]
#  [[0.3 0.3] [0.3 0.3] [0.3 0.3] [0.3 0.3] [0.  0. ]]]

使用itertools.zip_longest

from itertools import zip_longest

d = [
    [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]],
    [[0.2, 0.2], [0.2, 0.2], [0.2, 0.2], [0.2, 0.2], [0.2, 0.2]],
    [[0.3, 0.3], [0.3, 0.3], [0.3, 0.3], [0.3, 0.3]],
]

out = list(zip(*zip_longest(*d, fillvalue=[0.0, 0.0])))
print(np.array(out))

印刷:

[[[0.1 0.1]
  [0.2 0.2]
  [0.3 0.3]
  [0.  0. ]
  [0.  0. ]]

 [[0.2 0.2]
  [0.2 0.2]
  [0.2 0.2]
  [0.2 0.2]
  [0.2 0.2]]

 [[0.3 0.3]
  [0.3 0.3]
  [0.3 0.3]
  [0.3 0.3]
  [0.  0. ]]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM