Python numpy 数组 1.9.2 获取 ValueError:无法将输入数组从形状 (4,2) 广播到形状 (4)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32909619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:28:07  来源:igfitidea点击:

numpy array 1.9.2 getting ValueError: could not broadcast input array from shape (4,2) into shape (4)

pythonnumpy

提问by Manish

Following piece of code was working in numpy 1.7.1 but it is giving value error in the current version. I want to know the root cause of it.

以下代码在 numpy 1.7.1 中工作,但在当前版本中给出值错误。我想知道它的根本原因。

    import numpy as np
    x = [1,2,3,4]
    y = [[1, 2],[2, 3], [1, 2],[2, 3]]

    a = np.array([x, np.array(y)])

Following is the output I get in numpy 1.7.1

以下是我在 numpy 1.7.1 中得到的输出

>>>a
array([[1, 2, 3, 4],
       [array([1, 2]), array([2, 3]), array([1, 2]), array([2, 3])]], dtype=object)

But the same code produces error in version 1.9.2.

但是相同的代码在 1.9.2 版本中会产生错误。

    ----> 5 a = np.array([x, np.array(y)])

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

I have found one possible solution the this. But I don't know whether this is the best thing to do.

我找到了一种可能的解决方案 this. 但我不知道这是否是最好的做法。

b= np.empty(2, dtype=object)
b[:] = [x, np.array(y)]

>>> b
array([[1, 2, 3, 4],
       array([[1, 2],
       [2, 3],
       [1, 2],
       [2, 3]])], dtype=object)

Please suggest a solution to achieve the desired output. Thanks

请提出一个解决方案来实现所需的输出。谢谢

回答by hpaulj

What exactly are you trying to produce? I don't have a 1.7 version to test your example.

你到底想生产什么?我没有 1.7 版本来测试您的示例。

np.array(x)produces a (4,)array. np.array(y)a (4,2).

np.array(x)产生一个(4,)数组。 np.array(y)一个(4,2)

As noted in a comment, in 1.8.1 np.array([x, np.array(y)])produces

如评论中所述,在 1.8.1 中np.array([x, np.array(y)])产生

ValueError: setting an array element with a sequence.

I can make a object dtype array, consisting of the list and the array

我可以创建一个对象 dtype 数组,由列表和数组组成

In [90]: np.array([x, np.array(y)],dtype=object)
Out[90]: 
array([[1, 2, 3, 4],
       [array([1, 2]), array([2, 3]), array([1, 2]), array([2, 3])]], dtype=object)

I can also concatenate 2 arrays to make a (4,3)array (xis the first column)

我也可以连接 2 个数组来创建一个(4,3)数组(x是第一列)

In [92]: np.concatenate([np.array(x)[:,None],np.array(y)],axis=1)
Out[92]: 
array([[1, 1, 2],
       [2, 2, 3],
       [3, 1, 2],
       [4, 2, 3]])

np.column_stack([x,y])does the same thing.

np.column_stack([x,y])做同样的事情。



Curiously in a dev 1.9 (I don't have production 1.9.2 installed) it works (sort of)

奇怪的是在 dev 1.9(我没有安装生产 1.9.2)它可以工作(有点)

In [9]: np.__version__
Out[9]: '1.9.0.dev-Unknown'

In [10]: np.array([x,np.array(y)])
Out[10]: 
array([[        1,         2,         3,         4],
       [174420780, 175084380,  16777603,         0]])
In [11]: np.array([x,np.array(y)],dtype=object)
Out[11]: 
array([[1, 2, 3, 4],
   [None, None, None, None]], dtype=object)
In [16]: np.array([x,y],dtype=object)
Out[16]: 
array([[1, 2, 3, 4],
   [[1, 2], [2, 3], [1, 2], [2, 3]]], dtype=object)

So it looks like there is some sort of development going on.

所以看起来正在进行某种开发。

In any case making a new array from this list and a 2d array is ambiguous. Use column_stack(assuming you want a 2d int array).

无论如何,从这个列表中创建一个新数组和一个二维数组是不明确的。使用column_stack(假设您想要一个 2d int 数组)。



numpy 1.9.0 release notes:

numpy 1.9.0 发行说明:

The performance of converting lists containing arrays to arrays using np.array has been improved. It is now equivalent in speed to np.vstack(list).

使用 np.array 将包含数组的列表转换为数组的性能已得到改进。现在它的速度与 np.vstack(list) 相同。

With transposed yvstackworks:

与转置yvstack作品:

In [125]: np.vstack([[1,2,3,4],np.array([[1,2],[2,3],[1,2],[2,3]]).T])
Out[125]: 
array([[1, 2, 3, 4],
       [1, 2, 1, 2],
       [2, 3, 2, 3]])

If 1.7.1 worked, and xwas string names, not just ints as in your example, then it probably was producing a object array.

如果 1.7.1 有效,并且x是字符串名称,而不仅仅是您的示例中的整数,那么它可能正在生成一个对象数组。