torch timeseries

Solutions on MaxInterview for torch timeseries by the best coders in the world

showing results for - "torch timeseries"
Arianna
18 Jan 2019
1# Load dependencies
2from sklearn.preprocessing import MinMaxScaler
3
4# Instantiate a scaler
5"""
6This has to be done outside the function definition so that
7we can inverse_transform the prediction set later on.
8"""
9scaler = MinMaxScaler(feature_range=(-1, 1))
10
11# Extract values from the source .csv file
12df = pd.read_csv('../Data/TimeSeriesData/Alcohol_Sales.csv',index_col=0,parse_dates=True)
13y = df['S4248SM144NCEN'].values.astype(float)
14
15# Define a test size
16test_size = 12
17
18# Create the training set of values
19train_set = y[:-test_size]
20
21# DEFINE A FUNCTION:
22def create_train_data(seq,ws=12):
23    """Takes in a training sequence and window size (ws) of
24       default size 12, returns a tensor of (seq/label) tuples"""
25    seq_norm = scaler.fit_transform(seq.reshape(-1, 1))    
26    seq_norm = torch.FloatTensor(seq_norm).view(-1)
27
28    out = []
29    L = len(seq_norm)
30    for i in range(L-ws):
31        window = seq_norm[i:i+ws]
32        label = seq_norm[i+ws:i+ws+1]
33        out.append((window,label))
34    return out
35
36# Apply the function to train_set
37train_data = create_train_data(train_set,12)
38len(train_data)  # this should equal 313-12