2

If I'm trying to execute :

a = np.ones((1, 2))
b = np.ones((1, 3))

np.array([a, b], dtype=np.ndarray)

I get the following error :

ValueError: could not broadcast input array from shape (2,) into shape (1,)

But I'm would like to get :

array([array([[1., 1.]]), array([[1., 1., 1.]])], dtype=object)

But if I'm executing this :

a = np.ones((1, 2))
b = np.ones((2, 4))

np.array([a, b], dtype=np.ndarray)

I get the expected results :

array([array([[1., 1.]]), array([[1., 1., 1.],
                                 [1., 1., 1.]])], dtype=object)

Running on :

numpy==1.21.2 python==3.7.11

1
  • 2
    Make a 2 element object dtype array, and assign those 2 arrays to its slots. np.array can't be used directly for this. Commented Oct 15, 2021 at 8:01

1 Answer 1

1

Based on @hpaulh comments, this works:

import numpy as np


a = np.ones((1, 2))
b = np.ones((1, 3))

test = np.empty((2,), dtype=np.ndarray)

test[0] = a
test[1] = b

returns

array([array([[1., 1.]]), array([[1., 1., 1.]])], dtype=object)
Sign up to request clarification or add additional context in comments.

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.