问题
I am not getting the desired 2D linear interpolation functionality with LinearNDInterpolator. The following piece of code is trying to do an interpolation between the 4 knot points (0,0), (1,0), (0,1), (1,1). interp2d gives me the expected (linear-interpolated) result but LinearNDInterpolator is doing something else, which I am unable to figure out. Perhaps I am not using the API correctly. Unfortunately, I can't find detailed docs on the usage. Can someone please help or point me to the right forum (mathoverflow ?) to write to ?
>>> f = interp2d([0,1,0,1], [0,0,1,1], [0,1,2,4])
>>> f(0.5,0.5)
array([ 1.75])
>>> g = LinearNDInterpolator([[0,0],[1,0],[0,1],[1,1]], [0,1,2,4])
>>> g(0.5,0.5)
array(2.0)
回答1:
From http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.LinearNDInterpolator.html:
The interpolant is constructed by triangulating the input data with Qhull [R15], and on each triangle performing linear barycentric interpolation.
In your case, the triangles chosen appear to share the (0,0), (1,1)
edge. Since (0.5, 0.5)
is midway between (0,0)
and (1,1)
, the interpolated value lies between the values at those vertices, so it is (0+4)/2 = 2.0
.
回答2:
Because your four input points are co-circular, the underlying Delaunay triangulation is ambiguous; you might end up with an "edge" from (0,0) to (1,1), OR one along the "other diagonal", from (1,0) to (0,1).
In the first case, the interpolation of (0.5,0.5) will yield 2.0, in the latter case it will yield 1.5. You don't have control over which one you get.
If you want that control, you have two options:
1) supply the triangulation expressly by giving the interpolator an object that looks like one returned by the Delauney triangulation.
2) "nudge" one of your corners toward the center. For example, use (0.99999,0.99999) instead of (1,1); this will "force" the Delaunay triangulation to a deterministic outcome. (In this case, it would "force" the use of the (0,0) to (0.99999,0.99999) segment over the other diagonal.)
And, FWIW, I think it is no coincidence that the average of these two possible values (1.5 and 2.0) is what you observed when you used bilinear interpolation; I think that is expected, mathematically.
来源:https://stackoverflow.com/questions/12632683/scipy-interpolate-linearndinterpolator-not-producing-desired-functionality