python - 尝试在 python 中进行线性插值

标签 python scipy interpolation

我有 3 个数组:a、b、c,长度均为 15。

a=[950, 850, 750, 675, 600, 525, 460, 400, 350, 300, 250, 225, 200, 175, 150] 

b = [16, 12, 9, -35, -40, -40, -40, -45, -50, -55, -60, -65, -70, -75, -80]

c=[32.0, 22.2, 12.399999999999999, 2.599999999999998, -7.200000000000003, -17.0, -26.800000000000004, -36.60000000000001, -46.400000000000006, -56.2, -66.0, -75.80000000000001, -85.60000000000001, -95.4, -105.20000000000002] 

我试图在 b=c 的索引处找到 a 的值。

问题是没有地方 b=c,所以我需要在数组中的值之间进行线性插值,以找到 b=c 时 a 的值。那有意义吗?

我正在考虑使用 scipy.interpolate 进行插值。

我很难思考如何解决这个问题。对此有任何想法都很棒!

最佳答案

这是来自 another answer of mine 的函数的更简单变体:

from __future__ import division

import numpy as np


def find_roots(t, y):
    """
    Given the input signal `y` with samples at times `t`,
    find the times where `y` is 0.

    `t` and `y` must be 1-D numpy arrays.

    Linear interpolation is used to estimate the time `t` between
    samples at which sign changes in `y` occur.
    """
    # Find where y crosses 0.
    transition_indices = np.where(np.sign(y[1:]) != np.sign(y[:-1]))[0]

    # Linearly interpolate the time values where the transition occurs.
    t0 = t[transition_indices]
    t1 = t[transition_indices + 1]
    y0 = y[transition_indices]
    y1 = y[transition_indices + 1]
    slope = (y1 - y0) / (t1 - t0)
    transition_times = t0 - y0/slope

    return transition_times

该函数可以与 t = ay = b - c 一起使用。例如,这是您的数据,以 numpy 数组形式输入:

In [354]: a = np.array([950, 850, 750, 675, 600, 525, 460, 400, 350, 300, 250, 225, 200, 175, 150])

In [355]: b = np.array([16, 12, 9, -35, -40, -40, -40, -45, -50, -55, -60, -65, -70, -75, -80])

In [356]: c = np.array([32.0, 22.2, 12.399999999999999, 2.599999999999998, -7.200000000000003, -17.0, -26.800000000000004, -3
     ...: 6.60000000000001, -46.400000000000006, -56.2, -66.0, -75.80000000000001, -85.60000000000001, -95.4, -105.2000000000
     ...: 0002])

“b = c”的地方就是“b - c = 0”的地方,所以我们为y传递b - c:

In [357]: find_roots(a, b - c)
Out[357]: array([ 312.5])

所以a的线性插值是312.5。

使用以下 matplotlib 命令:

In [391]: plot(a, b, label="b")
Out[391]: [<matplotlib.lines.Line2D at 0x11eac8780>]

In [392]: plot(a, c, label="c")
Out[392]: [<matplotlib.lines.Line2D at 0x11f23aef0>]

In [393]: roots = find_roots(a, b - c)

In [394]: [axvline(root, color='k', alpha=0.2) for root in roots]
Out[394]: [<matplotlib.lines.Line2D at 0x11f258208>]

In [395]: grid()

In [396]: legend(loc="best")
Out[396]: <matplotlib.legend.Legend at 0x11f260ba8>

In [397]: xlabel("a")
Out[397]: <matplotlib.text.Text at 0x11e71c470>

我明白了

plot

关于python - 尝试在 python 中进行线性插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457469/

相关文章:

python - 对服装照片进行分类有哪些好的特征?

python - Python 有没有模拟键盘操作的库?

python - 如何运行 .py 模块?

跨顶点的 OpenGL 颜色插值

java - 沿方向行驶时减速

python - 构建 '_mysql' 扩展错误 : Unable to find vcvarsall. bat

python - 在 Python 3 中加速分配给定两个值的概率密度

python - 如何使用 kapteyn.kmpfit 计算具有 2 个或更多自变量的模型的置信带

java - 如何创建自定义插值器以在 android 中应用翻译动画

Python - 代码片段不适用于 Python 2.5.6,使用 IDLE