das*_*uki 6 python interpolation
我有20多个类似于表1的表.其中所有字母代表实际值.
Table 1:
$ / cars |<1 | 2 | 3 | 4+
<10,000 | a | b | c | d
20,000 | e | f | g | h
30,000 | i | j | k | l
40,000+ | m | n | o | p
Run Code Online (Sandbox Code Playgroud)
用户输入可以是例如(2.4,24594),其是f,g,j和k之间的值.我的Python函数定义和伪代码来计算这个双线性插值如下.
def bilinear_interpolation( x_in, y_in, x_high, x_low, y_low, y_high ):
# interpolate with respect to x
# interpolate with respect to y
# return result
Run Code Online (Sandbox Code Playgroud)
我应该如何存储表1中的数据(文件,字典,元组的元组或列表的字典),这样我才能最有效和正确地执行双线性插值?
如果你想要我能想到的计算效率最高的解决方案,并且不仅限于标准库,那么我会推荐scipy/numpy.首先,将a..p数组存储为2D numpy数组,然后将$ 4k-10k和1-4数组存储为1D numpy数组.如果两个1D数组单调增加,则使用scipy的interpolate.interp1d;如果不是,则使用interpolate.bsplrep(双变量样条表示),并且您的示例数组与示例一样小.或者只是写自己的,而不是scipy.这里有些例子:
# this follows your pseudocode most closely, but it is *not*
# the most efficient since it creates the interpolation
# functions on each call to bilinterp
from scipy import interpolate
import numpy
data = numpy.arange(0., 16.).reshape((4,4)) #2D array
prices = numpy.arange(10000., 50000., 10000.)
cars = numpy.arange(1., 5.)
def bilinterp(price,car):
return interpolate.interp1d(cars, interpolate.interp1d(prices, a)(price))(car)
print bilinterp(22000,2)
Run Code Online (Sandbox Code Playgroud)
我最后一次检查(来自2007-ish的scipy版本)它只适用于单调增加x和y的数组)
对于像这个4x4数组这样的小数组,我想你想用这个:http : //docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.bisplrep.html#scipy.interpolate.bisplrep 将处理更有趣的形状表面和功能只需要创建一次.对于较大的数组,我认为你想要这个(不确定它是否与interp1d具有相同的限制):http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp2d.html#scipy . interpolate.interp2d 但它们都需要比上例中的三个数组更不同且更冗长的数据结构.
| 归档时间: |
|
| 查看次数: |
1588 次 |
| 最近记录: |