-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_baseline.py
More file actions
199 lines (161 loc) Β· 6.48 KB
/
run_baseline.py
File metadata and controls
199 lines (161 loc) Β· 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
"""
MITSUI&CO. Commodity Prediction Challenge - Baseline Model Runner
This script runs a baseline model to test the project setup and establish performance benchmarks.
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# Import our custom modules
import sys
sys.path.append('src')
from data_processing import (
load_data, fill_missing, add_lag_features,
add_rolling_features, add_calendar_features
)
from models import get_model
from cv import time_series_cv_split
from ensemble import simple_average, weighted_average
def main():
"""Run baseline model."""
print("π Running Baseline Model")
print("=" * 50)
# Load data
print("Loading data...")
try:
train, test, train_labels, target_pairs = load_data(
'data/train.csv', 'data/test.csv',
'data/train_labels.csv', 'data/target_pairs.csv'
)
print("β
Data loaded successfully")
except Exception as e:
print(f"β Error loading data: {e}")
print("Please run setup_project.py first to generate sample data")
return False
# Basic preprocessing
print("\nPreprocessing data...")
# Convert date_id to datetime
train['date_id'] = pd.to_datetime(train['date_id'])
test['date_id'] = pd.to_datetime(test['date_id'])
train_labels['date_id'] = pd.to_datetime(train_labels['date_id'])
# Sort by date
train = train.sort_values('date_id').reset_index(drop=True)
test = test.sort_values('date_id').reset_index(drop=True)
train_labels = train_labels.sort_values('date_id').reset_index(drop=True)
# Fill missing values
train = fill_missing(train, method='ffill')
test = fill_missing(test, method='ffill')
print(f"β
Train shape: {train.shape}")
print(f"β
Test shape: {test.shape}")
# Feature engineering
print("\nFeature engineering...")
# Identify numerical features
feature_cols = [col for col in train.columns if col != 'date_id']
# Add calendar features
train = add_calendar_features(train)
test = add_calendar_features(test)
# Add lag features
if len(feature_cols) > 0:
train = add_lag_features(train, feature_cols, lags=[1, 2, 3])
test = add_lag_features(test, feature_cols, lags=[1, 2, 3])
# Add rolling features
if len(feature_cols) > 0:
train = add_rolling_features(train, feature_cols, windows=[3, 7])
test = add_rolling_features(test, feature_cols, windows=[3, 7])
print(f"β
Final train shape: {train.shape}")
print(f"β
Final test shape: {test.shape}")
# Prepare features and target
feature_cols = [col for col in train.columns if col not in ['date_id', 'year', 'month', 'dayofweek']]
# Remove features with too many missing values
missing_pct = train[feature_cols].isnull().sum() / len(train)
feature_cols = [col for col in feature_cols if missing_pct[col] < 0.5]
print(f"β
Selected {len(feature_cols)} features")
# Prepare X and y
X_train = train[feature_cols].fillna(0)
X_test = test[feature_cols].fillna(0)
# For sample data, use the first target column
if 'target' in train_labels.columns:
y_train = train_labels['target'].values
elif 'target_0' in train_labels.columns:
y_train = train_labels['target_0'].values
print("βΉοΈ Using target_0 from sample data")
else:
print("β No target column found in train_labels")
print(f"Available columns: {train_labels.columns.tolist()}")
return False
# Define baseline models
print("\nTraining baseline models...")
models = {
'LightGBM': get_model('lgbm', {
'n_estimators': 100,
'learning_rate': 0.1,
'max_depth': 6,
'random_state': 42
}),
'XGBoost': get_model('xgb', {
'n_estimators': 100,
'learning_rate': 0.1,
'max_depth': 6,
'random_state': 42
}),
'Ridge': get_model('ridge', {
'alpha': 1.0,
'random_state': 42
})
}
# Train models on full training data
final_models = {}
predictions = {}
for model_name, model in models.items():
print(f"Training {model_name}...")
model.fit(X_train, y_train)
final_models[model_name] = model
# Generate predictions
predictions[model_name] = model.predict(X_test)
print(f"β
{model_name} trained and predictions generated")
# Create ensemble predictions
print("\nCreating ensemble predictions...")
ensemble_simple = simple_average(list(predictions.values()))
ensemble_weighted = weighted_average(list(predictions.values()))
print("β
Ensemble predictions created")
# Save results
print("\nSaving results...")
# Create submission format
submission = pd.DataFrame({
'date_id': test['date_id'],
'target': ensemble_simple
})
# Save to outputs directory
submission.to_csv('outputs/baseline_submission.csv', index=False)
print("β
Baseline submission saved to outputs/baseline_submission.csv")
# Save detailed results
detailed_results = pd.DataFrame({
'date_id': test['date_id'],
'target': ensemble_simple,
'lightgbm_pred': predictions['LightGBM'],
'xgboost_pred': predictions['XGBoost'],
'ridge_pred': predictions['Ridge'],
'ensemble_simple': ensemble_simple,
'ensemble_weighted': ensemble_weighted
})
detailed_results.to_csv('outputs/baseline_detailed_results.csv', index=False)
print("β
Detailed results saved to outputs/baseline_detailed_results.csv")
# Print summary
print("\n" + "=" * 50)
print("β
Baseline model completed successfully!")
print(f"π Number of features: {len(feature_cols)}")
print(f"π Training samples: {len(X_train)}")
print(f"π Test samples: {len(X_test)}")
print(f"π Models trained: {len(models)}")
print("\nπ Next Steps:")
print("1. Check the generated files in outputs/ directory")
print("2. Run cross-validation for proper evaluation")
print("3. Implement more sophisticated feature engineering")
print("4. Add hyperparameter tuning")
print("5. Create multi-target modeling pipeline")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)