为什么1D scipy.interpolate.griddata使用method = nearest产生nans?

时间:2014-12-04 17:52:20

标签: python scipy nearest-neighbor interpolation

我在一组坐标上运行scipy.interpolate.griddata,这些坐标可能有很多维度(甚至是1)。当坐标为1D时,最近的方法在外边界时产生nans而不是最接近的值。一个例子:

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

target_points = [1.,2.,3.,4.,5.,6.,7.]
points = np.random.rand(50)*2*np.pi
values = np.sin(points)

interp = griddata(points, values, target_points, method='nearest')
plt.plot(points,values,'o')
plt.plot(target_points,interp,'ro')
print interp
plt.show()

打印的最后一个值是NaN。难道我做错了什么?如果这是scipy的限制,你有一个聪明的解决方法吗?

请注意,预期线性/立方模式会给出NaN,但对于最近的'而言,情况并非如此。模式。

2 个答案:

答案 0 :(得分:0)

当数据为1维时,griddata defers to interpolate.interp1d

if ndim == 1 and method in ('nearest', 'linear', 'cubic'):
    from .interpolate import interp1d
    points = points.ravel()
    ...
    ip = interp1d(points, values, kind=method, axis=0, bounds_error=False,
                  fill_value=fill_value)
    return ip(xi)

所以即使method='nearest'网格数据不会从interp1d behaves this way推断出来。

但是,还有其他工具,例如scipy.cluster.vq(矢量量化),您可以使用它们来查找最近的值。例如,

import numpy as np
import scipy.cluster.vq as vq
import matplotlib.pyplot as plt

target_points = np.array([1.,2.,3.,4.,5.,6.,7.])
points = (np.random.rand(50)*2*np.pi)
values = np.sin(points)
code, dist = vq.vq(target_points, points)
interp = values[code]

plt.plot(points,values,'o')
plt.plot(target_points,interp,'ro')
print interp
plt.show()

enter image description here

答案 1 :(得分:0)

这看起来像scipy.interpolate.griddata中的错误,因为行为不符合明确说明输入参数" fill_value"的文档。当方法是"最接近"时没有效果。

以下行的输出:

scipy.interpolate.griddata(points=np.array([1,2]), values=np.array([10,20]), xi=3, method='nearest', fill_value=-1)

array(-1.0),证明fill_value对输出有影响,与文档中所述相反。