使用np.interp插值缺失值

时间:2016-04-20 16:24:41

标签: python numpy interpolation

我遇到了以下问题:

使用不同的代码我生成产生x和y值,频率和强度的文件。我使用2种不同的方法,因此得到2组不同的x,y' s。我们的想法是将一个与另一个进行规范以获得归一化的通量。然而,辅助代码使用不同的采样率,因为它会自动调整其速率。因此,正常化是不可能的,因为arries有不同的长度!

解决方案插入缺失值。我找到的最合适的方法是使用np.interp。良好:

继承我的代码:

 #Freq2_f1 is the frequency of File 1 - high sampling rate
 #Freq2_f2 is frequency of File2 - low sampling rate
 #Inten2_f1 is intensity of File1 -  high sampling rate
 #Inten2_f2 is intensity of File 2 - low sampling rate

Freq_f2=filearray2[0:num2-3]
Freq1_f2 = list(itertools.chain.from_iterable(Freq_f2))
Freq2_f2=np.array(map(float,Freq1_f2))
Inten_f2=filearray2[num2+1:len(filearray2)]
Inten1_f2 = list(itertools.chain.from_iterable(Inten_f2))
Inten2_f2=np.array(map(float,Inten1_f2))


Inten_int=np.interp(Freq2_f1,Freq2_f2,Inten2_f2)
#Inten_int=griddata(Freq2_f2,Inten2_f2,Freq2_f1,method='linear')
print
print 'Input frequency=highly sampled frequency'
print(Freq2_f1)
print 'Length of input frequency',len(Freq2_f1)
print 'Frequency of less sampled data'
print(Freq2_f2)
print 'Length of less sampled intensity', len(Freq2_f1)
print 'Intensity of less sampled data'
print(Inten2_f2)
print 'Length of less sampled frequency', len(Freq2_f2)
print 'Output array of np.interp '
print(Inten_int)
print 'Length of interpolated intensity', len(Inten_int)

这就是它的收益: 如您所见,输出数组Inten_int只是1.051e-02的常量

Input frequency=highly sampled frequency
[  6.87718000e+01   6.86571000e+01   6.85425900e+01 ...,   3.92414600e-03
3.91760100e-03   3.56145800e-03]
Length of input frequency 9576
Frequency of less sampled data
[  6.87718000e+01   6.86571000e+01   6.85425900e+01 ...,   3.92347200e-03
3.91692900e-03   3.56145800e-03]
Length of less sampled frequency 5857
Intensity of less sampled data
[  1.02640000e-36   1.20500000e-36   1.42720000e-36 ...,   1.19530000e-02
1.19260000e-02   1.05100000e-02]
Length of less sampled intensity 5857
Output array of np.interp 
[  1.05100000e-02   1.05100000e-02   1.05100000e-02 ...,   1.05100000e-02
1.05100000e-02   1.02640000e-36]
Length of interpolated intensity 9576

我不明白为什么! 根据np.interp

的要求,频率值(单调)增加

我为这个蹩脚的变量命名道歉:(

1 个答案:

答案 0 :(得分:2)

docs开始,签名为:

np.interp(x, xp, fp, left=None, right=None, period=None)

  

...

     

xp :1-D浮点序列 - 如果未指定参数周期,则数据点的x坐标必须增加。 ... < / p>

(重点是我的。)

您的xpFreq2_f2)似乎正在减少 - 不会增加。

Frequency of less sampled data
[  6.87718000e+01   6.86571000e+01   6.85425900e+01 ...,   3.92347200e-03 3.91692900e-03   3.56145800e-03]

你可能会尝试:

Inten_int = np.interp(Freq2_f1, Freq2_f2[::-1], Inten2_f2[::-1])