我试图使用numpy.histogramdd生成两个2D直方图(我知道histogram2d,但我最终需要这个直尺到N维度)
两个直方图应该使用相同的范围,所以我在获得它们之前定义它。
问题在于我无法使用我的代码,使用不同的配置会出现ValueError: too many values to unpack
或ValueError: sequence too large; must be smaller than 32
错误。
这是MWE:
import numpy as np
def rand_data(N):
return np.random.uniform(low=1., high=2000., size=(N,))
# Some random 2D data.
N = 100
P = [rand_data(N), rand_data(N)]
Q = [rand_data(N), rand_data(N)]
# Number of bins.
b = np.sqrt(len(P[0])) * 2
# Max and min values for x and y
x_min = np.sort(np.minimum(P[0], Q[0]))[0]
x_max = np.sort(np.minimum(P[0], Q[0]))[-1]
y_min = np.sort(np.minimum(P[1], Q[1]))[0]
y_max = np.sort(np.minimum(P[1], Q[1]))[-1]
# Range for the histograms.
rang = [np.linspace(x_min, x_max, b), np.linspace(y_min, y_max, b)]
# Histograms
d_1 = np.histogramdd(zip(*[P[0], P[1]]), range=rang)[0]
d_2 = np.histogramdd(zip(*[Q[0], Q[1]]), range=rang)[0]
我做错了什么?
答案 0 :(得分:1)
以下代码应该适合您。有两个问题:bin的边缘传递给bins
参数,而不传递给range
参数。此外,传递元组列表似乎不起作用。如果将这些元组转换为numpy数组并传递数组,它应该按预期工作。
此代码适用于我:
import numpy as np
def rand_data(N):
return np.random.uniform(low=1., high=2000., size=(N,))
# Some random 2D data.
N = 100
P = [rand_data(N), rand_data(N)]
Q = [rand_data(N), rand_data(N)]
# Number of bins.
b = np.sqrt(len(P[0])) * 2
# Max and min values for x and y
x_min = np.sort(np.minimum(P[0], Q[0]))[0]
x_max = np.sort(np.minimum(P[0], Q[0]))[-1]
y_min = np.sort(np.minimum(P[1], Q[1]))[0]
y_max = np.sort(np.minimum(P[1], Q[1]))[-1]
# Range for the histograms.
rang = [np.linspace(x_min, x_max, b), np.linspace(y_min, y_max, b)]
# Histograms
sample1 = np.array(list(zip(*[P[0], P[1]])))
sample2 = np.array(list(zip(*[Q[0], Q[1]])))
d_1 = np.histogramdd(sample1, bins=rang)[0]
d_2 = np.histogramdd(sample2, bins=rang)[0]