Обобщение numpy гистограммы2d с двумерной переменной веса - PullRequest
0 голосов
/ 25 февраля 2020

Этот вопрос является обобщением другого вопроса , задаваемого несколько лет go.

Я пытаюсь создать 2-мерную гистограмму из трех numpy массивов. Каждый массив 1D x и y предоставляет оси x и y, а третий массив предоставляет счетчики для каждого заданного значения c x, y bin в диапазоне (57,101), (57,102) ... (59,102), (59,103) .

Мои три массива выглядят так:

x_axis = np.array([57, 58, 59])

y_axis = np.array([101, 102, 103])

counts = np.array([5, 8, 9],
                  [2, 7, 3],
                  [4, 6, 1])

Если я подключу эти массивы к следующему коду из histogram2d документации

H, xedges, yedges =np.histogram2d(x_axis, y_axis, bins=10, weights=counts) 

, я получу следующее ошибка, которая, как мне кажется, происходит из-за массива двумерных счетчиков:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-478-3bb43d2d52c3> in <module>
----> 1 H, xedges, yedges =np.histogram2d(x_axis, y_axis, bins=10, weights=counts)
      2 extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
      3 plt.imshow(H, extent=extent, interpolation='nearest')

<__array_function__ internals> in histogram2d(*args, **kwargs)

~/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/twodim_base.py in histogram2d(x, y, bins, range, normed, weights, density)
    713         xedges = yedges = asarray(bins)
    714         bins = [xedges, yedges]
--> 715     hist, edges = histogramdd([x, y], bins, range, normed, weights, density)
    716     return hist, edges[0], edges[1]
    717 

<__array_function__ internals> in histogramdd(*args, **kwargs)

~/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/histograms.py in histogramdd(sample, bins, range, normed, weights, density)
   1077     # Compute the number of repetitions in xy and assign it to the
   1078     # flattened histmat.
-> 1079     hist = np.bincount(xy, weights, minlength=nbin.prod())
   1080 
   1081     # Shape into a proper matrix

<__array_function__ internals> in bincount(*args, **kwargs)

ValueError: object too deep for desired array


Возможно, это необходимо обобщить для функции np.histogramdd, но я не уверен, как реализовать что.

...