我一直在浏览 documentation for scipy.interpolate
上的示例而且我无法弄清楚如何获取间隔不均匀的数据并对其进行插值,因为所有教程都使用 linspaces —— 间隔均匀。
例如,我有一些数据是这样传播的:
[--1--4-----5-3-22---55-]
其中每个 -
代表一个缺失值。
我将如何使用 scipy.interpolate
将插值函数拟合到此?
最佳答案
interpolate.interp1d适用于不均匀分布的数据。例如,
import re
import numpy as np
import scipy.interpolate as interpolate
import matplotlib.pyplot as plt
text = '--1--4-----5-3-22---55-'
parts = [c for c in re.split(r'(-|\d+)', text) if c]
data = np.array([(x, int(y)) for x, y in enumerate(parts) if y != '-'])
x, y = data.T
f = interpolate.interp1d(x, y, kind='cubic')
newx = np.linspace(x.min(), x.max())
newy = f(newx)
plt.plot(newx, newy)
plt.scatter(x, y, s=20)
plt.show()
产量
关于python - 使用 SciPy 插值不均匀数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459984/