gpt4 book ai didi

python - 将随机数据分箱至轴长度不等的规则 3D 网格

转载 作者:行者123 更新时间:2023-12-01 09:32:17 25 4
gpt4 key购买 nike

我在很长一段时间内使用卫星检索。每次检索都是二维的(即水平距离乘垂直距离),但是每个足迹都有 x 和 y 分量。因此,在考虑了所有检索之后,我获得了 3 个维度的随机点的数据。有没有一种简单的方法可以将数据分入具有不等轴长度(例如 100 x 400 x 50)的常规 3D 网格,以便我可以获得频率计数、计算统计数据等?

在下面的示例中,如何根据“position_3d”到“grid_3d”(常规网格)对具有随机 3D 位置的“vals”进行分箱?

import numpy as np
from random import uniform

lon0 = -130
lon1 = -115
lat0 = 30
lat1 = 45
z0 = 0
z1 = 2500
val0 = 0
val1 = 1.5
n = 1000

# 3D location of each data point
position_3d = np.array([[uniform(lon0,lon1) for p in range(0,n)],[uniform(lat0,lat1) for p in range(0,n)],[uniform(z0,z1) for p in range(0,n)]])

# Value at each 3D location
vals = np.array([uniform(val0,val1) for p in range(0,n)])

# Regular 3D grid to which I want to interpolate
grid_3d = np.array([[np.arange(lon0,lon1,0.1)],[np.arange(lat0,lat1,0.1)],[np.arange(z0,z1,100)]])

最佳答案

您可以使用 np.histogramdd 将值累积到容器中:

import numpy as np

lon0 = -130
lon1 = -115
lat0 = 30
lat1 = 45
z0 = 0
z1 = 250
val0 = 0
val1 = 1.5
n = 20

np.random.seed(100)

# 3D location of each data point
position_3d = np.array([[np.random.uniform(lon0,lon1) for p in range(0,n)],
[np.random.uniform(lat0,lat1) for p in range(0,n)],
[np.random.uniform(z0,z1) for p in range(0,n)]])

# Value at each 3D location
vals = np.array([np.random.uniform(val0,val1) for p in range(0,n)])

# Regular 3D grid to which I want to interpolate
grid_3d = [np.arange(lon0, lon1, 1.),
np.arange(lat0, lat1, 1.),
np.arange(z0, z1, 50.)]

h, _ = np.histogramdd(position_3d.T, [np.append(ax, np.inf) for ax in grid_3d],
weights=vals)

# Print non-zero values
coords = np.nonzero(h)
for i, j, k in zip(*coords):
print(f'({grid_3d[0][i]}, {grid_3d[1][j]}, {grid_3d[2][k]}): {h[i][j][k]}')

输出:

(-130.0, 32.0, 50.0): 1.4007619491996286
(-129.0, 35.0, 100.0): 2.869484362944637
(-128.0, 30.0, 50.0): 0.5105778342561542
(-128.0, 31.0, 50.0): 0.2604120026103076
(-128.0, 41.0, 50.0): 0.5447820061640247
(-127.0, 30.0, 50.0): 1.435518900529197
(-127.0, 39.0, 200.0): 0.36980382180532445
(-126.0, 38.0, 100.0): 0.6952470284057215
(-126.0, 44.0, 150.0): 0.88920810146383
(-124.0, 42.0, 100.0): 0.9449128133812461
(-122.0, 30.0, 200.0): 0.30651791530296635
(-122.0, 36.0, 150.0): 0.5643786814460445
(-120.0, 30.0, 150.0): 0.9034449865962983
(-118.0, 33.0, 50.0): 0.5816494204899461
(-118.0, 35.0, 0.0): 0.21390047166942527
(-118.0, 43.0, 0.0): 0.13808340506585792
(-118.0, 44.0, 0.0): 1.096951129589884
(-117.0, 38.0, 200.0): 0.41514759209450275
(-116.0, 43.0, 50.0): 0.8969605264933811

关于python - 将随机数据分箱至轴长度不等的规则 3D 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49861468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com