0

I have

x=np.array([[1,2,3],[3,4,5],[5,7,8]])
y=np.array([[5,2,5],[1,1,1],[2,2,2]])

I'd like to get

xy=[(1,5),(2,2),(3,5),(3,1),(4,1),(5,1),(5,2),(7,2),(8,2)]

What's the best (quick and clean) way to do so? Also, once I have xy, how do I relate the indexes of xy to the original numpy matrixes x,y ?

3 Answers 3

2

You can use this approach to get xy:

zip(x.flatten(),y.flatten())

#[(1, 5), (2, 2), (3, 5), (3, 1), (4, 1), (5, 1), (5, 2), (7, 2), (8, 2)]
Sign up to request clarification or add additional context in comments.

Comments

1

You can try:

>>> x = [[1,2,3],[3,4,5],[5,7,8]]
>>> y = [[5,2,5],[1,1,1],[2,2,2]]
>>> x1 = [val for x_data in x for val in x_data]
>>> y1 = [val for y_data in y for val in y_data]
>>> final = [(a,b) for a, b in zip(x1, y1)]
>>> print final
[(1, 5), (2, 2), (3, 5), (3, 1), (4, 1), (5, 1), (5, 2), (7, 2), (8, 2)]

Comments

0

Transpose or use nop.dstack():

>>> np.dstack((x.ravel(), y.ravel()))[0]
array([[1, 5],
       [2, 2],
       [3, 5],
       [3, 1],
       [4, 1],
       [5, 1],
       [5, 2],
       [7, 2],
       [8, 2]])
>>> 
>>> 
>>> np.array((x.ravel(), y.ravel())).T
array([[1, 5],
       [2, 2],
       [3, 5],
       [3, 1],
       [4, 1],
       [5, 1],
       [5, 2],
       [7, 2],
       [8, 2]])

1 Comment

A minor change is to reshape after dstack: np.dstack((x,y)).reshape(-1,2).

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.