1

i have to arrays:

a linear one;

x = array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. , 1.1,  1.2,  1.3,  1.4])

And a corresponding result which is a non-linear one;

y = array([ 13.07,  13.7 ,  14.35,  14.92,  15.5 ,  16.05,  16.56,  17.12,
        17.62,  18.08,  18.55,  19.02,  19.45,  19.88,  20.25])

Now: I want to convert y to a linearly spaced array and find the corresponding interpolated values of x.

i.e. find x when

y = array([ 13. ,  13.5,  14. ,  14.5,  15. ,  15.5,  16. ,  16.5,  17. , 17.5,  18. ,  18.5,  19. ,  19.5,  20. ])

Thanks in advance.

I use the following method using the interp function in numpy:

ynew = np.linspace(np.min(y), np.max(y), len(y))
xnew = np.interp(ynew, y, x)

i.e. exchanging x and y in the np.interp function.

Is this always correct ? or will it break down for some condition.

2
  • How do you want to get from y1 to y2? What's the logic there? Commented Mar 17, 2015 at 5:53
  • @jedwards it is simply some measured data. no logic. Commented Mar 17, 2015 at 6:00

1 Answer 1

4

Unless I'm missing something, this case calls for a simple invocation of numpy.interp. You want to predict x from y which is the reverse of how people usually do their variable definitions, but other than that wrinkle, all you need is:

import numpy as np
x = np.array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. , 1.1,  1.2,  1.3,  1.4])
y = np.array([ 13.07,  13.7 ,  14.35,  14.92,  15.5 ,  16.05,  16.56,  17.12,
        17.62,  18.08,  18.55,  19.02,  19.45,  19.88,  20.25])
ynew = np.array([ 13. ,  13.5,  14. ,  14.5,  15. ,  15.5,  16. ,  16.5,  17. , 17.5,  18. ,  18.5,  19. ,  19.5,  20. ])
xnew = np.interp(ynew, y, x)
print xnew

Which gives as ouput:

[ 0.          0.06825397  0.14615385  0.22631579  0.3137931   0.4
  0.49090909  0.58823529  0.67857143  0.776       0.8826087   0.9893617
  1.09574468  1.21162791  1.33243243]
Sign up to request clarification or add additional context in comments.

4 Comments

i just found that answer myself. so i have modified my question to find wheather this method is valid for all cases.
Sorry I missed your edit superbuch -- it looks like it came in after my answer? Either way, I hope the answer was still useful to you. I think the np.interp function is generally useful, but without knowing exactly what you mean by "all" cases, I would hesitate to use that word.
thanks for answer. By all cases i mean when either x or y or both are negative or when one or the other or both are decreasing or one is increasing and the other is decreasing.
You could try a few quick variations: np.interp(ynew, y[::-1], x[::-1]), np.interp(ynew, -y[::-1], -x[::-1]), np.interp(-ynew[::-1], -y[::-1], -x[::-1]), etc., and see which ones work and which ones run into problems. At least a few of those will give results that are not useful. It took a moment or two but eventually for each one I realized what the problem was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.