my code : %% import numpy as np import pandas as pd from tensorflow.keras.models import…
- User
my code :
%%
import numpy as np import pandas as pd from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense from tensorflow.keras.utils import Sequence from sklearn.preprocessing import MinMaxScaler import os
%%
class DataGenerator(Sequence): def init(self, file_list, batch_size, n_steps=30, scale=True, shuffle=False): self.file_list = file_list self.batch_size = batch_size self.n_steps = n_steps self.scale = scale self.shuffle = shuffle self.scalers = {}
def __len__(self): # Calculate the total number of samples dynamically total = 0 for file in self.file_list: df = pd.read_csv(file).drop([ 'Date', 'Symbol', 'y_High_1d', 'y_Low_1d', 'y_Priority_1d', 'y_High_2d', 'y_Low_2d', 'y_Priority_2d', 'y_High_3d', 'y_Low_3d', 'y_Priority_3d', 'y_High_5d', 'y_Low_5d', 'y_Priority_5d'], axis=1) total += df.shape[0] - self.n_steps return total // self.batch_size def __getitem__(self, idx): batch_x = [] batch_y = [] for file in self.file_list: df = pd.read_csv(file).drop([ 'Date', 'Symbol', 'y_High_1d', 'y_Low_1d', 'y_Priority_1d', 'y_High_2d', 'y_Low_2d', 'y_Priority_2d', 'y_High_3d', 'y_Low_3d', 'y_Priority_3d', 'y_High_5d', 'y_Low_5d', 'y_Priority_5d'], axis=1) if self.scale: if file not in self.scalers: scaler = MinMaxScaler(feature_range=(0, 1)) self.scalers[file] = scaler.fit(df) else: scaler = self.scalers[file] df = scaler.transform(df) # Generate batches based on the number of steps for i in range(self.n_steps, len(df)): batch_x.append(df[i-self.n_steps:i].values) batch_y.append(df.iloc[i]['y_High_1d']) if len(batch_x) >= self.batch_size: return np.array(batch_x), np.array(batch_y) if self.shuffle: c = list(zip(batch_x, batch_y)) np.random.shuffle batch_x, batch_y = zip(*c) return np.array(batch_x), np.array(batch_y) def __on_epoch_end__(self): if self.shuffle: np.random.shuffle(self.file_list)%%
def build_model(input_shape): model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=input_shape)) # model.add(LSTM(units=50)) model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error') return model%%
data_path =r'F:\day_spot' file_list = [os.path.join(data_path, f) for f in os.listdir(data_path) if f.endswith('.csv')]
%%
n_features = 6427 batch_size = 32
%%
data_gen = DataGenerator(file_list=file_list, batch_size=batch_size, n_steps=30, scale=True, shuffle=False) model = build_model((30, n_features)) # 30 days look-back, 6427 features
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.