首页 > 解决方案 > 在一维序列的大列表上的 Scipy 二次一维插值非常慢

问题描述

我有一个一维序列的大列表(500 000+),我想对它们中的每一个进行二次插值。由于它们的大小不同,我不得不遍历它们并在每个序列上应用 scipy.interpolate.interp1D 。它非常慢,我正在寻找一种方法来解决问题。

目前,我有以下功能:

def interp_sequences(sequences, max_len):

'''Interpolate sequences in order to reduce their length to max_len

    sequences (list of numpy arrays): The sequences to interpolate

    maxlen (int): The maximum length of the sequence: All sequences will be interpolated to match this length
    -------------------------------------------------------------------
    returns (ndarray): The interpolated sequences
'''

interp_obs = np.zeros((len(obs_list), 5, 120))
# Looping is dirty... But the sequences have different lengths...
for idx, s in enumerate(sequences): 
    original_len = s.shape[1]
    f = interp1d(np.arange(original_len), s, 'quadratic', axis = 1)
    interp_seq = np.apply_along_axis(f, 0, np.linspace(0, original_len -1, num = max_len))
    interp_obs[idx] = interp_seq

return interp_obs

我也尝试过使用不会提高性能的列表理解。我想使用 numba-scipy 但它目前不支持插值。先感谢您 !

标签: pythonperformancescipyinterpolation

解决方案


推荐阅读