将不同大小的numpy数组组合成一个更大的矩阵
Combine numpy arrays of different sizes into a bigger matrix
我想将不同大小的 numpy 数组的所有可能组合成一个更大的矩阵。例如 a = np.array([1, 2, 3, 4, 5])
, b = np.array([6, 7, 8])
, c = np.array([9, 10, 3, 4, 5])
输出应该是:
array([[1., 6., 9.],
[2., 7., 10.],
[3., 8., 3.],
[4., 6., 4.],
[5., 7., 5.],
[1., 8., 10.],
[2., 6., 3.],
[3., 7., 4.],
[4., 8., 5.],
[5., 6., 9.],
.....
[5., 8., 5.])
涵盖所有可能的组合。请注意,数组 b 值正在重复。我试过制作数组然后使用广播原理。
arr= np.ones((75,3))
arr[:,0]=arr[:,0]*a
arr[:,1]=arr[:,1]*b
arr[:,2]=arr[:,2]*c
但是获取操作数不能和形状一起广播。
(编辑)需要一个可以具有可变长度数组的动态数量的解决方案。不一定是三个数组。
您可以使用列表理解:
arr = np.array([[x,y,z] for x in a for y in b for z in c])
我认为您正在寻找 meshgrid
,它适用于 任何 个数组,现在只需将它们添加到参数中即可:
np.array(np.meshgrid(a,b,c)).T.reshape(-1,3)
如果你有一个数组列表:
l = [a,b,c]
np.array(np.meshgrid(*l)).T.reshape(-1,len(l))
输出:
array([[ 1, 6, 9],
[ 1, 7, 9],
[ 1, 8, 9],
[ 2, 6, 9],
[ 2, 7, 9],
[ 2, 8, 9],
[ 3, 6, 9],
[ 3, 7, 9],
[ 3, 8, 9],
[ 4, 6, 9],
[ 4, 7, 9],
[ 4, 8, 9],
[ 5, 6, 9],
[ 5, 7, 9],
[ 5, 8, 9],
[ 1, 6, 10],
[ 1, 7, 10],
[ 1, 8, 10],
[ 2, 6, 10],
[ 2, 7, 10],
[ 2, 8, 10],
[ 3, 6, 10],
[ 3, 7, 10],
[ 3, 8, 10],
[ 4, 6, 10],
[ 4, 7, 10],
[ 4, 8, 10],
[ 5, 6, 10],
[ 5, 7, 10],
[ 5, 8, 10],
[ 1, 6, 3],
[ 1, 7, 3],
[ 1, 8, 3],
[ 2, 6, 3],
[ 2, 7, 3],
[ 2, 8, 3],
[ 3, 6, 3],
[ 3, 7, 3],
[ 3, 8, 3],
[ 4, 6, 3],
[ 4, 7, 3],
[ 4, 8, 3],
[ 5, 6, 3],
[ 5, 7, 3],
[ 5, 8, 3],
[ 1, 6, 4],
[ 1, 7, 4],
[ 1, 8, 4],
[ 2, 6, 4],
[ 2, 7, 4],
[ 2, 8, 4],
[ 3, 6, 4],
[ 3, 7, 4],
[ 3, 8, 4],
[ 4, 6, 4],
[ 4, 7, 4],
[ 4, 8, 4],
[ 5, 6, 4],
[ 5, 7, 4],
[ 5, 8, 4],
[ 1, 6, 5],
[ 1, 7, 5],
[ 1, 8, 5],
[ 2, 6, 5],
[ 2, 7, 5],
[ 2, 8, 5],
[ 3, 6, 5],
[ 3, 7, 5],
[ 3, 8, 5],
[ 4, 6, 5],
[ 4, 7, 5],
[ 4, 8, 5],
[ 5, 6, 5],
[ 5, 7, 5],
[ 5, 8, 5]])
我想将不同大小的 numpy 数组的所有可能组合成一个更大的矩阵。例如 a = np.array([1, 2, 3, 4, 5])
, b = np.array([6, 7, 8])
, c = np.array([9, 10, 3, 4, 5])
输出应该是:
array([[1., 6., 9.],
[2., 7., 10.],
[3., 8., 3.],
[4., 6., 4.],
[5., 7., 5.],
[1., 8., 10.],
[2., 6., 3.],
[3., 7., 4.],
[4., 8., 5.],
[5., 6., 9.],
.....
[5., 8., 5.])
涵盖所有可能的组合。请注意,数组 b 值正在重复。我试过制作数组然后使用广播原理。
arr= np.ones((75,3))
arr[:,0]=arr[:,0]*a
arr[:,1]=arr[:,1]*b
arr[:,2]=arr[:,2]*c
但是获取操作数不能和形状一起广播。
(编辑)需要一个可以具有可变长度数组的动态数量的解决方案。不一定是三个数组。
您可以使用列表理解:
arr = np.array([[x,y,z] for x in a for y in b for z in c])
我认为您正在寻找 meshgrid
,它适用于 任何 个数组,现在只需将它们添加到参数中即可:
np.array(np.meshgrid(a,b,c)).T.reshape(-1,3)
如果你有一个数组列表:
l = [a,b,c]
np.array(np.meshgrid(*l)).T.reshape(-1,len(l))
输出:
array([[ 1, 6, 9],
[ 1, 7, 9],
[ 1, 8, 9],
[ 2, 6, 9],
[ 2, 7, 9],
[ 2, 8, 9],
[ 3, 6, 9],
[ 3, 7, 9],
[ 3, 8, 9],
[ 4, 6, 9],
[ 4, 7, 9],
[ 4, 8, 9],
[ 5, 6, 9],
[ 5, 7, 9],
[ 5, 8, 9],
[ 1, 6, 10],
[ 1, 7, 10],
[ 1, 8, 10],
[ 2, 6, 10],
[ 2, 7, 10],
[ 2, 8, 10],
[ 3, 6, 10],
[ 3, 7, 10],
[ 3, 8, 10],
[ 4, 6, 10],
[ 4, 7, 10],
[ 4, 8, 10],
[ 5, 6, 10],
[ 5, 7, 10],
[ 5, 8, 10],
[ 1, 6, 3],
[ 1, 7, 3],
[ 1, 8, 3],
[ 2, 6, 3],
[ 2, 7, 3],
[ 2, 8, 3],
[ 3, 6, 3],
[ 3, 7, 3],
[ 3, 8, 3],
[ 4, 6, 3],
[ 4, 7, 3],
[ 4, 8, 3],
[ 5, 6, 3],
[ 5, 7, 3],
[ 5, 8, 3],
[ 1, 6, 4],
[ 1, 7, 4],
[ 1, 8, 4],
[ 2, 6, 4],
[ 2, 7, 4],
[ 2, 8, 4],
[ 3, 6, 4],
[ 3, 7, 4],
[ 3, 8, 4],
[ 4, 6, 4],
[ 4, 7, 4],
[ 4, 8, 4],
[ 5, 6, 4],
[ 5, 7, 4],
[ 5, 8, 4],
[ 1, 6, 5],
[ 1, 7, 5],
[ 1, 8, 5],
[ 2, 6, 5],
[ 2, 7, 5],
[ 2, 8, 5],
[ 3, 6, 5],
[ 3, 7, 5],
[ 3, 8, 5],
[ 4, 6, 5],
[ 4, 7, 5],
[ 4, 8, 5],
[ 5, 6, 5],
[ 5, 7, 5],
[ 5, 8, 5]])