【问题标题】:Interpolating uneven data with SciPy用 SciPy 插值不均匀数据
【发布时间】:2013-07-01 20:25:26
【问题描述】:

我一直在查看 documentation for scipy.interpolate 上的示例,但我不知道如何获取间隔不均匀的数据并对其进行插值,因为所有教程都使用间隔均匀的 linspaces。

例如,我有一些这样的数据传播:

[--1--4-----5-3-22---55-]

其中每个- 代表一个缺失值。

我将如何使用scipy.interpolate 来拟合插值函数?

【问题讨论】:

  • 您是否尝试过将x 向量设置为等于您拥有数据的索引,并且您的y 向量等于这些索引处的值?我不确定这是否适用于您的情况,但我想到了。

标签: python numpy scipy curve-fitting interpolation


【解决方案1】:

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()

产量

【讨论】:

  • 谢谢!作为后续,我是否可以将插值函数扩展到数据之外?当我尝试通过在 linspace(0,some_number>x.max()) 上简单地运行它来执行此操作时,我得到一个 ValueError ,表明 x_new 中有一个值高于插值范围。
  • @zmjjmz 在文档中 docs.scipy.org/doc/scipy/reference/generated/…>:只需添加 bounds_error=False
猜你喜欢
  • 2011-07-05
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
相关资源
最近更新 更多