Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy Array Reshaping takes 3 hours. Is there any way to make it faster?

I have written a function that reshapes the numpy array per given window length.
The function does the following.

Array Reshaping

My function as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def w_s(df, l):
    """
    Convert numpy array into desired shape with lag 1.
    Args:
        df (numpy.ndarray): Numpy array.
        l (integer): Length of the sample window.
    
    Returns:
        Returns numpy array in a desired shape to be used in decision trees.
    """
    data = np.zeros((l, 1))
    data = np.append(data, df)
    data = data[l:]
    for i in range(1, l):
        s1 = np.roll(df,0-i)
        data = np.append(data,s1)
    data = data.reshape(l, len(df)).T
    
    return data[:-(l-1)]

I have two arrays with the length of 1780000. The function takes around 3 hours.

CPU times: user 5min 20s, sys: 43min 45s, total: 49min 6s Wall time: 3h 5min 46s

My machine is Mac M1. I am running this on Jupyter cell where server runs on Firefox. How can I do this faster?

>Solution :

That’s sliding_window_view:

>>> import numpy as np

>>> arr = np.arange(10)
>>> np.lib.stride_tricks.sliding_window_view(arr, 5)
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8],
       [5, 6, 7, 8, 9]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading