4

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]
..........
..........

5 Answers 5

2

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()])
Sign up to request clarification or add additional context in comments.

2 Comments

Yess vstack not always work agree with you but dstack does the trick thanks for the answer
@ShivamSharma mind explaining why vstack's transpose doesn't work here?
2

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 ]])

Comments

2

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.

2 Comments

There are so many better ways to flatten a list than sum(x, [])
Won't disagree. I would rather use reduce for improved readability and speed but this is more fun.
1

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 ]]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.