Python 在numpy.historogramdd方面有问题
我试图从具有复杂值的2D数组创建N维直方图。我想计算给定容器的数组实部和虚部的出现次数,并将结果存储在3D数组中。当我硬编码I=0并删除for循环时,它只在第一次迭代中运行。我以前从未在python中使用过直方图,我无法理解错误。代码如下所示 xsoft被定义为复杂类型的2d数组,我通过从xsoft中查找max,min值来计算bnd_边,并创建边作为容器Python 在numpy.historogramdd方面有问题,python,numpy,Python,Numpy,我试图从具有复杂值的2D数组创建N维直方图。我想计算给定容器的数组实部和虚部的出现次数,并将结果存储在3D数组中。当我硬编码I=0并删除for循环时,它只在第一次迭代中运行。我以前从未在python中使用过直方图,我无法理解错误。代码如下所示 xsoft被定义为复杂类型的2d数组,我通过从xsoft中查找max,min值来计算bnd_边,并创建边作为容器 xsoft = np.empty((M, MAX,), dtype=complex) # e.g has dims 4*100 xsoft[:
xsoft = np.empty((M, MAX,), dtype=complex) # e.g has dims 4*100
xsoft[:] = np.nan
edges = np.linspace(-bnd_edges, bnd_edges, numbin) #numbin=10
pSOFT = np.empty((len(edges)-1, M, len(edges)-1)) # len(edges)= 10
pSOFT[:] = np.nan
for i in range(M):
pSOFT[:, i, :], edges = np.histogramdd((xsoft[i, :].real, xsoft[i, :].imag), bins=(edges, edges))
该代码导致以下错误
Traceback (most recent call last):
File " ", line 194, in <module>
pSOFT[:, i, :], edges = np.histogramdd((xsoft[i, :].real, xsoft[i, :].imag), bins=(edges, edges))
File "<__array_function__ internals>", line 5, in histogramdd
File " " line 1066, in histogramdd
raise ValueError(
ValueError: `bins[0]` must be a scalar or 1d array
Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“”,第194行,在
pSOFT[:,i,:],edges=np.historogramdd((xsoft[i,:]real,xsoft[i,:]imag),bin=(edges,edges))
Historogramdd中第5行的文件“”
文件“”第1066行,在Historogramdd中
升值误差(
ValueError:`bins[0]`必须是标量或1d数组
进程已完成,退出代码为1
之所以出现此错误,是因为您正在使用第二个返回值historogramdd
覆盖边的原始定义。
将代码中的最后一行替换为:
pSOFT[:, i, :], edges_i = np.histogramdd((xsoft[i, :].real, xsoft[i, :].imag), bins=(edges, edges))
什么是类型(bnd_edges)
?它是标量(例如float类型)?是的,它是一个带有float类型的标量(例如1.296**)哦,是的,这是一个非常严重的错误。两条边的名称不应该是相同的。谢谢你指出。@Alveena没关系。我刚刚注意到我写的是“覆盖”而不是“覆盖”在我的回答中,我也犯了一个蹩脚的错误:)