2013-08-23 2 views
2

시간 증가가 불규칙한 데이터 세트의 10 초 차이를 계산하려고합니다. 데이터는 동일한 길이의 2 차원 1 차원 어레이에 존재합니다. 시간 및 데이터 값에 대한 다른 데이터를 표시합니다.numpy 배열의 불규칙한 시간 간격을 갖는 이동 시간 델타 가속화

주위를 찌를 때마다 나는 해결책을 내놓을 수 있었지만 배열의 모든 항목을 반복해야한다는 (용의자가) 근거가 너무 느립니다.

내 일반적인 방법은 시간 배열을 반복하는 것이며 각 시간 값에 대해 x 초 이전의 시간 값 인덱스를 찾습니다. 그런 다음 데이터 배열의 해당 인덱스를 사용하여 차이를 계산합니다.

코드는 다음과 같습니다.

첫째, 다음

def trailing_diff(time_array,data_array,seconds): 
    trailing_list=[] 
    for i in xrange(len(time_array)): 
     now=time_array[i] 
     if now<seconds: 
      trailing_list.append(0) 
     else: 
      then=find_closest(time_array,now-seconds) 
      trailing_list.append(data_array[i]-data_array[then]) 
    return np.asarray(trailing_list) 

불행하게도이 특히 잘 확장되지 않는 다음과 같은 방식으로 사용하고, 나는 싶습니다 양성 리코

def find_closest(A, target): 
    #A must be sorted 
    idx = A.searchsorted(target) 
    idx = np.clip(idx, 1, len(A)-1) 
    left = A[idx-1] 
    right = A[idx] 
    idx -= target - left < right - target 
    return idx 

에서 find_closest 기능 이것을 계산할 수 있습니다 (그리고 플롯).

내가 더 편리하게 할 수있는 방법에 대한 의견이 있으십니까?

편집 : 입/출력

In [48]:time1 
Out[48]: 
array([ 0.57200003, 0.579  , 0.58800006, 0.59500003, 
     0.5999999 , 1.05999994, 1.55900002, 2.00900006, 
     2.57599998, 3.05599999, 3.52399993, 4.00699997, 
     4.09599996, 4.57299995, 5.04699993, 5.52099991, 
     6.09299994, 6.55999994, 7.04099989, 7.50900006, 
     8.07500005, 8.55799985, 9.023  , 9.50699997, 
     9.59399986, 10.07200003, 10.54200006, 11.01999998, 
     11.58899999, 12.05699992, 12.53799987, 13.00499988, 
     13.57599998, 14.05599999, 14.52399993, 15.00199985, 
     15.09299994, 15.57599998, 16.04399991, 16.52199984, 
     17.08899999, 17.55799985, 18.03699994, 18.50499988, 
     19.0769999 , 19.5539999 , 20.023  , 20.50099993, 
     20.59099984, 21.07399988]) 

In [49]:weight1 
Out[49]: 
array([ 82.268, 82.268, 82.269, 82.272, 82.275, 82.291, 82.289, 
     82.288, 82.287, 82.287, 82.293, 82.303, 82.303, 82.314, 
     82.321, 82.333, 82.356, 82.368, 82.386, 82.398, 82.411, 
     82.417, 82.419, 82.424, 82.424, 82.437, 82.45 , 82.472, 
     82.498, 82.515, 82.541, 82.559, 82.584, 82.607, 82.617, 
     82.626, 82.626, 82.629, 82.63 , 82.636, 82.651, 82.663, 
     82.686, 82.703, 82.728, 82.755, 82.773, 82.8 , 82.8 , 
     82.826]) 

In [50]:trailing_diff(time1,weight1,10) 
Out[50]: 
array([ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 
     0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 
     0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 
     0. , 0.169, 0.182, 0.181, 0.209, 0.227, 0.254, 0.272, 
     0.291, 0.304, 0.303, 0.305, 0.305, 0.296, 0.274, 0.268, 
     0.265, 0.265, 0.275, 0.286, 0.309, 0.331, 0.336, 0.35 , 
     0.35 , 0.354]) 
+0

약간의 (작은) 입출력을 표시 할 수 있습니까? – Daniel

+0

@Ophion. Embarrasing 누락. 결정된. – Chris

+0

time_array 및 data_array의 크기는 얼마나됩니까? – tom10

답변

1

은 기성품 보간 루틴을 사용합니다. 당신이 정말로 가까운 이웃 동작을 원하는 경우에, 나는의 scipy.interpolate.interp1d을 scipy되어야 할 것이다 생각하지만, 선형 보간은 더 나은 옵션을 것, 그리고 당신은 NumPy와의 numpy.interp 사용할 수 있습니다

def trailing_diff(time, data, diff): 
    ret = np.zeros_like(data) 
    mask = (time - time[0]) >= diff 
    ret[mask] = data[mask] - np.interp(time[mask] - diff, 
             time, data) 
    return ret 

time = np.arange(10) + np.random.rand(10)/2 
weight = 82 + np.random.rand(10) 

>>> time 
array([ 0.05920317, 1.23000929, 2.36399981, 3.14701595, 4.05128494, 
     5.22100886, 6.07415922, 7.36161563, 8.37067107, 9.11371986]) 
>>> weight 
array([ 82.14004969, 82.36214992, 82.25663272, 82.33764514, 
     82.52985723, 82.67820915, 82.43440796, 82.74038368, 
     82.84235675, 82.1333915 ]) 
>>> trailing_diff(time, weight, 3) 
array([ 0.  , 0.  , 0.  , 0.18093749, 0.20161107, 
     0.4082712 , 0.10430073, 0.17116831, 0.20691594, -0.31041841]) 

가장 가까운 이웃을 얻으려면, 당신은

할 것
from scipy.interpolate import interp1d 

def trailing_diff(time, data, diff): 
    ret = np.zeros_like(data) 
    mask = (time - time[0]) >= diff 
    interpolator = interp1d(time, data, kind='nearest') 
    ret[mask] = data[mask] - interpolator(time[mask] - diff) 
    return ret 
+0

이 사망했습니다. 가장 가까운 이웃 구현은 완벽하게 작동하고 ~ 35ms에서 ~ 580us로 나를 데려옵니다. 또한, 매우 이해하기 쉬운 읽을 수 있습니다. 많은 감사합니다. – Chris

관련 문제