This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create a function to reshape a 1d array using a sliding window with a step. | |
| # NOTE: The function uses numpy's internat as_strided function because looping in python is slow in comparison. | |
| # Adopted from http://www.rigtorp.se/2011/01/01/rolling-statistics-numpy.html and | |
| # https://gist.github.com/codehacken/708f19ae746784cef6e68b037af65788 | |
| import numpy as np | |
| # Reshape a numpy array 'a' of shape (x) to form shape((n - window_size) // step + 1, window_size)) | |
| def rolling_window(a, window, step): | |
| shape = a.shape[:-1] + ((a.shape[-1] - window + 1)//step, window) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| MIT License | |
| Copyright (c) 2018 Fanjin Zeng | |
| This work is licensed under the terms of the MIT license, see <https://opensource.org/licenses/MIT>. | |
| ''' | |
| def sliding_window_view(x, shape, step=None, subok=False, writeable=False): | |
| """ | |
| Create sliding window views of the N dimensions array with the given window | |
| shape. Window slides across each dimension of `x` and provides subsets of `x` |