Python(和編程)新手在這里。我撰寫了一個代碼,它通過兩個陣列(x 和 y;x 是線性時間,y 是測量值)來洗掉小于某個閾值的“事件”。每個都是一個形狀為 (12000000,) 的陣列。Start 和 Stop 是相同形狀的陣列 ~(600,),分別包含每個事件開始和結束的時間。閾值是一個浮點數。
該代碼有效,但速度非常慢。我不確定是因為使用了 np.where 還是因為我必須遍歷 np.nanmax。
有關如何使此運行更快的任何提示?
def event_threshold (x, y, start, stop, threshold):
result_start = []
result_stop = []
for i_start,i_stop in zip(start,stop):
start_x = np.where(x == i_start)[0][0]
stop_x = np.where(x == i_stop)[0][0]
if threshold >= 0:
if np.nanmax(y[start_x:stop_x]) >= threshold:
#Add elements if cross positive threshold
result_start = np.append(result_start, i_start)
result_stop = np.append(result_stop, i_stop)
else: #if threshold is negative
if np.nanmin(y[start_x:stop_x]) <= threshold:
#Add elements if cross negative threshold
result_start = np.append(result_start, i_start)
result_stop = np.append(result_stop, i_stop)
return result_start, result_stop
uj5u.com熱心網友回復:
通常,您應該盡可能用串列推導式替換 for 回圈以加速您的代碼。
我已根據您的評論將串列理解解決方案更新為非背靠背索引。請參閱下面的代碼示例。在我的情況下,計算速度超過 x10。讓我知道,如果這就是你要找的。
干杯
import time
import numpy as np
def event_threshold (x, y, start, stop, threshold):
result_start = []
result_stop = []
for i_start,i_stop in zip(start,stop):
start_x = np.where(x == i_start)[0][0]
stop_x = np.where(x == i_stop)[0][0]
if threshold >= 0:
if np.nanmax(y[start_x:stop_x]) >= threshold:
#Add elements if cross positive threshold
result_start = np.append(result_start, i_start)
result_stop = np.append(result_stop, i_stop)
else: #if threshold is negative
if np.nanmin(y[start_x:stop_x]) <= threshold:
#Add elements if cross negative threshold
result_start = np.append(result_start, i_start)
result_stop = np.append(result_stop, i_stop)
return result_start, result_stop
# create minimal example
n=100001
start_width = 100
x = np.array([i for i in range(n)])
y = np.random.rand(n)
start = np.array([i for i in range(0,n-1,start_width)])
stop = start [int(i) for i in np.random.uniform(1,start_width,len(start))]
threshold=0.95
# timed result event threshold function
t_0 = time.time()
result_stop_ = event_threshold(x,y,start,stop,threshold)
print(f'time elapsed (s): {time.time()-t_0}')
# timed result list comprehension
t_0 = time.time()
result_stop = [(start[i], stop[i]) for i in range(len(stop)) if np.nanmax(abs(y[start[i]:stop[i]])) >= threshold]
result_stop = np.array(result_stop)
print(f'time elapsed (s): {time.time()-t_0}')

PS:但請注意,結果是 np.array 而不是元組
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/355124.html
