jinglescode / time-series-forecasting-pytorch

Acquiring data from Alpha Vantage and predicting stock prices with PyTorch's LSTM
https://www.alphavantage.co/academy/#lstm-for-finance
Apache License 2.0
203 stars 84 forks source link

Additional features #5

Open Dextergellizeau opened 3 years ago

Dextergellizeau commented 3 years ago

Hello,

Please show how to add additional features (such as a technical indicator) to the script to help in prediction.

jinglescode commented 3 years ago

That's mostly data preparation, there are many ways to do it. Your aim is to include features in data_x_train and data_x_val, before creating the PyTorch Dataset at line 167:

dataset_train = TimeSeriesDataset(data_x_train, data_y_train)
dataset_val = TimeSeriesDataset(data_x_val, data_y_val)

You also need to remove at line 155:

x = np.expand_dims(x, 2)  # in our case, we have only 1 feature, so we need to convert `x` into [batch, sequence, features] for LSTM
Dextergellizeau commented 3 years ago

Hello Hong,

Thanks for your quick response, however I am not getting it to work. I tried changing the following: Note fea is the dataset with the additional features.

class Normalizer_Fea(): def init(self): self.mu = None self.sd = None

def fit_transform_Fea(self, x):
    self.mu = np.mean(x, axis=(0), keepdims=True)
    self.sd = np.std(x, axis=(0), keepdims=True)
    normalized_x = (x - self.mu)/self.sd
    return normalized_x

def inverse_transform_Fea(self, x):
    return (x*self.sd) + self.mu

normalize features

scaler_fea = Normalizer_Fea() normalized_fea = scaler_fea.fit_transform_Fea(fea)

def prepare_data_x(x, window_size):

perform windowing

n_row = x.shape[0] - window_size + 1
output = np.lib.stride_tricks.as_strided(x, shape=(n_row, window_size), strides=(x.strides[0], x.strides[0]))
return output[:-1], output[-1]

def prepare_data_y(x, window_size):

perform simple moving average

# output = np.convolve(x, np.ones(window_size), 'valid') / window_size

# use the next day as label
output = x[window_size:]
return output

data_x, data_x_unseen = prepare_data_x(normalized_fea, window_size=config["data"]["window_size"])

data_y = prepare_data_y(normalized_data_close_price, window_size=config["data"]["window_size"])

split_index = int(data_y.shape[0]*config["data"]["train_split_size"]) data_x_train = data_x[:split_index, :] data_x_val = data_x[split_index:, :]

data_y_train = data_y[:split_index] data_y_val = data_y[split_index:]

Then I applied your changes and now receiving this error:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x16 and 1x32)

Regards,

Dexter Gellizeau

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10

From: Hong Jing @.> Sent: Thursday, June 10, 2021 11:20 PM To: @.> Cc: @.>; @.> Subject: Re: [jinglescode/time-series-forecasting-pytorch] Additional features (#5)

That's mostly data preparation, there are many ways to do it. Your aim is to include features in data_x_train and data_x_val, before creating the PyTorch Dataset at line 167https://github.com/jinglescode/time-series-forecasting-pytorch/blob/main/project.py#L167:

dataset_train = TimeSeriesDataset(data_x_train, data_y_train)

dataset_val = TimeSeriesDataset(data_x_val, data_y_val)

You also need to remove at line 155https://github.com/jinglescode/time-series-forecasting-pytorch/blob/main/project.py#L155:

x = np.expand_dims(x, 2) # in our case, we have only 1 feature, so we need to convert x into [batch, sequence, features] for LSTM

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/jinglescode/time-series-forecasting-pytorch/issues/5#issuecomment-859232929, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AUN6P7U2UQPGBOEQLX6CVN3TSF6IVANCNFSM46PT6MFA.

Piyush-Bijwal commented 2 years ago

hi @Dextergellizeau , were you able to get the code running? i'm also trying to do the same but not able to get the code running. Regards, Piyush