如何使用plpython执行基本的scipy gridddata插值?

2024-06-25 05:48:57 发布

您现在位置:Python中文网/ 问答频道 /正文

无法从plpython documentation examplegriddata example转换到实际工作代码。你知道吗

假设“my_table”有一些简单参考网格的x、y和z值,其中x、y对是点坐标,z是该点的值。输入参数是inxiny,这两个参数定义了一个点坐标,我们要为它插入一个z值。你知道吗

create or replace function somefunc(inx numeric, iny numeric) 
returns numeric as 
$$ 
import numpy as np
import scipy as sp

# get the lookup table
refvals = plpy.execute("SELECT x, y, z FROM my_table")

points = np.ndarray(refvals['x'], refvals['y']) #nope
points = np.ndarray(refvals["x"], refvals["y"]) #nope
# ERROR:  TypeError: list indices must be integers, not str

points = np.array(refvals[:,0], refvals[:,1]) #nope
# ERROR:  TypeError: list indices must be integers, not tuple

zvals = refvals["z"] # nope

return sp.interpolate.griddata(points, zvals, (inx, iny), method="cubic");
$$ LANGUAGE plpython2u stable;

这不起作用,因为我不明白refvals是怎么回事,以及如何将其元素转换为griddata的适当数据类型。我在想r数据帧(我会使用refvals[,c('x','y')]来获得x,y坐标对)或SQL表(我会使用select x, y from refvals),但是refvals对象在python中称为list of dict,我不太了解如何正确访问它。你知道吗

我从文档foo = rv[i]["my_column"]的语法中得知,可能需要循环(例如for i = 0:len)?你知道吗


Tags: import参数examplemyasnptablepoints