python - 使用 SciPy 插值不均匀数据

标签 python numpy scipy curve-fitting interpolation

我一直在浏览 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()

产量 enter image description here

关于python - 使用 SciPy 插值不均匀数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459984/

相关文章:

python - 如何重载 __repr__ 方法以显示链表堆栈中的所有项目?

python - 形态侵 eclipse ——Scipy ndimage 和 Scikit image 的区别

python - scipy给出了 undefined symbol : clapack_sgesv

python - 如何迭代行索引并从Python数据集中找到具有最大行列式的组?

Python 3.4 与 numpy、scipy 和 matplotlib 的兼容性

python - 导入 scikit-learn 时出错

python - 图像中文本的边界框

python - 使用 opencv 和 python 从具有可变帧速率的 IP 摄像机录制视频

python - Pandas 在 groupby 中按条件过滤

python - 复制 Python int * numpy.array 行为