Claire
Claire

Reputation: 11

Scipy.interpolate get x values that produces certain y value 2 lists of numbers

I have 2 lists of data, one is

x = [13.7,12.59,11.22,10.00,8.91,7.94,7.08,6.31,5.62,5.01,4.47,3.98,3.55,3.16,2.82,2.51,2.24,2.00,
    1.78,1.59,1.41,1.26,1.12,1.00,0.89,0.79,0.71,0.63,0.56,0.50,0.40,0.32,0.25,0.20,0.16,0.13,
   0.1,0.08,0.06,0.05,0.04,0.03,0.025,0.02,0.016,0.013,0.01,0.008,0.006,0.005,0.004]

one is

y = [0.19 0.34 0.5 0.5 0.53 0.92 0.92 0.92 0.92 0.92 0.93 0.95 0.96 0.96 0.99 0.99 0.99 0.99 0.99 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0]

I need to get the x value when y=0.5,y=0.6,y=0.7

I have 52 pairs of this messy data, and I need to get x values by a given y, but y=0.5 etc are not in the y lists.

I try to generate some functions for each pair of data, and then try to find the x values, but I don't know how.

Could you help me?

Upvotes: 1

Views: 7095

Answers (1)

Gabriel
Gabriel

Reputation: 10904

The interp1d function is a fine way to do this, but you have to make some changes. Suppose we have x and y as defined in your post (note I added commas and a value of 1.0 to the list y to make them the same length).

x = [13.7, 12.59, 11.22, 10.0, 8.91, 7.94, 7.08, 6.31, 5.62, 5.01, 4.47, 3.98, 3.55, 3.16, 2.82, 2.51, 2.24, 2.0, 1.78, 1.59, 1.41, 1.26, 1.12, 1.0, 0.89, 0.79, 0.71, 0.63, 0.56, 0.5, 0.4, 0.32, 0.25, 0.2, 0.16, 0.13, 0.1, 0.08, 0.06, 0.05, 0.04, 0.03, 0.025, 0.02, 0.016, 0.013, 0.01, 0.008, 0.006, 0.005, 0.004]

y = [0.19, 0.34, 0.5, 0.5, 0.53, 0.92, 0.92, 0.92, 0.92, 0.92, 0.93, 0.95, 0.96, 0.96, 0.99, 0.99, 0.99, 0.99, 0.99, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

The first thing to note is that the first argument in scipy.interpolate.interp1d must be monotonically increasing. That means you can either do,

from scipy.interpolate import interp1d
f = interp1d( y, x )

or

x.reverse()
y.reverse()
f = interp1d( x, y )

In the first case we get,

>>> f(0.5), f(0.6), f(0.7)
(array(11.22), array(8.735897435897437), array(8.487179487179487))

and in the second we get,

>>> f(0.5), f(0.6), f(0.7)
(array(1.0), array(1.0), array(1.0))

Upvotes: 3

Related Questions