numpy array 1.9.2获取ValueError:无法将输入数组从形状(4,2)广播到形状(4) [英] numpy array 1.9.2 getting ValueError: could not broadcast input array from shape (4,2) into shape (4)
问题描述
以下代码在numpy 1.7.1中工作,但在当前版本中给出了值错误.我想知道它的根本原因.
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.
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)])
以下是我在numpy 1.7.1中得到的输出
Following is the output I get in numpy 1.7.1
>>>a
array([[1, 2, 3, 4],
[array([1, 2]), array([2, 3]), array([1, 2]), array([2, 3])]], dtype=object)
但是相同的代码在1.9.2版中会产生错误.
But the same code produces error in version 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.
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
推荐答案
您到底想产生什么?我没有1.7版本可以测试您的示例.
What exactly are you trying to produce? I don't have a 1.7 version to test your example.
np.array(x)
产生一个(4,)
数组. np.array(y)
和(4,2)
.
np.array(x)
produces a (4,)
array. np.array(y)
a (4,2)
.
如注释中所述,在1.8.1中np.array([x, np.array(y)])
产生
As noted in a comment, in 1.8.1 np.array([x, np.array(y)])
produces
ValueError: setting an array element with a sequence.
我可以创建一个由列表和数组组成的对象dtype数组
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)
我还可以将2个数组连接成一个(4,3)
数组(x
是第一列)
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]])
np.column_stack([x,y])
做同样的事情.
奇怪的是,在1.9开发版(我没有安装生产版1.9.2)中,它可以工作(有点)
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)
因此,似乎正在进行某种开发.
So it looks like there is some sort of development going on.
无论如何,从该列表中创建一个新数组和2d数组是模棱两可的.使用column_stack
(假设您要使用2d int数组).
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发行说明:
numpy 1.9.0 release notes:
使用np.array将包含数组的列表转换为数组的性能已得到改进.现在它的速度相当于np.vstack(list).
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).
换位后的y
vstack
起作用:
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]])
如果1.7.1有效,并且x
是字符串名称,而不仅仅是示例中的整数,则可能是在生成对象数组.
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.
这篇关于numpy array 1.9.2获取ValueError:无法将输入数组从形状(4,2)广播到形状(4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!