强制numpy创建对象数组

SiL*_*hon 4 python arrays numpy

我有一个数组:

x = np.array([[1, 2, 3], [4, 5, 6]])
Run Code Online (Sandbox Code Playgroud)

我想创建的另一个数组shape=(1, 1)dtype=np.object其唯一的元素为x。

我已经试过这段代码:

a = np.array([[x]], dtype=np.object)
Run Code Online (Sandbox Code Playgroud)

但是它会产生一系列形状(1, 1, 2, 3)

我当然可以:

a = np.zeros(shape=(1, 1), dtype=np.object)
a[0, 0] = x
Run Code Online (Sandbox Code Playgroud)

但我希望该解决方案能够轻松扩展到更大的a形状,例如:

[[x, x], [x, x]]
Run Code Online (Sandbox Code Playgroud)

无需for在所有索引上运行循环。

有什么建议可以实现吗?


UPD1

数组可能不同,如:

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [0, 1, 2]])
u = np.array([[3, 4, 5], [6, 7, 8]])
v = np.array([[9, 0, 1], [2, 3, 4]])
[[x, y], [u, v]]
Run Code Online (Sandbox Code Playgroud)

它们也可能具有不同的形状,但是对于那种情况,一个简单的np.array([[x, y], [u, v]])构造函数就可以了


UPD2

我真的想要一个可以处理任意x, y, u, v形状(不一定都一样)的解决方案。

SiL*_*hon 6

自己找到了解决方案:

a=np.zeros(shape=(2, 2), dtype=np.object)
a[:] = [[x, x], [x, x]]
Run Code Online (Sandbox Code Playgroud)


wim*_*wim 5

a = np.empty(shape=(2, 2), dtype=np.object)
a.fill(x)
Run Code Online (Sandbox Code Playgroud)


Pau*_*zer 4

这是一个非常通用的方法:它适用于嵌套列表、数组列表的列表 - 无论这些数组的形状是否不同或相等。当数据聚集在一个数组中时它也可以工作,这实际上是最棘手的情况。(到目前为止发布的其他方法在这种情况下不起作用。)

让我们从困难的情况开始,一个大数组:

# create example
# pick outer shape and inner shape
>>> osh, ish = (2, 3), (2, 5)
# total shape
>>> tsh = (*osh, *ish)
# make data
>>> data = np.arange(np.prod(tsh)).reshape(tsh)
>>>
# recalculate inner shape to cater for different inner shapes
# this will return the consensus bit of all inner shapes
>>> ish = np.shape(data)[len(osh):]
>>> 
# block them
>>> data_blocked = np.frompyfunc(np.reshape(data, (-1, *ish)).__getitem__, 1, 1)(range(np.prod(osh))).reshape(osh)
>>> 
# admire
>>> data_blocked
array([[array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]]),
        array([[10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]]),
        array([[20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29]])],
       [array([[30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39]]),
        array([[40, 41, 42, 43, 44],
       [45, 46, 47, 48, 49]]),
        array([[50, 51, 52, 53, 54],
       [55, 56, 57, 58, 59]])]], dtype=object)
Run Code Online (Sandbox Code Playgroud)

使用OP的例子,它是一个数组列表的列表:

>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> y = np.array([[7, 8, 9], [0, 1, 2]])
>>> u = np.array([[3, 4, 5], [6, 7, 8]])
>>> v = np.array([[9, 0, 1], [2, 3, 4]])
>>> data = [[x, y], [u, v]]
>>> 
>>> osh = (2,2)
>>> ish = np.shape(data)[len(osh):]
>>> 
>>> data_blocked = np.frompyfunc(np.reshape(data, (-1, *ish)).__getitem__, 1, 1)(range(np.prod(osh))).reshape(osh)
>>> data_blocked
array([[array([[1, 2, 3],
       [4, 5, 6]]),
        array([[7, 8, 9],
       [0, 1, 2]])],
       [array([[3, 4, 5],
       [6, 7, 8]]),
        array([[9, 0, 1],
       [2, 3, 4]])]], dtype=object)
Run Code Online (Sandbox Code Playgroud)

以及具有不同形状子数组的示例(注意v.T):

>>> data = [[x, y], [u, v.T]]
>>> 
>>> osh = (2,2)
>>> ish = np.shape(data)[len(osh):]
>>> data_blocked = np.frompyfunc(np.reshape(data, (-1, *ish)).__getitem__, 1, 1)(range(np.prod(osh))).reshape(osh)>>> data_blocked
array([[array([[1, 2, 3],
       [4, 5, 6]]),
        array([[7, 8, 9],
       [0, 1, 2]])],
       [array([[3, 4, 5],
       [6, 7, 8]]),
        array([[9, 2],
       [0, 3],
       [1, 4]])]], dtype=object)
Run Code Online (Sandbox Code Playgroud)