This code snippet shows a simple way to do linear or nearest-neighbor interpolation using only NumPy.  This is handy if you don't have SciPy installed or don't want to introduce a dependency on SciPy in distributed code.  By using the numpy.searchsorted() method and vectorized operations it is reasonably fast, though I have not made much effort to truly optimize the algorithm.

def interpolate(yin, xin, xout, method='linear'):
  """
  Interpolate the curve defined by (xin, yin) at points xout. The array
  xin must be monotonically increasing. The output has the same data type as
  the input yin.

  :param yin: y values of input curve
  :param xin: x values of input curve
  :param xout: x values of output interpolated curve
  :param method: interpolation method ('linear' | 'nearest')

  @:rtype: numpy array with interpolated curve
  """
  lenxin = len(xin)

  i1 = numpy.searchsorted(xin, xout)
  i1[ i1==0 ] = 1
  i1[ i1==lenxin ] = lenxin-1

  x0 = xin[i1-1]
  x1 = xin[i1]
  y0 = yin[i1-1]
  y1 = yin[i1]

  if method == 'linear':
    return (xout - x0) / (x1 - x0) * (y1 - y0) + y0
  elif method == 'nearest':
    return numpy.where(numpy.abs(xout - x0) < numpy.abs(xout - x1), y0, y1)
  else:
    raise ValueError('Invalid interpolation method: %s' % method)# Python code here



    5           11