使用圆形而不是矩形对二维数据进行分箱 - 来自 pandas df

Binning 2D data with circles instead of rectangles - from pandas df

我有一个包含 x、y 数据的数据框,需要将其装箱成圆圈。即以某个点为中心的一定大小和间距的圆网格。因此,例如,在此 sampling/binning 之后会遗漏一些数据。这怎么可能?

我已经尝试 np.histogram2d 并创建了 masks/broadcasting。蒙版太慢了,我好像播不成圈。仅通过此答案判断该点是否在所述圆圈网格内:Binning 2D data into overlapping circles in x,y.

如果有办法将边缘或其他东西输入 histogram2d 并使边缘成为圆形,请告诉我。干杯

唯一可以做到这一点的方法是像这样遍历你的点和圆网格:

def inside_circle(x, y, x0, y0, r):
    return (x - x0)*(x - x0) + (y - y0)*(y - y0) < r*r


x_bins = np.linspace(-9, 9, 30)
y_bins = np.linspace(-9, 9, 30)

h = df['upmm']
w = df['anode_entrance']

histo = np.zeros((32,32))

for i in range(0, len(h)):
    for j in range(0, len(x_bins)):
        for k in range(0, len(y_bins)):
            if inside_circle(h[i], w[i], x_bins[j], y_bins[k], 0.01):
                histo[j][k] = histo[j][k] + 1
            
plt.imshow(histo, cmap='hot', interpolation='nearest')
plt.show()