Write a function strides() that given an array of tokens T as numpy array, using strided trick, returns [T[0:50], T[10:60], T[20: 70] ... ]
Write a function strides() that given an array of tokens T as numpy array, using strided trick, returns [T[0:50], T[10:60], T[20: 70] ... ]
To solve this problem, we need to generate overlapping windows from a given numpy array of tokens using strided tricks. Each window should start at intervals of 10 elements and have a length of 50 elements. The goal is to efficiently create these windows using numpy's strided functionality to avoid unnecessary memory usage. ### Approach 1. **Window Size and Step**: The window size is fixed at 50 elements, and each subsequent window starts 10 elements after the previous one. 2. **Check Validity**: If the array length is less than the window size, return an empty list since no valid window can be formed. 3. **Calculate Number of Windows**: Determine the number of valid windows using the formula `(len(T) - window_size) // step + 1`. 4. **Strided Array**: Use numpy's `as_strided` function to create a view of the array with the calculated shape and strides. This allows us to efficiently generate overlapping windows without copying data. ### Solution Code
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np from numpy.lib.stride_tricks import as_strided def strides(T): window_size = 50 step = 10 if len(T) < window_size: return [] num_windows = (len(T) - window_size) // step + 1 new_shape = (num_windows, window_size) new_strides = (step * T.strides[0], T.strides[0]) strided_view = as_strided(T, shape=new_shape, strides=new_strides) return list(strided_view) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np from numpy.lib.stride_tricks import as_strided def strides(T): window_size = 50 step = 10 if len(T) < window_size: return [] num_windows = (len(T) - window_size) // step + 1 new_shape = (num_windows, window_size) new_strides = (step * T.strides[0], T.strides[0]) strided_view = as_strided(T, shape=new_shape, strides=new_strides) return list(strided_view) |
Testing if the previous output contains the string as_strided
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import numpy as np from numpy.lib.stride_tricks import as_strided def strides(T): window_size = 50 step = 10 if len(T) < window_size: return [] num_windows = (len(T) - window_size) // step + 1 new_shape = (num_windows, window_size) new_strides = (step * T.strides[0], T.strides[0]) strided_view = as_strided(T, shape=new_shape, strides=new_strides) return list(strided_view) answer = strides(np.arange(100)).shape expected = (6, 50) assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = strides(np.arange(100)).sum() expected = 14850 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 20, in <module>
answer = strides(np.arange(100)).shape
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'shape'
Testing if the previous output contains the string All tests passed
: False