Interpolation of data
Posted February 27, 2013 at 02:42 PM | categories: interpolation | tags:
When we have data at two points but we need data in between them we use interpolation. Suppose we have the points (4,3) and (6,2) and we want to know the value of y at x=4.65, assuming y varies linearly between these points. we use the interp1d command to achieve this. The syntax in python is slightly different than in matlab.
from scipy.interpolate import interp1d x = [4, 6] y = [3, 2] ifunc = interp1d(x, y) print ifunc(4.65) ifunc = interp1d(x, y, bounds_error=False) # do not raise error on out of bounds print ifunc([4.65, 5.01, 4.2, 9])
2.675 [ 2.675 2.495 2.9 nan]
The default interpolation method is simple linear interpolation between points. Other methods exist too, such as fitting a cubic spline to the data and using the spline representation to interpolate from.
from scipy.interpolate import interp1d x = [1, 2, 3, 4]; y = [1, 4, 9, 16]; # y = x^2 xi = [ 1.5, 2.5, 3.5]; # we want to interpolate on these values y1 = interp1d(x,y) print y1(xi) y2 = interp1d(x,y,'cubic') print y2(xi) import numpy as np print np.array(xi)**2
[ 2.5 6.5 12.5] [ 2.25 6.25 12.25] [ 2.25 6.25 12.25]
In this case the cubic spline interpolation is more accurate than the linear interpolation. That is because the underlying data was polynomial in nature, and a spline is like a polynomial. That may not always be the case, and you need some engineering judgement to know which method is best.
Copyright (C) 2013 by John Kitchin. See the License for information about copying.