numpy中的数组的维数 [英] dimensions of array of arrays in numpy

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

问题描述

我想对锯齿状数组"进行操作,我更喜欢写 "A + A" 代替 "[[x + y表示压缩后的x,y,(A,A)]"

I'd like to operate on "jagged arrays", and I prefer write "A + A" instead of "[x + y for x,y in zipped(A,A)]"

为此,我想将不同大小的数组列表转换为整个numpy数组,但是由于看似过度的广播而遇到错误(请注意前三个成功,但最后一个失败):

For that I'd like to convert list of arrays of different sizes into an overall numpy array, but ran into an error due to seemingly over-zealous broadcasting (notice the first three succeeded, but the last one failed):

In[209]: A = array([ones([3,3]), array([1, 2])])
In[210]: A = array([ones([3,3]), array([1, 2])], dtype=object)
In[211]: A = array([ones([3,2]), array([1, 2])], dtype=object)
In[212]: A = array([ones([2,2]), array([1, 2])], dtype=object)
Traceback (most recent call last):
  File "/home/hzhang/.conda/envs/myenv/lib/python3.4/site-
packages/IPython/core/interactiveshell.py", line 2881, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-212-7297723106f9>", line 1, in <module>
  A = array([ones([2,2]), array([1, 2])], dtype=object)
ValueError: could not broadcast input array from shape (2,2) into shape (2)

帮助?

推荐答案

您的案例是我对第三个案例的回答

Your case is a variant on the 3rd case in my answer to

如何创建不同形状的数组的对象数组时,防止numpy广播

np.array尝试从输入列表中创建数字的多维数组.如果组件的尺寸足够不同,则可以使数组分开,而改为对象数组.我认为这种数组是美化/贬低的列表.

np.array tries to create a multidimensional array of numbers from the input list. If the component dimensions are sufficiently different it resorts to keeping the arrays separate, making an object array instead. I think of this kind of array as a glorified/debased list.

如何存储多个具有不同长度的numpy 1d数组并打印

在您遇到问题的情况下,尺寸足够接近以至于它认为"它可以创建一个2d数组,但是当它开始填充这些值时,它发现它无法广播值,因此抛出错误.有人可能会说它应该回溯并采用对象"阵列路线.但是该决策树深深地埋在了已编译的代码中.

In your problem case, the dimensions are close enough that it 'thinks' it can make a 2d array, but when it starts to fill in those values it finds that it can't broadcast values to do so, and so throws the error. One could argue that it should have backtracked and taken the 'object' array route. But that decision tree is buried deep in compiled code.

早期的SO问题中的问题案例是

The problem case in that earlier SO question was

np.array([np.zeros((2, 2)), np.zeros((2,3))])

第一个尺寸匹配,但第二个不匹配.我不确定您的IN[211]为何起作用,但In[212]为何不起作用.但是错误消息是相同的,直到尝试(2,2)=>(2).

The 1st dimensions match, but the 2nd don't. I'm not entirely sure why your IN[211] works but In[212] does not. But the error message is the same, right down to the (2,2) => (2) attempt.

糟糕-我首先将您的问题示例阅读为:

oops - I first read your problem example as:

np.array([np.ones([2,2]), np.ones([1, 2])], dtype=object)

也就是说,将(2,2)与(1,2)组合在一起,这确实会产生(2,)对象.您实际上所结合的是

That is, combining a (2,2) with (1,2), which does produce a (2,) object. What you are actually combine is a

 (2,2) with a (2,) 

所以看起来目标是np.empty((2,2),float)(或object),因为out[...]=[ones([2,2]), array([1,2])]会产生此错误.

So it looks like the target is np.empty((2,2),float) (or object), because out[...]=[ones([2,2]), array([1,2])] produces this error.

无论如何,创建对象数组的最可靠方法是对其进行初始化,然后复制该数组.

In any case the most reliable way of creating an object array is to initialize it, and copy the arrays.

Out[90]: array([None, None], dtype=object)
In [91]: arr[:]=[ones([2,2]), array([1, 2])]
In [92]: arr
Out[92]: 
array([array([[ 1.,  1.],
       [ 1.,  1.]]), array([1, 2])], dtype=object)


对像这样的对象数组进行数学运算时要小心.起作用的是失败或失败:


Be cautious about doing math on object arrays like this. What works is hit-or-miss:

In [93]: A+A
Out[93]: 
array([array([[ 2.,  2.],
       [ 2.,  2.],
       [ 2.,  2.]]),
       array([2, 4])], dtype=object)

In [96]: np.min(A[1])
Out[96]: 1
In [97]: np.min(A)
....
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [98]: A.sum()
Out[98]: 
array([[ 2.,  3.],
       [ 2.,  3.],
       [ 2.,  3.]])

这有效,因为A[0]+A[1]有效. A[1]是(2,),广播到(3,2).

this works because A[0]+A[1] works. A[1] is (2,) which broadcasts to (3,2).

使用对象数组numpy求助于某种列表理解,遍历对象元素.这样可能会带来数组表示法的便利,但是速度与真正的2d数组的速度不一样.

With object arrays numpy resorts to some sort of list comprehension, iterating over the object elements. So may get the convenience of array notation, but not the same speed as you would with a true 2d array.

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

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