【问题标题】:numpy.histogramdd: ValueError: The dimension of bins must be equal to the dimension of the sample xnumpy.histogramdd: ValueError: bins 的维度必须等于样本 x 的维度
【发布时间】:2019-12-06 20:38:25
【问题描述】:

我遇到了一个我无法解释的错误。如果我使用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)

【问题讨论】:

    标签: python numpy histogram


    【解决方案1】:

    根据docs

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

    一定是这样的:

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

    【讨论】:

    • 呃呃呃为什么?谢谢史蒂夫
    • 对我来说也很奇怪,但确实如此,我不知道是什么原因
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2020-07-18
    • 2020-03-15
    • 2022-01-19
    • 2018-02-22
    相关资源
    最近更新 更多