Reputation: 349
I have one solution for particular problem as
[[0.34 0.26 0.76 ]
[0.79 0.82 0.37 ]
[0.93 0.87 0.94]]
I have another solution for same problem as
[[0.21 0.73 0.69 ]
[0.35 0.24 0.53]
[0.01 0.42 0.50]]
Now I have to merge their ith position together so the resultant array would be like
[[0.34 0.21]
[0.26 0.73]
[0.76 0.69]
[0.79 0.35]
..........
..........
Upvotes: 4
Views: 780
Reputation: 61505
Here are some more ways to do the same thing. In terms of readability, numpy.ndarray.flatten
is more straightforward.
Input arrays:
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 ]])
As a first step, flatten them:
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 ]])
Then concatenate or stack them:
# 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 one-line:
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 ]])
Note that all the stacking methods (stack
, hstack
, vstack
, dstack
, column_stack
), call numpy.concatenate()
under the hood.
Upvotes: 0
Reputation: 51185
Setup
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
and 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 ]]])
If you're concerned with the extra dimension this introduces, you can vstack
and transpose:
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 ]])
Another alternative using np.column_stack
np.column_stack([x.ravel(), y.ravel()])
Upvotes: 2
Reputation: 1599
Here's a one-liner that doesn't need numpy:
[list(a) for a in zip(sum(x, []), sum(y, []))]
sum(x, [])
flattens the list of list into a single flat list. Then we zip the two lists together and list out the elements.
Upvotes: 2
Reputation: 500
You may do it like this by using ravel()
and 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 ]]
Upvotes: 1
Reputation: 51425
You can use vstack
on your 2 arrays and reshape appropriately:
np.vstack([arr1,arr2]).reshape(2,-1).T
Example:
>>> 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 ]])
Upvotes: 2