【问题标题】:Interpolation out of the range in PythonPython中的插值超出范围
【发布时间】:2020-08-31 23:01:20
【问题描述】:

我正在为一些列表做插值。

我的一些数据超出了范围,我该如何做一个简单的限制来解决这个问题?

例如,我的列表之一如下所示:

testList = [[(0.0, -0.9960135495794032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]]

我想进行插值以找到 (x, y=0) 和 (x, y=-1)。

我知道左侧部分 [(0.0, -0.9960135495794032), (0.5, -1.0)] 超出了 (x, y=0) 的范围。

如何编辑我的代码以使列表超出范围的结果直接等于 (x= 0, y=0)?

这是我的代码:

from scipy import interpolate

res_j = []
for j in range(0, 2, 1):
    res_k = []
    for k in range(0, 2, 1): 
        testList = [
                    [[(0.0, -0.9960135495794032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]],
                    [[(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0)], [(5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)]],
                    ]

        x = [c[0] for c in testList[j][k]]
        y = [c[1] for c in testList[j][k]]
        # Enter y to find x: f(y) = x
        f = interpolate.interp1d(y, x)
        interpolate_result = (f(0), f(-1))
        res_k.append(interpolate_result)
    res_j.append(res_k)

print(res_j)

我试图得到如下结果:

[[(array(0), array(0.5)), (array(2.06474439), array(0.5))], [(array(4.57078944), array(5.)), (array(5.37357663), array(5.))]]

【问题讨论】:

  • testList 的结构是否有充分的理由?因为它让事情变得比他们需要的更复杂。
  • @mapf 我不确定是否有原因,这只是我得到的一个数据集。我正在考虑直接将 tuple: (0, 0) 添加到列表中,而列表中没有 (x, y>0)。如果可行,您能否给我一个有关如何执行此操作的提示?

标签: python list tuples


【解决方案1】:

对于这类问题,我一般建议第一步是查看您正在使用的方法/类/函数/等的文档。在这种情况下,the documentation for scipy.interpolate.interp1d 告诉我们所有我们需要知道的信息:

bounds_error:bool,可选

如果为 True,则在任何时候尝试对 x 范围之外的值进行插值时都会引发 ValueError(需要外插)。如果为 False,则为越界值分配 fill_value。默认情况下,除非fill_value="extrapolate",否则会引发错误。

和:

fill_value:类数组或(类数组,类数组)或“外推”,可选

  • 如果是 ndarray(或浮点数),此值将用于填充数据范围之外的请求点。如果未提供,则默认值为 NaN。类数组必须正确广播到非插值轴的维度。

由于您希望值范围之外的值默认为0,这就是您所需要的:

from scipy import interpolate

testList = [
    [
        [(0.0, -0.9960135495794032), (0.5, -1.0)],
        [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]
    ],
    [
        [(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0)],
        [(5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)]
    ],
]

results = []
for subset in testList:
    sub_result = []
    for dataset in subset:
        x = [coord[0] for coord in dataset]
        y = [coord[1] for coord in dataset]
        # Enter y to find x: f(y) = x
        f = interpolate.interp1d(y, x, bounds_error=False, fill_value=0)
        interpolate_result = (f(0), f(-1))
        sub_result.append(interpolate_result)
    results.append(sub_result)

print(results)

如果由于某种原因,文档没有告诉您要查找的内容,在许多情况下,只需在 Internet 上搜索错误代码,就会产生有用的结果,因为很有可能,其他人已经询问过类似的问题。

在您的情况下,搜索 ValueError: A value in x_new is above the interpolation range. 时,this 是第一个结果。这通常意味着比自己提出问题要少得多(对所有相关人员而言)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 2011-08-02
    • 2021-01-01
    相关资源
    最近更新 更多