创建数组数组时与 numpy 广播的行为不一致

Inconsistent behaviour with numpy broadcasting while creating array of array

如果我尝试执行:

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

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

我收到以下错误:

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

但我想得到:

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

但是如果我执行这个:

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

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

我得到了预期的结果:

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

运行 在 :

numpy==1.21.2 python==3.7.11

根据@hpaulh 的评论,这个有效:

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)