Python ValueError:使用卷积时对象对于所需数组来说太深了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15923081/
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
ValueError: object too deep for desired array while using convolution
提问by Olivier_s_j
I'm trying to do this:
我正在尝试这样做:
h = [0.2,0.2,0.2,0.2,0.2]
Y = np.convolve(Y, h, "same")
Ylooks like this:
Y看起来像这样:


While doing this I get this error:
这样做时,我收到此错误:
ValueError: object too deep for desired array
Why is this?
为什么是这样?
My guess is because somehow the convolvefunction does not see Yas a 1D array.
我的猜测是因为不知何故,该convolve函数并未将其Y视为一维数组。
采纳答案by user4815162342
The Yarray in your screenshot is not a 1D array, it's a 2D array with 300 rows and 1 column, as indicated by its shapebeing (300, 1).
在Y你的屏幕截图阵列不是一维数组,它是一个二维数组与300行1列,其指示shape的存在(300, 1)。
To remove the extra dimension, you can slice the array as Y[:, 0]. To generally convert an n-dimensional array to 1D, you can use np.reshape(a, a.size).
要删除额外的维度,您可以将数组切片为Y[:, 0]。通常要将 n 维数组转换为 1D,您可以使用np.reshape(a, a.size).
Another option for converting a 2D array into 1D is flatten()function from numpy.ndarraymodule, with the difference that it makes a copy of the array.
将 2D 数组转换为 1D 的另一个选项是模块中的flatten()函数numpy.ndarray,不同之处在于它制作了数组的副本。
回答by Sandip Kumar
np.convolve()takes one dimension array. You need to check the input and convert it into 1D.
np.convolve()接受一维数组。您需要检查输入并将其转换为一维。
You can use the np.ravel(), to convert the array to one dimension.
您可以使用np.ravel(), 将数组转换为一维。

