[英]Optimizing Array Element Shifting in Python / Numpy
問題:
運行我編寫的數據分析代碼的行分析后,我發現大約70%的總運行時間集中在對兩個不同的數組操作例程的調用上。 我最終希望以實時方式分析數據,因此這里的任何優化都會有很大幫助。
這兩個函數采用左邊的矩陣並將其帶到右邊的表格(反之亦然)。
我感興趣的矩陣目前通過N 2d numpy數組存儲為N(其中N是偶數)。
碼:
我編寫了以下代碼來完成此任務:
# Shifts elements of a vector to the left by the given amount.
def Vec_shift_L(vec, shift=0):
s = vec.size
out = np.zeros(s, dtype=complex)
out[:s-shift] = vec[shift:]
out[s-shift:] = vec[:shift]
return out
# Shifts elements of a vector to the right by the given amount.
def Vec_shift_R(vec,shift=0):
s=vec.size
out=np.zeros(s, dtype=complex)
out[:shift] = vec[s-shift:]
out[shift:] = vec[:s-shift]
return out
# Shifts a matrix from the left form (above) to the right form.
def OP_Shift(Trace):
s = Trace.shape
Out = np.zeros(s, dtype=complex)
for i in np.arange(s[0]):
Out[i,:] = Vec_shift_L(Trace[i,:], (i+s[0]/2) % s[0])
for i in np.arange(s[0]):
Out[i,:] = np.flipud(Out[i,:])
return Out
# Shifts a matrix from the right form (above) to the left form.
def iOP_Shift(Trace):
s = Trace.shape
Out = np.zeros(s, dtype=complex)
for i in np.arange(s[0]):
Out[i,:] = np.flipud(Trace[i,:])
for i in np.arange(s[0]):
Out[i,:] = Vec_shift_R(Out[i,:], (i+s[0]/2) % s[0])
return Out
我最初寫這篇文章的時候並沒有意識到numpy的roll函數,所以我寫了vec_shift函數。 與使用當前系統上的roll相比,它們的性能似乎提高了約30%。
有沒有辦法進一步提高這段代碼的性能?
讓NumPy broadcasting
為您提供矢量化解決方案!
# Store shape of input array
s = Trace.shape
# Store arrays corresponding to row and column indices
I = np.arange(s[0])
J = np.arange(s[1]-1,-1,-1)
# Store all iterating values in "(i+s[0]/2) % s[0]" as an array
shifts = (I + s[0]/2)%s[0]
# Calculate all 2D linear indices corresponding to 2D transformed array
linear_idx = (((shifts[:,None] + J)%s[1]) + I[:,None]*s[1])
# Finally index into input array with indices for final output
out = np.take(Trace,linear_idx)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.