[英]Combining two unequal arrays
I have created two 1 dimensional, column arrays using numpy - one having 100 cells, and the second 10000 cells. 我使用numpy创建了两个一维的列数组-一个具有100个单元格,第二个10000个单元格。 What I want to do now is to write 2 dimensional array having for every cell in first array (the one with 100 elements) written all 10000 values from the second array.
我现在想做的是编写一个二维数组,其中第一个数组(每个单元有100个元素)的每个单元格都写入了第二个数组的所有10000个值。 Little example explaining it:
解释它的小例子:
a =
[[1],
[2],
[3]]
b =
[[4],
[5]]
And I'd like to obtain: 我想获得:
c = [[1], [4],
[1], [5],
[2], [4],
[2], [5],
[3], [4],
[3], [5]]
I was trying to find any solution, but I was unsuccessful. 我试图找到任何解决方案,但未成功。 I hope to find help here.
我希望在这里寻求帮助。 Cheers, Jonh
干杯,乔恩
Is this what you want? 这是你想要的吗? I used the function
np.repeat
to repeat each individual element (first array) and np.tile
to repeat the whole array (second array). 我使用函数
np.repeat
重复每个单独的元素(第一个数组),并使用np.tile
重复整个数组(第二个数组)。
>>> import numpy as np
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[4],[5]])
>>>
>>> at = np.repeat(a, len(b), axis = 0)
>>> at
array([[1],
[1],
[2],
[2],
[3],
[3]])
>>> bt = np.tile(b, (len(a), 1))
>>> bt
array([[4],
[5],
[4],
[5],
[4],
[5]])
>>> np.concatenate((at, bt), axis = 1)
array([[1, 4],
[1, 5],
[2, 4],
[2, 5],
[3, 4],
[3, 5]])
You want itertools.product
. 您需要
itertools.product
。
In [2]: import itertools
In [3]: scipy.array(list(itertools.product([1,2,3], [4,5])))
Out[3]:
array([[1, 4],
[1, 5],
[2, 4],
[2, 5],
[3, 4],
[3, 5]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.