对于特定问题,我有一个解决方案
[[0.34 0.26 0.76 ]
[0.79 0.82 0.37 ]
[0.93 0.87 0.94]]
对于相同的问题,我还有另一种解决方法
[[0.21 0.73 0.69 ]
[0.35 0.24 0.53]
[0.01 0.42 0.50]]
现在我必须将它们的第i个位置合并在一起,这样结果数组才像
[[0.34 0.21]
[0.26 0.73]
[0.76 0.69]
[0.79 0.35]
..........
..........
答案 0 :(得分:2)
设置
x = np.array([[0.34, 0.26, 0.76 ], [0.79, 0.82, 0.37 ], [0.93, 0.87, 0.94]])
y = np.array([[0.21, 0.73, 0.69 ], [0.35, 0.24, 0.53], [0.01, 0.42, 0.50]])
dstack
和 ravel
np.dstack([x.ravel(), y.ravel()])
array([[[0.34, 0.21],
[0.26, 0.73],
[0.76, 0.69],
[0.79, 0.35],
[0.82, 0.24],
[0.37, 0.53],
[0.93, 0.01],
[0.87, 0.42],
[0.94, 0.5 ]]])
如果您担心由此带来的额外维度,可以 vstack
进行转置:
np.vstack([x.ravel(), y.ravel()]).T
array([[0.34, 0.21],
[0.26, 0.73],
[0.76, 0.69],
[0.79, 0.35],
[0.82, 0.24],
[0.37, 0.53],
[0.93, 0.01],
[0.87, 0.42],
[0.94, 0.5 ]])
使用 np.column_stack
np.column_stack([x.ravel(), y.ravel()])
答案 1 :(得分:2)
您可以在两个数组上使用vstack
并适当地重塑:
np.vstack([arr1,arr2]).reshape(2,-1).T
示例:
>>> arr1
array([[ 0.34, 0.26, 0.76],
[ 0.79, 0.82, 0.37],
[ 0.93, 0.87, 0.94]])
>>> arr2
array([[ 0.21, 0.73, 0.69],
[ 0.35, 0.24, 0.53],
[ 0.01, 0.42, 0.5 ]])
>>> np.vstack([arr1,arr2]).reshape(2,-1).T
array([[ 0.34, 0.21],
[ 0.26, 0.73],
[ 0.76, 0.69],
[ 0.79, 0.35],
[ 0.82, 0.24],
[ 0.37, 0.53],
[ 0.93, 0.01],
[ 0.87, 0.42],
[ 0.94, 0.5 ]])
答案 2 :(得分:2)
这里是不需要numpy的单线:
[list(a) for a in zip(sum(x, []), sum(y, []))]
sum(x, [])
将列表列表展平为一个单独的列表。然后我们将两个列表压缩在一起并列出元素。
答案 3 :(得分:1)
您可以使用ravel()
和numpy.concatenate(x,y,axis)
来做到这一点:
np.concatenate((np.reshape(x.ravel(),(-1,1)),np.reshape(y.ravel(),(-1,1))),axis=1)
[[ 0.34 0.21]
[ 0.26 0.73]
[ 0.76 0.69]
[ 0.79 0.35]
[ 0.82 0.24]
[ 0.37 0.53]
[ 0.93 0.01]
[ 0.87 0.42]
[ 0.94 0.5 ]]
答案 4 :(得分:0)
还有其他一些方法可以做同样的事情。在可读性方面,numpy.ndarray.flatten
更简单。
输入数组:
In [207]: arr1
Out[207]:
array([[0.34, 0.26, 0.76],
[0.79, 0.82, 0.37],
[0.93, 0.87, 0.94]])
In [208]: arr2
Out[208]:
array([[0.21, 0.73, 0.69],
[0.35, 0.24, 0.53],
[0.01, 0.42, 0.5 ]])
第一步,将它们展平:
In [209]: arr1_flattened = arr1.flatten()[:, np.newaxis]
In [210]: arr1_flattened
Out[210]:
array([[0.34],
[0.26],
[0.76],
[0.79],
[0.82],
[0.37],
[0.93],
[0.87],
[0.94]])
In [211]: arr2_flattened = arr2.flatten()[:, np.newaxis]
In [212]: arr2_flattened
Out[212]:
array([[0.21],
[0.73],
[0.69],
[0.35],
[0.24],
[0.53],
[0.01],
[0.42],
[0.5 ]])
然后串联或堆叠它们:
# just horizontally stack (np.hstack) the flattened arrays
In [213]: np.hstack([arr1_flattened, arr2_flattened])
Out[213]:
array([[0.34, 0.21],
[0.26, 0.73],
[0.76, 0.69],
[0.79, 0.35],
[0.82, 0.24],
[0.37, 0.53],
[0.93, 0.01],
[0.87, 0.42],
[0.94, 0.5 ]])
单行:
In [205]: np.hstack([arr1.flatten()[:, None], arr2.flatten()[:, None]])
Out[205]:
array([[0.34, 0.21],
[0.26, 0.73],
[0.76, 0.69],
[0.79, 0.35],
[0.82, 0.24],
[0.37, 0.53],
[0.93, 0.01],
[0.87, 0.42],
[0.94, 0.5 ]])
# same thing can be done using np.concatenate
In [206]: np.concatenate([arr1.flatten()[:, None], arr2.flatten()[:, None]], axis=1)
Out[206]:
array([[0.34, 0.21],
[0.26, 0.73],
[0.76, 0.69],
[0.79, 0.35],
[0.82, 0.24],
[0.37, 0.53],
[0.93, 0.01],
[0.87, 0.42],
[0.94, 0.5 ]])
请注意,所有堆叠方法(stack
,hstack
,vstack
,dstack
,column_stack
)都在内部调用numpy.concatenate()