%% import pandas as pd import datetime as dt from datetime import date import numpy as…
- User
%%
import pandas as pd import datetime as dt from datetime import date import numpy as np import tensorflow as tf from sklearn.preprocessing import MinMaxScaler import keras import os import joblib from tensorflow.keras.layers import Dense, Dropout, LSTM from tensorflow.keras.models import Sequential import gc import time import shutil from multiprocessing import Process import sys
csv_directory = r"C:\Users\arisa\Desktop\day_spot_summary_3" csv_files = [file for file in os.listdir(csv_directory) if file.endswith('.csv')] csv_file = csv_files[0]
look_back = 60
optimizer = keras.optimizers.Adam(learning_rate=0.0003,clipvalue=0.5)
for csv_file in csv_files:
file_path = os.path.join(csv_directory, csv_file) unique_part = file_path.split('_')[-2]
df = pd.read_csv(file_path)
include_substrings = ["y_"] exact_columns_to_keep = ["Open", "High", "Low", "Close","volume_base", "volume_crypto", "tradecount",] filtered_columns = [col for col in df.columns if any(col.startswith(s) for s in include_substrings)] columns_to_keep = list(set(exact_columns_to_keep + filtered_columns)) df = df[columns_to_keep] features = df.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)
Scale the features and targets
feature_scaler = MinMaxScaler(feature_range=(0, 1)) scaled_features = feature_scaler.fit_transform(features) joblib.dump(feature_scaler,f'x_scalers/x_{unique_part}_scaler.sav')
def train_model(size):
# Corrected to create a flat list of column names y_cols = [f'y_High_{size}d', f'y_Low_{size}d', f'y_Priority_{size}d'] # Now you can properly index df with y_cols targets = df[y_cols] # Continuing with your existing code… target_scaler = MinMaxScaler(feature_range=(0, 1)) scaled_targets = target_scaler.fit_transform(targets) joblib.dump(target_scaler,f'y_scalers/y{size}_{unique_part}_scaler.sav') x_train = [] y_train = [] for i in range(look_back, len(scaled_features)): x_train.append(scaled_features[i-look_back:i]) y_train.append(scaled_targets[i]) # Assuming the target is the next time step x_train, y_train = np.array(x_train), np.array(y_train) input_shape = (x_train.shape[1], x_train.shape[2]) model = Sequential() model.add(LSTM(units = 100, activation = 'tanh', return_sequences=True,input_shape = input_shape)) model.add(Dropout(0.4)) model.add(LSTM(units = 240, activation = 'tanh')) model.add(Dropout(0.5)) model.add(Dense(units = 3)) model.compile(optimizer=optimizer, loss = 'mean_squared_error', metrics=[tf.keras.metrics.MeanAbsoluteError()]) model.fit(x_train, y_train,epochs = 1000) mae = model.evaluate(x_train, y_train) loss_value = str(mae[0])[0:5] mae_value = str(mae[1])[0:5] base_filename = f'models/lstm_model_{unique_part}_y{size}_mae_{loss_value}_{mae_value}' filename = base_filename + '.h5' counter = 0 # Loop until a unique filename is found while os.path.exists(filename): counter += 1 filename = f"{base_filename}_dup{counter}.h5" model.save(filename)if name == 'main': intervals = [1,2,3,5] for size in intervals:
proc = Process(target=train_model, args=(size)) proc.start() proc.join()doned_file_path = os.path.join(r"C:\Users\arisa\Desktop\doned_path", csv_file) shutil.move(file_path,doned_file_path)
try: # This is for Windows systems if sys.platform == "win32": os.system(f'start /B {sys.executable} {"".join(sys.argv)}') else: # This is for Unix/Linux/MacOS systems os.execv(sys.executable, [sys.executable] + sys.argv) except Exception as e: # If the attempt to restart fails, you can fallback to this method print(f"Failed to restart: {e}") os.system(f'python {" ".join(sys.argv)}')
error: Traceback (most recent call last): File "c:\Users\arisa\Desktop\Stock-Price-Prediction-Using-LSTM-main\lstm_all.py", line 102, in proc = Process(target=train_model, args=(size)) File "C:\Users\arisa.conda\envs\tf\lib\multiprocessing\process.py", line 91, in init self._args = tuple(args) TypeError: 'int' object is not iterable
- Assistant
Your issue is related to how you're passing arguments to the
Processinstance fortrain_model. Specifically, when specifying theargsparameter, you need to ensure it is an iterable even if it's a single value. Python's multiprocessing library is expecting a tuple, list, or other iterable for theargsparameter, but you are passing an integer directly.Here is the part with the issue:
for size in intervals: proc = Process(target=train_model, args=(size)) proc.start() proc.join()To correct this, alter the
args=(size)to ensuresizeis passed as a single-element tuple. You do this by adding a comma aftersize, like so:for size in intervals: proc = Process(target=train_model, args=(size,)) proc.start() proc.join()With this change, Python now correctly interprets
(size,)as a tuple with one element, which resolves theTypeError: 'int' object is not iterableissue you encountered.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.