Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy.interp2d warning and large errors off the grid

I am trying to interpolate a 2-dimensional function and I am running into what I consider weird behavior by scipy.interpolate.interp2d. I don't understand what the problem is, and I'd be happy for any help or hints.

import numpy as np
from scipy.interpolate import interp2d

x = np.arange(10)
y = np.arange(20)
xx, yy = np.meshgrid(x, y, indexing = 'ij')
val = xx + yy
f = interp2d(xx, yy, val, kind = 'linear')

When I run this code, I get the following Warning:

scipy/interpolate/fitpack.py:981: RuntimeWarning: No more knots can be added because the number of B-spline coefficients already exceeds the number of data points m. Probable causes: either s or m too small. (fp>s) kx,ky=1,1 nx,ny=18,15 m=200 fp=0.000000 s=0.000000
warnings.warn(RuntimeWarning(_iermess2[ierm][0] + _mess))

I don't understand why interp2d would use any splines when I tell it it should do linear interpolation. When I continue and evaluate f on the grid everything is good:

>>> f(1,1)
array([ 2.])

When I evaluate it off the grid, I get large errors, even though the function is clearly linear.

>>> f(1.1,1)
array([ 2.44361975])

I am a bit confused and I am not sure what the problem is. Did anybody run into similar problems? I used to work with matlab and this is almost 1:1 how I would do it there, but maybe I did something wrong.

When I use a rectangular grid (i.e. y = np.arange(10)) everything works fine by the way, but that isn't what I need. When I use cubic instead of linear interpolation, the error gets smaller (that doesn't make much sense either since the function is linear) but is still unacceptably large.

like image 812
Tobias Avatar asked Jan 04 '16 02:01

Tobias


1 Answers

I tried a couple of things and managed to get (kind of) what I want using scipy.LinearNDInterpolator. However, I have to convert the grid to lists of points and values. Since the rest of my program stores coordinates and values in grid format that is kind of annoying, so if possible I'd still like to get the original code to work properly.

import numpy as np
import itertools
from scipy.interpolate import LinearNDInterpolator

x = np.arange(10)
y = np.arange(20)
coords = list(itertools.product(x,y))
val = [sum(c) for c in coords]
f = LinearNDInterpolator(coords, val)

>>>f(1,1)
array(2.0)

>>> f(1.1,1)
array(2.1)
like image 92
Tobias Avatar answered Sep 28 '22 09:09

Tobias