简体   繁体   English

Numpy交织奇形阵列

[英]Numpy Interweave oddly shaped arrays

Alright, here the given data; 好了,这里是给定的数据; There are three numpy arrays of the shapes: (i, 4, 2), (i, 4, 3), (i, 4, 2) the i is shared among them but is variable. 有三个形状的numpy数组:(i,4,2),(i,4,3),(i,4,2)i在它们之间共享,但是可变的。 The dtype is float32 for everything. dtype的所有内容均为float32。 The goal is to interweave them in a particular order. 目的是按照特定顺序交织它们。 Let's look at the data at index 0 for these arrays: 让我们看一下这些数组在索引0处的数据:

[[-208.  -16.]
 [-192.  -16.]
 [-192.    0.]
 [-208.    0.]]

[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]

[[ 0.49609375  0.984375  ]
 [ 0.25390625  0.984375  ]
 [ 0.25390625  0.015625  ]
 [ 0.49609375  0.015625  ]]

In this case, the concatened target array would look something like this: 在这种情况下,连接的目标数组将如下所示:

[-208, -16, 1, 1, 1, 0.496, 0.984, -192, -16, 1, 1, 1, ...]

And then continue on with index 1. 然后继续索引1。

I don't know how to achieve this, as the concatenate function just keeps telling me that the shapes don't match. 我不知道如何实现此目的,因为连接功能不断告诉我形状不匹配。 The shape of the target array does not matter much, just that the memoryview of it must be in the given order for upload to a gpu shader. 目标数组的形状无关紧要,只是它的memoryview必须以给定的顺序才能上传到gpu着色器。

Edit: I could achieve this with a few python for loops, but the performance impact would be a problem in this program. 编辑:我可以使用一些python for循环来实现这一点,但是对程序性能的影响将是一个问题。

Use np.dstack and flatten with np.ravel() - 使用np.dstack并使用np.ravel()展平-

np.dstack((a,b,c)).ravel()

Now, np.dstack is basically stacking along the third axis. 现在, np.dstack基本沿第三轴堆叠。 So, alternatively we can use np.concatenate too along that axis, like so - 因此,或者我们也可以沿该轴使用np.concatenate ,就像这样-

np.concatenate((a,b,c),axis=2).ravel()

Sample run - 样品运行-

1) Setup Input arrays : 1)设置输入数组:

In [613]: np.random.seed(1234)
     ...: n = 3
     ...: m = 2
     ...: a = np.random.randint(0,9,(n,m,2))
     ...: b = np.random.randint(11,99,(n,m,2))
     ...: c = np.random.randint(101,999,(n,m,2))
     ...: 

2) Check input values : 2)检查输入值:

In [614]: a
Out[614]: 
array([[[3, 6],
        [5, 4]],

       [[8, 1],
        [7, 6]],

       [[8, 0],
        [5, 0]]])

In [615]: b
Out[615]: 
array([[[84, 58],
        [61, 87]],

       [[48, 45],
        [49, 78]],

       [[22, 11],
        [86, 91]]])

In [616]: c
Out[616]: 
array([[[104, 359],
        [376, 560]],

       [[472, 720],
        [566, 115]],

       [[344, 556],
        [929, 591]]])

3) Output : 3)输出:

In [617]: np.dstack((a,b,c)).ravel()
Out[617]: 
array([  3,   6,  84,  58, 104, 359,   5,   4,  61,  87, 376, 560,   8,
         1,  48,  45, 472, 720,   7,   6,  49,  78, 566, 115,   8,   0,
        22,  11, 344, 556,   5,   0,  86,  91, 929, 591])

What I would do is: 我要做的是:

np.hstack([a, b, c]).flatten()

assuming a, b, c are the three arrays 假设a,b,c是三个数组

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

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