ValueError:使用卷积时对象对于所需数组而言太深 [英] ValueError: object too deep for desired array while using convolution
问题描述
我正在尝试这样做:
h = [0.2,0.2,0.2,0.2,0.2]
Y = np.convolve(Y, h, "same")
Y
看起来像这样:
执行此操作时出现此错误:
While doing this I get this error:
ValueError: object too deep for desired array
这是为什么?
我的猜测是因为convolve
函数无法将Y
视为一维数组.
My guess is because somehow the convolve
function does not see Y
as a 1D array.
推荐答案
屏幕截图中的Y
数组不是一维数组,而是具有300行和1列的2D数组,如其shape
所示(300, 1)
.
The Y
array in your screenshot is not a 1D array, it's a 2D array with 300 rows and 1 column, as indicated by its shape
being (300, 1)
.
要删除额外的维,可以将数组切片为Y[:, 0]
.要将n维数组通常转换为1D,可以使用np.reshape(a, a.size)
.
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)
.
将2D数组转换为1D的另一个选项是numpy.ndarray
模块的flatten()
函数,不同之处在于它复制了数组.
Another option for converting a 2D array into 1D is flatten()
function from numpy.ndarray
module, with the difference that it makes a copy of the array.
这篇关于ValueError:使用卷积时对象对于所需数组而言太深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!