18

Using numpy.interp I am able to compute the one-dimensional piecewise linear interpolant to a function with given values at discrete data-points.

Is it a similar function to return me the logarithmic interpolation?

ali_m
  • 71,714
  • 23
  • 223
  • 298
DimKoim
  • 1,024
  • 6
  • 20
  • 33
  • 1
    It's not clear from your question what exactly you mean by 'logarithmic interpolation'. Could you give some example input/output? – ali_m Mar 30 '15 at 13:23

2 Answers2

27

In the past, I've just wrapped the normal interpolation to do it in log-space, i.e.

def log_interp(zz, xx, yy):
    logz = np.log10(zz)
    logx = np.log10(xx)
    logy = np.log10(yy)
    return np.power(10.0, np.interp(logz, logx, logy))

Personally, I much prefer the scipy interpolation functions (as @mylesgallagher mentions), for example:

import scipy as sp
import scipy.interpolate

def log_interp1d(xx, yy, kind='linear'):
    logx = np.log10(xx)
    logy = np.log10(yy)
    lin_interp = sp.interpolate.interp1d(logx, logy, kind=kind)
    log_interp = lambda zz: np.power(10.0, lin_interp(np.log10(zz)))
    return log_interp

Then you can just call this as a function on an arbitrary value.

mx0
  • 6,445
  • 12
  • 49
  • 54
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
  • I have tried this metod but it cannot interpolate values out of the range used in for interpolating. Do you know a way for estimating new values? – Luis González Mar 24 '20 at 15:43
  • 1
    @LuisGonzález scipy doesn't have extensive support for extrapolation, but check out this answer: https://stackoverflow.com/a/8166155/230468, NOTE: extrapolation, especially with splines, can result in surprising behavior (e.g. the curve can 'turn' sharply outside of the input range). – DilithiumMatrix Mar 24 '20 at 15:49
0

If I am understanding you correctly, you have some discrete data that you want to get a smooth set of values that would arise in between the values you have. I am assuming you don't want an equation of a log function that approximates the data.

Unfortunately numpy does not have anything outside of the linear piecewise interpolation, however if you look into using SciPy it does have a more powerful interpolation function. See SciPy's interpolate documentation for more detail.

It includes more complex interpolations like 'cubic' interpolations which will give you very smooth approximations, but it won't be a logarithm and it won't give you an equation.

If you want an equation what you are looking for is a regression technique not interpolation, but I don't think you are.

petezurich
  • 9,280
  • 9
  • 43
  • 57
mgallagher
  • 466
  • 6
  • 14