Home > Mobile >  unexpected result after interpolating columns to target x-coordinates
unexpected result after interpolating columns to target x-coordinates

Time:01-13

I am using np.interp to linearly interpolate two columns of data in a csv file to a list of target (expected) values. I converted the target values to a numpy array. Each row in the csv file correspond to each value in the target data.

Here is a sample of data:

x= [246.72,   8.59, 194.4 , 145.73, 198.27]

v11         v12         
22.028949   22.863912   
0           9.006818    
134.238326  102.571956  
94.222921   107.823484  
200.163640  212.396264  

script:

df['intrepo_v'] = pd.Series(dtype= 'float')
df['intrepo_v'] = np.interp(x, df['v11'].values,
df['v12'].values)

result:

42.579263
22.863912
42.579263
42.579263
42.579263

The aim of this interpolation is to create intrepo_v and move the values from v11 and v12 to some values close to x. Currently the result is not close to v11, v12, or intrepo_v. Can anyone suggest a way to solve this problem?

CodePudding user response:

For numpy.interp the values must be increasing (see the help), scipy does not assume the values are sorted, try:

import scipy    
function=scipy.interpolate.interp1d(df['v11'].values,df['v12'].values)


df['intrepo_v'] =function(x)

or alternatively sort the input values with numpy

  •  Tags:  
  • Related