numpy数组1.9.2得到ValueError:无法将形状(4,2)的输入数组广播为形状(4)

Man*_*ish 13 python numpy

以下代码片段在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)])
Run Code Online (Sandbox Code Playgroud)

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

>>>a
array([[1, 2, 3, 4],
       [array([1, 2]), array([2, 3]), array([1, 2]), array([2, 3])]], dtype=object)
Run Code Online (Sandbox Code Playgroud)

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

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

ValueError: could not broadcast input array from shape (4,2) into shape (4) 
Run Code Online (Sandbox Code Playgroud)

我找到了一个可能的解决方案.但我不知道这是否是最好的事情.

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)
Run Code Online (Sandbox Code Playgroud)

请建议一个解决方案,以实现所需的输出.谢谢

hpa*_*ulj 3

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

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

正如评论中指出的,在 1.8.1 中np.array([x, np.array(y)])产生

ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

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

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]])
Run Code Online (Sandbox Code Playgroud)

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


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

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)
Run Code Online (Sandbox Code Playgroud)

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


numpy 1.9.0 release notes:

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

With transposed y vstack works:

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]])
Run Code Online (Sandbox Code Playgroud)

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