当第一维的第一大小匹配时,创建数组数组失败 [英] Creation of array of arrays fails, when first size of first dimension matches

查看:58
本文介绍了当第一维的第一大小匹配时,创建数组数组失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不理解 np.array 函数的某些行为。这段代码按我期望的方式工作:

I stumpled upon some behaviour of the np.array function I do not understand. This code works the way I expect it:

arr1 = np.zeros((3,2))
arr2 = np.zeros((2,2))

np.array([arr1,arr2])





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

但是这段代码给了我一个错误:

But this code gives me an error:

arr1 = np.zeros((2,3))
arr2 = np.zeros((2,2))

np.array([arr1,arr2])




ValueError:无法将输入数组从形状(2,3)广播到形状(2)

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

为什么第一维的大小匹配?以及如何强制该功能像第一个示例中那样操作?

Why does it make a difference, whether the size of the first dimension matches? And how can I force the function to behave as it does in the first example?

推荐答案

numpy数组1.9.2出现ValueError:可能没有将输入数组从形状(4,2)转换为形状(4)

对象数组的创建方式已发生变化。 np.array 尽可能创建多维数组。创建对象数组是回退选择,可用于输入形状不匹配的情况。

there have been changes in how object arrays are created. Where possible np.array tries to create multidimensional array. Creating an object array has been the fallback choice, to be used when the inputs don't match in shape. And even then it can be unpredictable.

最可靠的方法是制作一个大小合适的空对象数组并插入对象

The surest way is to make an empty object array of the right size and insert the objects

In [242]: a=np.zeros((2,3));b=np.ones((2,2))

In [243]: arr=np.zeros((2,), object)
In [244]: arr[0]=a; arr[1]=b

In [245]: arr
Out[245]: 
array([array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]]),
       array([[ 1.,  1.],
       [ 1.,  1.]])], dtype=object)
In [246]: arr[:]=[a,b]           # also works

您已经看到 np.array([aT,b])有效。如果将数组转换为嵌套列表,则会得到二维平面列表数组; 2和3元素列表是不兼容对象的最低级别。

You've already seen that np.array([a.T, b]) works. If I turn the arrays into nested lists I get a 2d array of flat lists; 2 and 3 element lists are the lowest level of incompatible objects.

In [250]: np.array([a.tolist(),b.tolist()])
Out[250]: 
array([[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
       [[1.0, 1.0], [1.0, 1.0]]], dtype=object)

In [251]: _.shape
Out[251]: (2, 2)

np.array 已编译,因此需要一些时间挖掘以解码其逻辑。我之前的挖掘表明,进行了一些更改以加快从数组创建数组的速度(更像是连接)。此问题可能是该更改的副作用。

np.array is compiled, so it would take some digging to decode its logic. My earlier digging indicates that the made some changes to speed up the creation of an array from arrays (more like concatenate). This problem may be a side effect of that change.

带回家的消息是:如果要创建对象数组数组,请不要依赖 np.array 做正确的工作。它主要用于制作标量的多维数组。

The take home message is: if you want to create an object array of arrays, don't count on np.array to do the job right. It's designed primarily to make multidimensional arrays of scalars.

例如,如果数组大小匹配:

For example if the arrays match in size:

In [252]: np.array([a,a]).shape
Out[252]: (2, 2, 3)

如果必须使用 arr [...] = [a,a] 我想要2个元素的对象数组。

I have to use arr[...]=[a,a] if I want a 2 element object array.

这篇关于当第一维的第一大小匹配时,创建数组数组失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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