0

How would I combine these two arrays:

x = np.asarray([[1.0, 1.1, 1.2, 1.3], [2.0, 2.1, 2.2, 2.3], [3.0, 3.1, 3.2, 3.3], 
                 [4.0, 4.1, 4.2, 4.3], [5.0, 5.1, 5.2, 5.3]])
y = np.asarray([[0.1], [0.2], [0.3], [0.4], [0.5]])

Into something like this:

xy = [[0.1, [1.0, 1.1, 1.2, 1.3]], [0.2, [2.0, 2.1, 2.2, 2.3]...

Thank you for the assistance!

Someone suggested I post code that I have tried and I realized I had forgot to:

xy = np.array(list(zip(x, y)))

This is my current solution, however it is extremely inefficient.

6
  • 1
    You forgot to post the code you tried. Commented Dec 8, 2020 at 17:58
  • 1
    In NumPy, the original two separate arrays will be a lot more convenient and efficient to work with. Commented Dec 8, 2020 at 17:58
  • This would be ideal, however I have to randomize both arrays and so need some way to pair them together first. Commented Dec 8, 2020 at 18:02
  • 1
    No you don't. If you're talking about shuffling them in the same order, you can do that by generating a single random permutation array and applying that permutation to both arrays. Commented Dec 8, 2020 at 18:03
  • You could make a (5,2) object dtype array and assign these arrays (with some care). Or makes (5,) shape structured array, and assign the arrays to fields. But if all you are doing is shuffling, I agree that it's better to leave them separate. Commented Dec 8, 2020 at 19:53

2 Answers 2

1

You can use zip to combine

[[a,b] for a,b in zip(y,x)]

Out:

[[array([0.1]), array([1. , 1.1, 1.2, 1.3])],
 [array([0.2]), array([2. , 2.1, 2.2, 2.3])],
 [array([0.3]), array([3. , 3.1, 3.2, 3.3])],
 [array([0.4]), array([4. , 4.1, 4.2, 4.3])],
 [array([0.5]), array([5. , 5.1, 5.2, 5.3])]]
Sign up to request clarification or add additional context in comments.

Comments

1

A pure numpy solution will be much faster than list comprehension for large arrays.

I do have to say your use case makes no sense, as there is no logic in putting these arrays into a single data structure, and I believe you should re check your design.

Like @user2357112 supports Monica was subtly implying, this is very likely an XY problem. See if this is really what you are trying to solve, and not something else. If you want something else, try asking about that.

I strongly suggest checking what you want to do before moving on, as you will put yourself in a place with bad design.


That aside, here's a solution

import numpy as np


x = np.asarray([[1.0, 1.1, 1.2, 1.3], [2.0, 2.1, 2.2, 2.3], [3.0, 3.1, 3.2, 3.3],
                 [4.0, 4.1, 4.2, 4.3], [5.0, 5.1, 5.2, 5.3]])
y = np.asarray([[0.1], [0.2], [0.3], [0.4], [0.5]])


xy = np.hstack([y, x])
print(xy)

prints

[[0.1 1.  1.1 1.2 1.3]
 [0.2 2.  2.1 2.2 2.3]
 [0.3 3.  3.1 3.2 3.3]
 [0.4 4.  4.1 4.2 4.3]
 [0.5 5.  5.1 5.2 5.3]]

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.