numpy.histogramdd:ValueError:箱的尺寸必须等于样本x的尺寸

时间:2019-12-06 20:38:25

标签: python numpy histogram

我遇到了一个我无法解释的错误。如果我将numpy.histogramdd与列表一起用作输入,则可以使用,但是如果我将转换为numpy数组的相同列表馈入它,则会失败:

ValueError: The dimension of bins must be equal to the dimension of the  sample x.

为什么会这样?我正在使用Python 3.7.5, numpy 1.17.3

import numpy as np

bin_edges = np.array([
    [10.08, 11.25454545, 12.42909091, 13.60363636, 14.77818182,
     15.95272727, 17.12727273, 18.30181818, 19.47636364, 20.65090909,
     21.82545455, 23.],
    [-0.03, 0.18, 0.39, 0.6, 0.81, 1.02, 1.23, 1.44, 1.65, 1.86],
    [-0.54, -0.27222222, -0.00444444, 0.26333333, 0.53111111, 0.79888889,
     1.06666667, 1.33444444, 1.60222222, 1.87]])

arr = [[17.10827794, 11.83391358, 12.83095133, 22.95133446, 10.56728914,
        10.79192481, 16.44720125, 15.6446372, 20.89509702, 11.20721727],
       [15.45189, 22.95207101, 19.59385685, 23.25150156, 11.46710741,
        23.48013989, 10.77612015, 12.9927327, 21.71852366, 22.60270056],
       [22.94057217, 18.50618166, 13.21763666, 10.82516534, 17.54735419,
        13.48076389, 17.82794113, 14.31666433, 11.71345638, 16.17708158]]

# This works
hst = np.histogramdd(arr, bins=bin_edges)

# This fails
arr = np.array(arr)
hst = np.histogramdd(arr, bins=bin_edges)

1 个答案:

答案 0 :(得分:1)

按照docs

  

sample:(N,D)个数组,或(D,N)array_like

因此必须是:

arr = np.array(arr)
hst = np.histogramdd(arr.T, bins=bin_edges)