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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:22:56  来源:igfitidea点击:

ValueError: object too deep for desired array while using convolution

pythonnumpyconvolutionvalueerror

提问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看起来像这样:

screenshot

截屏

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(), 将数组转换为一维。

回答by mohitR0_0

np.convolveneeds a flattened array as one of it's inputs, you can use numpy.ndarray.flatten()which is quite fast, find it here.

np.convolve需要一个扁平数组作为它的输入之一,您可以使用numpy.ndarray.flatten()它非常快,在这里找到它。

回答by jelde015

You could try using scipy.ndimage.convolveit allows convolution of multidimensional images. hereis the docs

您可以尝试使用scipy.ndimage.convolve它允许多维图像的卷积。是文档