强制 numpy 创建对象数组 [英] Force numpy to create array of objects

查看:48
本文介绍了强制 numpy 创建对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

x = np.array([[1, 2, 3], [4, 5, 6]])

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

我试过这个代码:

a = np.array([[x]], dtype=np.object)

但它产生一个形状为 (1, 1, 2, 3) 的数组.

我当然可以:

a = np.zeros(shape=(1, 1), dtype=np.object)a[0, 0] = x

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

[[x, x], [x, x]]

无需在所有索引上运行 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]]

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

<小时>

UPD2

我真的想要一个适用于任意 x, y, u, v 形状的解决方案,不一定完全相同.

解决方案

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

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

# 创建示例# 选择外部形状和内部形状>>>osh, ish = (2, 3), (2, 5)# 总形状>>>tsh = (*osh, *ish)# 制作数据>>>数据 = np.arange(np.prod(tsh)).reshape(tsh)>>># 重新计算内部形状以适应不同的内部形状# 这将返回所有内部形状的共识位>>>ish = np.shape(data)[len(osh):]>>># 阻止他们>>>data_blocked = np.frompyfunc(np.reshape(data, (-1, *ish)).__getitem__, 1, 1)(range(np.prod(osh))).reshape(osh)>>># 钦佩>>>数据阻塞数组([[数组([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]),数组([[10, 11, 12, 13, 14],[15, 16, 17, 18, 19]]),数组([[20, 21, 22, 23, 24],[25, 26, 27, 28, 29]])],[数组([[30, 31, 32, 33, 34],[35, 36, 37, 38, 39]]),数组([[40, 41, 42, 43, 44],[45, 46, 47, 48, 49]]),数组([[50, 51, 52, 53, 54],[55, 56, 57, 58, 59]])]], dtype=object)

使用 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]])>>>数据 = [[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)>>>数据阻塞数组([[数组([[1, 2, 3],[4, 5, 6]]),数组([[7, 8, 9],[0, 1, 2]])],[数组([[3, 4, 5],[6, 7, 8]]),数组([[9, 0, 1],[2, 3, 4]])]], dtype=object)

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

<预><代码>>>>数据 = [[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)>>>数据阻塞数组([[数组([[1, 2, 3],[4, 5, 6]]),数组([[7, 8, 9],[0, 1, 2]])],[数组([[3, 4, 5],[6, 7, 8]]),数组([[9, 2],[0, 3],[1, 4]])]], dtype=object)

I have an array:

x = np.array([[1, 2, 3], [4, 5, 6]])

and I want to create another array of shape=(1, 1) and dtype=np.object whose only element is x.

I've tried this code:

a = np.array([[x]], dtype=np.object)

but it produces an array of shape (1, 1, 2, 3).

Of course I can do:

a = np.zeros(shape=(1, 1), dtype=np.object)
a[0, 0] = x

but I want the solution to be easily scalable to greater a shapes, like:

[[x, x], [x, x]]

without having to run for loops over all the indices.

Any suggestions how this could be achieved?


UPD1

The arrays may be different, as in:

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]]

They may also be of different shapes, but for that case a simple np.array([[x, y], [u, v]]) constructor works fine


UPD2

I really want a solution that works with arbitrary x, y, u, v shapes, not necessarily all the same.

解决方案

Here is a pretty general method: It works with nested lists, lists of lists of arrays - regardless of whether the shapes of these arrays are different or equal. It also works when the data come clumped together in one single array, which is in fact the trickiest case. (Other methods posted so far will not work in this case.)

Let's start with the difficult case, one big array:

# 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)

Using OP's example which is a list of lists of arrays:

>>> 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)

And an example with different shape subarrays (note the 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)

这篇关于强制 numpy 创建对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆