i have following code to train my NN model : %% from sklearn.preprocessing import…
- User
i have following code to train my NN model :
%%
from sklearn.preprocessing import StandardScaler import pandas as pd import numpy as np from tensorflow import keras import joblib
def calculate_targets_scaling_params(file_path): scaler = StandardScaler() for chunk in pd.read_csv(file_path, chunksize=10000): # Adjust chunksize based on your memory capacity filtered_chunk = chunk[['y_High_5d','y_Low_5d','y_Priority_5d']] # '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'
codescaler.partial_fit(filtered_chunk) # Accumulate means and variances return scaler.mean_, scaler.var_%%
x_scaler_loaded = joblib.load('nn_x_scaler.sav') y_scaler_loaded = joblib.load('nn_y_hlp5_scaler.sav')
def data_generator(file_path, batch_size, x_scaler, y_scaler): chunksize = batch_size
codewhile True: # Loop forever, so the generator never terminates for chunk in pd.read_csv(file_path, chunksize=chunksize): # if data_type == 'train' and row_counter >= train_rows: # continue # Skip the rest if we are fetching training data but have reached the end of the train set # elif data_type == 'val' and row_counter < train_rows: # row_counter += len(chunk) # continue # Skip this chunk if we are fetching validation data but are still in the train range # Assuming your CSV has headers that match features/targets # Normalizing the features filtered_c = chunk.drop(['Date', 'Symbol'], axis=1) feature_data = filtered_c.drop([ '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) target_data = filtered_c[['y_High_5d', 'y_Low_5d', 'y_Priority_5d' # , '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' ]] feature_data_scaled = pd.DataFrame(x_scaler.transform(feature_data), columns=feature_data.columns) # Assuming target_data also needs to be scaled, apply scaler separately target_data_scaled = pd.DataFrame(y_scaler.transform(target_data), columns=target_data.columns) # Now, feature_data_scaled and target_data_scaled are both DataFrames, scaled and ready to use yield feature_data_scaled.values, target_data_scaled.values # row_counter += len(chunk) - Assistant