如何使用numpy在列表中的值对之间进行插值

时间:2018-11-14 15:05:13

标签: python numpy

我有类似这样的列表:

x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]

我想在列表中的项目对之间进行插值,以使列表长度为n,而不是长度为6的列表,列表中每对项目之间的间隔值相等。我当前的方法是使用列表推导来遍历列表中的对,并np.extend将空列表与结果一起使用。有更好的现成功能可以做到这一点吗?

我目前的做法:

import numpy as np

x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]
result_x = []
result_y = []
[result_x.extend(np.linspace(first, second, 5)) for first, second, in  zip(x_data, x_data[1:])]
[result_y.extend(np.linspace(first, second, 5)) for first, second, in  zip(y_data, y_data[1:])]
print(result_x, '\n'*2, result_y)

Out: [3.0, 3.5, 4.0, 4.5, 5.0, 5.0, 5.5, 6.0, 6.5, 7.0, 7.0, 7.25, 7.5, 7.75, 8.0, 8.0, 7.25, 6.5, 5.75, 5.0, 5.0, 4.25, 3.5, 2.75, 2.0] 



[15.0, 16.25, 17.5, 18.75, 20.0, 20.0, 20.5, 21.0, 21.5, 22.0, 22.0, 22.25, 22.5, 22.75, 23.0, 23.0, 22.5, 22.0, 21.5, 21.0, 21.0, 19.25, 17.5, 15.75, 14.0]

2 个答案:

答案 0 :(得分:1)

我认为该功能可以使用np.interp来实现您想要的功能:

import numpy as np

def interpolate_vector(data, factor):
    n = len(data)
    # X interpolation points. For factor=4, it is [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, ...]
    x = np.linspace(0, n - 1, (n - 1) * factor + 1)
    # Alternatively:
    # x = np.arange((n - 1) * factor + 1) / factor
    # X data points: [0, 1, 2, ...]
    xp = np.arange(n)
    # Interpolate
    return np.interp(x, xp, np.asarray(data))

示例:

x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]

print(interpolate_vector(x_data, 4))
# [3.   3.5  4.   4.5  5.   5.5  6.   6.5  7.   7.25 7.5  7.75 8.   7.25
#  6.5  5.75 5.   4.25 3.5  2.75 2.  ]
print(interpolate_vector(y_data, 4))
# [15.   16.25 17.5  18.75 20.   20.5  21.   21.5  22.   22.25 22.5  22.75
#  23.   22.5  22.   21.5  21.   19.25 17.5  15.75 14.  ]

答案 1 :(得分:1)

Scipy具有插值功能,可以轻松处理这种方法。您只需提供您的当前数据和内插数据将基于的新“ x”值即可。

from scipy import interpolate

x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]
t1 = np.linspace(0, 1, len(x_data))
t2 = np.linspace(0, 1, len(y_data))

n = 50
t_new = np.linspace(0, 1, n)

f = interpolate.interp1d(t1, x_data)
x_new = f(t_new)

f = interpolate.interp1d(t2, y_data)
y_new = f(t_new)