我有一個隨機資料串列和一個閾值:
threshold = 3
data = [2,2,2,2,2,5,5,2,2,2,2,3,4,5,6,4,5,4,3,4,5,3,3,7,8,2,2,2] # data
timestamp =[]
for i in range(len(data)):
timestamp.append(i)
print(timestamp)
我正在嘗試提取低于閾值的時間戳,但是,如果在低于閾值的 2 個時間范圍之間出現一系列連續時間戳(小于 4 個時間戳 (<4)),我們也將其視為低于閾值
因此,此示例應回傳:
belowthreshold = [0,1,2,3,4,5,6,7,8,9,10,25,26,27]
所以我們可以看到連續5,5被跳過并被視為低于閾值,因為它之前和之后的值都低于閾值
目前,我的方法是:
belowthreshold = []
for j in range(len(data)):
if data[j] < threshold and data[j]: # check if greater than threshold, meaning energy is being used at home
belowthreshold.append(j) # add this time to a list
然而,它很明顯只提取小于閾值的值。
解決這個問題的最佳方法是什么?
提前感謝您的回答
uj5u.com熱心網友回復:
嘗試使用串列理解itertools.zip_longest:
import itertools
output = [i for i, (x, y, z) in enumerate(itertools.zip_longest(data,data[1:],data[2:],fillvalue=0)) if x<threshold or y<threshold or z<threshold]
>>> output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 24, 25, 26, 27]
編輯:
考慮到“連續”時間戳可以在任一側,您可以使用itertools.groupby自定義鍵來檢查該值是否小于閾值。
這會將資料分成以下幾組:[2, 2, 2, 2, 2], [5, 5], [2, 2, 2, 2], [3, 4, 5, 6, 4, 5, 4, 3, 4, 5, 3, 3, 7, 8],[2, 2, 2]
output = list()
i = 0
for k, v in itertools.groupby(data, key=lambda x: x<threshold):
values = list(v)
if k or len(values) < 4:
output = [i x for x in range(len(values))]
i = len(values)
>>> output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 26, 27]
uj5u.com熱心網友回復:
當步驟超過閾值時,您可以使用臨時串列來存盤步驟,如果它們僅保持在 3 或更少步驟以上,您可以將它們添加到結果串列中,否則您重置此臨時串列。開始了:
threshold = 3
data = [2,2,2,2,2,5,5,2,2,2,2,3,4,5,6,4,5,4,3,4,5,3,3,7,8,2,2,2] # data
steps =[] # 'time stamp'
for i in range(len(data)):
steps.append(i)
print(steps)
belowthreshold = []
temp_above_threshold = []
consecutive_above_counter = 0
for j in range(len(data)):
if data[j] < threshold:
if consecutive_above_counter < 4: # add only if less than 4 steps were above threshold
belowthreshold = belowthreshold temp_above_threshold
# reset counter and temporary list
consecutive_above_counter = 0
temp_above_threshold = []
belowthreshold.append(j) # add this time to a list
else:
consecutive_above_counter = 1
if consecutive_above_counter < 4:
temp_above_threshold.append(j)
else:
temp_above_threshold = []
print(belowthreshold)
編輯:我試圖在您的代碼之后帶來一個簡單的解決方案,而不會增加以后可能難以跟蹤的額外包復雜性。
uj5u.com熱心網友回復:
我已設法使用以下代碼復制您的輸出:
def below_threshold(threshold, list_of_value):
indices = set()
for i in range(2, len(list_of_value)):
if all(list_of_value[k] >= threshold for k in [i, i - 1, i - 2]):
indices = indices.union({i, i-1, i-2})
return set(range(len(list_of_value))).difference(indices)
print(below_threshold(3, [2, 2, 2, 2, 2, 5, 5, 2, 2, 2, 2, 3, 4, 5, 6, 4, 5, 4, 3, 4, 5, 3, 3, 7, 8, 2, 2, 2]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/441420.html
