[Python] Faster moving median in numpy

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to calculate a moving median for around 10.000 signals that each are a list of length around 750.

    An example dataframe looks like this:

    num_indices = 2000 # Set number of indices

    # Generate lists of values (each a list of numbers from 0 to 1)
    column_data = [np.random.random(750).tolist() for _ in range(num_indices)]

    # Create DataFrame
    df = pd.DataFrame({'values': column_data}, index=range(num_indices))


    I have found this implementation that uses np.lib.stride_tricks, but it is a bit slow for my purpose. Does anyone have an idea for a faster method?

    def moving_median(signal,n=150):
    # Compute rolling median for valid windows
    swindow = np.lib.stride_tricks.sliding_window_view(signal, (n,))
    b = np.nanmedian(swindow, axis=1)
    b_full = np.concatenate([[np.nanmedian(signal)]*(n-1), b]) # Prepend first `n-1` values unchanged
    return signal - b_full


    And finally:

    df.iloc[:,0].apply(lambda x: moving_median(x))

    Continue reading...

Compartilhe esta Página