2016-04-23 30 views
0

我有2列信息。第二列是以秒爲單位的時間。第一列當時是錯誤的。我需要製作一個包含以秒爲單位的錯誤值2.5s的向量。應該有172個。這是我的數據: COL 0 =錯誤,列1秒需要使用不同類型的插值? numpy interp1d

array([[0.00, 0.01], 
    [1.91, 9.60], 
    [0.00, 19.08], 
    [2.05, 28.64], 
    [1.04, 38.19], 
    [1.89, 47.73], 
    [1.69, 57.27], 
    [2.24, 66.79], 
    [1.89, 76.33], 
    [1.86, 85.88], 
    [2.37, 95.39], 
    [2.29, 104.93], 
    [2.03, 114.45], 
    [2.16, 123.99], 
    [1.34, 133.52], 
    [2.40, 143.03], 
    [2.17, 152.54], 
    [0.00, 162.03], 
    [1.61, 171.59], 
    [2.31, 181.13], 
    [2.15, 190.67], 
    [2.22, 200.19], 
    [2.16, 209.72], 
    [0.00, 219.20], 
    [2.65, 228.76], 
    [1.74, 238.33], 
    [0.00, 247.85], 
    [2.33, 257.42], 
    [1.85, 266.94], 
    [0.00, 276.50], 
    [2.27, 286.06], 
    [1.67, 295.62], 
    [2.41, 305.15], 
    [0.00, 314.65], 
    [1.32, 324.21], 
    [2.39, 333.74], 
    [2.19, 343.27], 
    [2.51, 352.81], 
    [2.41, 362.33], 
    [1.79, 371.86], 
    [0.00, 381.36], 
    [3.07, 390.93], 
    [2.30, 400.47], 
    [0.00, 409.98], 
    [2.41, 419.54], 
    [2.22, 0.05], 
    [1.75, 9.59], 
    [2.18, 19.14], 
    [1.99, 28.64], 
    [1.80, 38.16], 
    [1.45, 47.68], 
    [1.57, 57.21], 
    [2.24, 66.74], 
    [0.00, 76.24], 
    [2.31, 85.80], 
    [0.00, 95.29], 
    [2.39, 104.85], 
    [0.00, 114.34], 
    [0.95, 123.89], 
    [2.35, 133.42], 
    [2.43, 142.98], 
    [1.66, 152.48], 
    [1.08, 162.01], 
    [0.00, 171.53], 
    [1.20, 181.08], 
    [2.43, 190.64], 
    [2.42, 200.16], 
    [2.59, 209.69], 
    [1.98, 219.22], 
    [1.75, 228.76], 
    [2.28, 238.28], 
    [1.98, 247.80], 
    [1.08, 257.33], 
    [2.08, 266.84], 
    [2.30, 276.37], 
    [0.00, 285.84], 
    [1.38, 295.40], 
    [2.19, 304.95], 
    [0.00, 314.44], 
    [1.54, 324.01], 
    [2.19, 333.52], 
    [0.00, 343.02], 
    [2.13, 352.59], 
    [2.31, 362.13], 
    [0.00, 371.61], 
    [2.36, 381.18], 
    [2.02, 390.71], 
    [2.68, 400.24], 
    [0.00, 409.71], 
    [2.19, 419.28]]) 

我嘗試使用線性內插使用下面的代碼=時間,但是得到了錯誤ValueError: A value in x_new is below the interpolation range.

import numpy as np 
#import scipy 
#import matplotlib.pyplot as plt 
from scipy import interpolate 
float_formatter = lambda x: "%.2f" % x 
#np.set_printoptions(formatter={'float_kind':float_formatter}) 

# Read the text file with the errors - error,time format 
orig=np.genfromtxt('Error_Onsets.csv',delimiter=',') 
print repr(orig) 
# Build a linear interpolator, giving it the known time (X) and error (Y) 
interpf = interpolate.interp1d(orig[:,1],orig[:,0],kind='linear') 

# What's the TR? 
TR=2.5 

# Setup the new vector of times, spaced by TRs 
new_times=np.arange(0,172*TR,TR) 

# Interpolate using the func defined above to get the error at any TR 
new_err = interpf(new_times) 

我請閱讀,這可能是因爲x值需要穩定增加線性插值是適當的。我會很感激任何建議。

+2

查看['interp1d']的'bounds_error'和/或'fill_value'參數(http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html #scipy.interpolate.interp1d)。具體來說,當你沒有指定'bounds_error'時,你需要一個超出輸入範圍的值(就像你正在做的,在t = 0時)。 – jedwards

+1

如果你不得不使用穩定增長的價值,那麼你不會。除非您指定'assume_sorted = False',否則,矢量將按函數排序。 – jedwards

回答

2

我通常這樣做沒有插,只是用最新值(所以沒有從未來的數據採樣):

times = np.arange(orig[0,1], orig[-1,1], 2.5) 
indexes = np.searchsorted(orig[:,1], times, side='right') - 1 
np.column_stack((orig[indexes,0], times)) 

這給了你兩列:新時代2.5秒開,並且這些時間的最新錯誤值。

+0

我不明白這是做什麼。我得到以下的數組: 陣列([[0.01,0.01], [2.51,0.05], [5.01,0.05], [7.51,0.05], [10.01,9.59], [12.51,9.59 ], [15.01,9.59], [17.51,9.59],... 第一個值應該是錯誤的,但是錯誤永遠不會超過5,所以我不明白爲什麼我會爲我獲得巨大的值錯誤 – Maria

+0

@Maria:我很抱歉,在最後一行中,我錯誤地將列交換了,現在我已經更新了答案,請嘗試一下,第一列應該是錯誤,第二列是時間 –

+0

謝謝!仍然似乎是插值(或什麼...)。我得到以下內容:'array([[0.00,0.01], [2.22,2.51], [2.22,5.01], [2.22,7.51], [1.75,10.01], [1.75,12.51],'...誤差爲0,直到9s後,米不知道爲什麼它在2.51,5.01等2> – Maria