pytorch lstm time series code example
Example: torch timeseries
from sklearn.preprocessing import MinMaxScaler
"""
This has to be done outside the function definition so that
we can inverse_transform the prediction set later on.
"""
scaler = MinMaxScaler(feature_range=(-1, 1))
df = pd.read_csv('../Data/TimeSeriesData/Alcohol_Sales.csv',index_col=0,parse_dates=True)
y = df['S4248SM144NCEN'].values.astype(float)
test_size = 12
train_set = y[:-test_size]
def create_train_data(seq,ws=12):
"""Takes in a training sequence and window size (ws) of
default size 12, returns a tensor of (seq/label) tuples"""
seq_norm = scaler.fit_transform(seq.reshape(-1, 1))
seq_norm = torch.FloatTensor(seq_norm).view(-1)
out = []
L = len(seq_norm)
for i in range(L-ws):
window = seq_norm[i:i+ws]
label = seq_norm[i+ws:i+ws+1]
out.append((window,label))
return out
train_data = create_train_data(train_set,12)
len(train_data)