Python : 根据函数值缩小网格
Python : Reduce grid according to the values of a function
我有一个规则的 spaced 网格,假设 200*200*200 = 8,000,000 点。在这个网格的每个点上,我还有一些函数 f 的值列表(它采用正值和负值并且在 space 上变化很大),如下所示:
import numpy as np
from itertools import product
x = np.linspace(0, 200*0.05, 200)
y = np.linspace(0, 200*0.05, 200)
z = np.linspace(0, 200*0.05, 200)
coordinates = np.array(list(product(x, y, z)))
和
In [1]: print(coordinates, coordinates.shape)
[[ 0. 0. 0. ]
[ 0. 0. 0.05025126]
[ 0. 0. 0.10050251]
...,
[ 10. 10. 9.89949749]
[ 10. 10. 9.94974874]
[ 10. 10. 10. ]]
(8000000, 3)
In [2]: print(f,"\n",f.shape)
[ 2.46143000e-08 3.01043000e-08 3.64817000e-08 ..., 6.79642000e-08
5.83957000e-08 4.95127000e-08]
(8000000,)
In [3]: print(np.max(f), np.min(f), np.min(np.absolute(f)))
6.21966 -271.035 1.10296e-09
如何获得点数较少的新网格(~250,000 点),这在 f 高的区域非常精确 值,在低 f 值的区域更不精确 ?
这个新网格可以是规则的,但也可以更复杂,只要我之后仍然可以在 space 上集成该功能。
预先感谢您的帮助!
编辑:我刚刚发现了 scipy.interpolate.griddata 函数,如果我想办法制作一个新网格,即使这个网格不规则,它也会非常有用。是否有生成网格的 python 库?
受此 的启发,我最终使用了以下代码,并定义了 f 的概率密度:
n = 250000
g = 2 #the higher g, the more precise the grid will be in regions of high f, and vice-versa
x = np.linspace(0, 200*0.05, 200)
y = np.linspace(0, 200*0.05, 200)
z = np.linspace(0, 200*0.05, 200)
[x_grid,y_grid,z_grid] = np.meshgrid(x,y,z)
xi,yi,zi = x_grid.ravel(),y_grid.ravel(),z_grid.ravel()
#create normalized pdf
pdf = np.log10(np.absolute(f))
pdf = pdf - pdf.min() + 1
pdf = pdf**g
pdf = pdf/np.sum(pdf)
#obtain indices of randomly selected points, as specified by pdf:
randices = np.random.choice(np.arange(x_grid.size), n, replace = False,p = pdf.ravel())
#random positions:
x_rand = xi[randices]
y_rand = yi[randices]
z_rand = zi[randices]
#coordinates
grid_coord = np.array([x_rand, y_rand, z_rand]).swapaxes(0,1)
我有一个规则的 spaced 网格,假设 200*200*200 = 8,000,000 点。在这个网格的每个点上,我还有一些函数 f 的值列表(它采用正值和负值并且在 space 上变化很大),如下所示:
import numpy as np
from itertools import product
x = np.linspace(0, 200*0.05, 200)
y = np.linspace(0, 200*0.05, 200)
z = np.linspace(0, 200*0.05, 200)
coordinates = np.array(list(product(x, y, z)))
和
In [1]: print(coordinates, coordinates.shape)
[[ 0. 0. 0. ]
[ 0. 0. 0.05025126]
[ 0. 0. 0.10050251]
...,
[ 10. 10. 9.89949749]
[ 10. 10. 9.94974874]
[ 10. 10. 10. ]]
(8000000, 3)
In [2]: print(f,"\n",f.shape)
[ 2.46143000e-08 3.01043000e-08 3.64817000e-08 ..., 6.79642000e-08
5.83957000e-08 4.95127000e-08]
(8000000,)
In [3]: print(np.max(f), np.min(f), np.min(np.absolute(f)))
6.21966 -271.035 1.10296e-09
如何获得点数较少的新网格(~250,000 点),这在 f 高的区域非常精确 值,在低 f 值的区域更不精确 ?
这个新网格可以是规则的,但也可以更复杂,只要我之后仍然可以在 space 上集成该功能。 预先感谢您的帮助!
编辑:我刚刚发现了 scipy.interpolate.griddata 函数,如果我想办法制作一个新网格,即使这个网格不规则,它也会非常有用。是否有生成网格的 python 库?
受此
n = 250000
g = 2 #the higher g, the more precise the grid will be in regions of high f, and vice-versa
x = np.linspace(0, 200*0.05, 200)
y = np.linspace(0, 200*0.05, 200)
z = np.linspace(0, 200*0.05, 200)
[x_grid,y_grid,z_grid] = np.meshgrid(x,y,z)
xi,yi,zi = x_grid.ravel(),y_grid.ravel(),z_grid.ravel()
#create normalized pdf
pdf = np.log10(np.absolute(f))
pdf = pdf - pdf.min() + 1
pdf = pdf**g
pdf = pdf/np.sum(pdf)
#obtain indices of randomly selected points, as specified by pdf:
randices = np.random.choice(np.arange(x_grid.size), n, replace = False,p = pdf.ravel())
#random positions:
x_rand = xi[randices]
y_rand = yi[randices]
z_rand = zi[randices]
#coordinates
grid_coord = np.array([x_rand, y_rand, z_rand]).swapaxes(0,1)