30 sheets of excel file with variance fluctuating at days and i want variance around 5%sizable dataset in the sense mine is around totally 6 3 excel files around 30 sheets with 50 rows and 50 columns is it good for lstmits around 4 years give best 2 models which i should consider.ok give a clear format i need to keep my data as i need to combine from different sheets for both these models before inputing (lstm and xgboost)ok the format i have these rows (export data) in my dataset fy25actual,fy24actual,fy23actual,fy22actual,sales,cases,average sales price,forecast25,forecast24, forecast23,variance25,variance 24,variance 23,inflation rate,8 warehouses names , runrate as rows and daily date as columns in different files so which format should i make it as final for the models
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Let's break down the steps to prepare your dataset for both LSTM and XGBoost models. Given the complexity of your data, which spans multiple Excel files and sheets, we need to consolidate and format it correctly.
Step 1: Consolidate Data
Combine Sheets and Files:
You have 3 Excel files, each with around 30 sheets.
Each sheet has 50 rows and 50 columns.
The rows include: fy25actual, fy24actual, fy23actual, fy22actual, sales, cases, average sales price, forecast25, forecast24, forecast23, variance25, variance24, variance23, inflation rate, 8 warehouse names, runrate, and daily date as columns.
First, you need to combine all these sheets into a single DataFrame. Here’s a Python script using pandas to do that:
python
import pandas as pd
import glob
# List to hold all DataFrames
dfs = []
# Loop through each Excel file
for file in glob.glob("path_to_your_excel_files/*.xlsx"):
# Read all sheets in the Excel file
xl = pd.ExcelFile(file)
for sheet in xl.sheet_names:
df = xl.parse(sheet)
dfs.append(df)
# Concatenate all DataFrames
combined_df = pd.concat(dfs, ignore_index=True)
# Save the combined DataFrame to a new Excel file (optional)
combined_df.to_excel("combined_data.xlsx", index=False)
Step 2: Data Cleaning and Formatting
Reshape Data:
Your data has daily dates as columns, which is not suitable for time-series models like LSTM. We need to reshape it so that each row represents a single day's data.
Here’s how to reshape the data:
python
import pandas as pd
# Load the combined data
combined_df = pd.read_excel("combined_data.xlsx")
# Identify the date columns
date_columns = [col for col in combined_df.columns if 'daily date' not in col.lower()]
# Reshape the DataFrame
melted_df = combined_df.melt(id_vars=[ 'fy25actual','fy24actual','fy23actual','fy22actual', 'sales', 'cases', 'average sales price',
'forecast25', 'forecast24', 'forecast23', 'variance25', 'variance24', 'variance23',
'inflation rate', 'warehouse1', 'warehouse2', 'warehouse3', 'warehouse4',
'warehouse5', 'warehouse6', 'warehouse7', 'warehouse8', 'runrate'],
var_name='daily_date',
value_name='value')
# Convert daily_date to datetime
melted_df['daily_date'] = pd.to_datetime(melted_df['daily_date'])
# Sort the DataFrame by date
final_df = melted_df.sort_values(by='daily_date').reset_index(drop=True)
# Save the final DataFrame to a new Excel file (optional)
final_df.to_excel("final_data.xlsx", index=False)
Step 3: Prepare Data for Models
For LSTM:
LSTM requires data to be in a sequential format. We need to create sequences of a certain length (e.g., 60 days) as input and the next day as the target.
Normalise the Data:
Normalise the features to ensure that the LSTM can train effectively.
Create Sequences:
Here’s a script to prepare the data for LSTM:
python
import numpy as np
from sklearn.preprocessing import MinMaxScaler
# Load the final data
final_df = pd.read_excel("final_data.xlsx")
# Drop rows with missing values
final_df.dropna(inplace=True)
# Features and target
features = final_df.drop(columns=['daily_date', 'value']) # Assuming 'value' is the target
target = final_df['value']
# Normalise the features
scaler = MinMaxScaler()
scaled_features = scaler.fit_transform(features)
# Function to create sequences
def create_sequences(data, target, seq_length):
xs, ys = [], []
for i in range(len(data) - seq_length):
x = data[i:i+seq_length]
y = target[i+seq_length]
xs.append(x)
ys.append(y)
return np.array(xs), np.array(ys)
seq_length = 60 # e.g., 60 days
X, y = create_sequences(scaled_features, target, seq_length)
# Split into training and testing sets
split = int(0.8 * len(X))
X_train, X_test = X[:split], X[split:]
y_train, y_test = y[:split], y[split:]
For XGBoost:
XGBoost does not require sequential data, so we can use the reshaped DataFrame directly. However, we should still normalise the features.
Normalise the Data:
Normalise the features to ensure that the XGBoost can train effectively.
Here’s a script to prepare the data for XGBoost:
python
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
# Load the final data
final_df = pd.read_excel("final_data.xlsx")
# Drop rows with missing values
final_df.dropna(inplace=True)
# Features and target
X = final_df.drop(columns=['daily_date', 'value']) # Assuming 'value' is the target
y = final_df['value']
# Normalise the features
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
# Convert to DMatrix for XGBoost
train_dmatrix = xgb.DMatrix(X_train, label=y_train)
test_dmatrix = xgb.DMatrix(X_test, label=y_test)
Final Data Format
For both models, your final data should look like this:
LSTM:
X_train and X_test: Arrays of shape (num_samples, seq_length, num_features)
y_train and y_test: Arrays of shape (num_samples,)
XGBoost:
train_dmatrix and test_dmatrix: XGBoost DMatrix objects with normalised features and targets.
Columns in Final DataFrame
Your final DataFrame (final_df) should have the following columns:
daily_date: Datetime column representing the day.
fy25actual, fy24actual, fy23actual, fy22actual: Actual values for the fiscal years.
sales, cases, average sales price: Sales-related features.
forecast25, forecast24, forecast23: Forecast values for the fiscal years.
variance25, variance24, variance23: Variance values for the fiscal years.
inflation_rate: Inflation rate.
warehouse1, warehouse2, ..., warehouse8: Features for each warehouse.
runrate: Run rate.
value: The target variable (e.g., sales, cases, or another metric you want to predict).
This format ensures that your data is correctly prepared for both LSTM and XGBoost models.