In [82]:
import numpy as np
from timeit import timeit
from numpy.lib.stride_tricks import as_strided
In [83]:
prices = np.random.randn(100)
In [84]:
prices.shape
Out[84]:
(100,)
In [85]:
window = 10
In [87]:
def make_X1():
    X1 = np.zeros([len(prices) - window, window])
    for day in range(len(X1)):
        X1[day, : ] = prices[day: day+window]
    return X1

In [81]:

def make_X2(): stride, = prices.strides desited_shape = [ len(prices) - window , window ] X2 = as_strided( prices, desited_shape, strides = [stride, stride], writeable = False ) return X2


In [88]:
timeit(make_X1)
Out[88]:
59.9649412



[ i for i in range ( 101 )] 
In [163]:
prices = [[ 1,2,3,4,5 ],[ 5, 4, 3, 2, 1 ],[ 1, 2, 3, 4, 5 ],[ 5, 4, 3, 2, 1 ],[ 1, 2, 3, 4, 5 ]]
In [168]:
X3 = as_strided(  prices, (2, 2), strides = [ 20 , 4], writeable = True  )

In [169]:
X3
Out[169]:
array([[1, 2],
       [5, 4]])


+ Recent posts