将随机数据分组到具有不等轴长度的常规3D网格

时间:2018-04-16 15:52:35

标签: python numpy scipy

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

在下面的例子中,我如何将'vals'(具有根据'position_3d'的随机3D位置)加到'grid_3d'(这是一个常规网格)?

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)]])

1 个答案:

答案 0 :(得分:1)

您可以使用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