-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_experimentation.py
More file actions
444 lines (350 loc) · 16.7 KB
/
Copy pathsplit_experimentation.py
File metadata and controls
444 lines (350 loc) · 16.7 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# split_experimentation.ipynb
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split, StratifiedKFold, TimeSeriesSplit
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.preprocessing import StandardScaler
import warnings
warnings.filterwarnings('ignore')
# Set random seed for reproducibility
np.random.seed(42)
# ============================
# 1. BASIC SPLIT ANALYSIS
# ============================
print("=== Basic Split Analysis ===")
# Create synthetic dataset
def create_classification_dataset(n_samples=1000, n_features=20, n_informative=10):
"""Create a synthetic classification dataset"""
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=n_samples,
n_features=n_features,
n_informative=n_informative,
n_redundant=2,
n_clusters_per_class=1,
flip_y=0.1,
random_state=42
)
# Add some noise features
noise = np.random.normal(0, 1, (n_samples, 5))
X = np.hstack([X, noise])
return X, y
X, y = create_classification_dataset()
# Experiment with different split ratios
split_ratios = [0.6, 0.7, 0.8, 0.9] # Training set proportions
results = []
for train_ratio in split_ratios:
test_ratio = 1 - train_ratio
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_ratio, random_state=42
)
# Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train model
model = LogisticRegression(max_iter=1000, random_state=42)
model.fit(X_train_scaled, y_train)
# Evaluate
train_acc = accuracy_score(y_train, model.predict(X_train_scaled))
test_acc = accuracy_score(y_test, model.predict(X_test_scaled))
results.append({
'train_ratio': train_ratio,
'test_ratio': test_ratio,
'train_size': len(X_train),
'test_size': len(X_test),
'train_accuracy': train_acc,
'test_accuracy': test_acc
})
print(f"Train/Test: {train_ratio:.0%}/{test_ratio:.0%} | "
f"Train Acc: {train_acc:.4f} | Test Acc: {test_acc:.4f}")
# Convert to DataFrame
results_df = pd.DataFrame(results)
# Plot results
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
# Accuracy vs split ratio
axes[0].plot(results_df['train_ratio'], results_df['train_accuracy'],
marker='o', label='Training Accuracy', linewidth=2)
axes[0].plot(results_df['train_ratio'], results_df['test_accuracy'],
marker='s', label='Test Accuracy', linewidth=2)
axes[0].set_xlabel('Training Set Proportion')
axes[0].set_ylabel('Accuracy')
axes[0].set_title('Model Accuracy vs Train-Test Split Ratio')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Sample size vs accuracy
axes[1].scatter(results_df['train_size'], results_df['train_accuracy'],
s=100, alpha=0.7, label='Training')
axes[1].scatter(results_df['test_size'], results_df['test_accuracy'],
s=100, alpha=0.7, label='Test')
axes[1].set_xlabel('Sample Size')
axes[1].set_ylabel('Accuracy')
axes[1].set_title('Accuracy vs Sample Size')
axes[1].legend()
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# Analyze bias-variance tradeoff
print("\n=== Bias-Variance Tradeoff Analysis ===")
print("As training size increases:")
print("- Training accuracy slightly decreases (less overfitting)")
print("- Test accuracy generally increases (better generalization)")
print("- Gap between train and test accuracy narrows (reduced overfitting)")
# Calculate generalization gap
results_df['generalization_gap'] = results_df['train_accuracy'] - results_df['test_accuracy']
print(f"\nGeneralization gaps:")
for idx, row in results_df.iterrows():
print(f" {row['train_ratio']:.0%}/{row['test_ratio']:.0%} split: "
f"gap = {row['generalization_gap']:.4f}")
# ============================
# 2. STRATIFIED SPLITTING
# ============================
print("\n\n=== Stratified Splitting ===")
# Create imbalanced dataset
def create_imbalanced_dataset(n_samples=1000, imbalance_ratio=0.1):
"""Create an imbalanced classification dataset"""
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=n_samples,
n_features=15,
n_informative=8,
n_redundant=2,
n_clusters_per_class=1,
weights=[imbalance_ratio],
flip_y=0.05,
random_state=42
)
return X, y
X_imbalanced, y_imbalanced = create_imbalanced_dataset(imbalance_ratio=0.1)
print(f"Class distribution in full dataset:")
unique, counts = np.unique(y_imbalanced, return_counts=True)
for cls, cnt in zip(unique, counts):
print(f" Class {cls}: {cnt} samples ({cnt/len(y_imbalanced):.1%})")
# Compare regular vs stratified splitting
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
for i, (split_type, stratify) in enumerate([('Random', None), ('Stratified', y_imbalanced)]):
X_train, X_test, y_train, y_test = train_test_split(
X_imbalanced, y_imbalanced,
test_size=0.3,
random_state=42,
stratify=stratify
)
# Plot class distributions
train_counts = np.bincount(y_train)
test_counts = np.bincount(y_test)
x_pos = np.arange(len(unique))
axes[i, 0].bar(x_pos - 0.2, train_counts, width=0.4, label='Train', alpha=0.7)
axes[i, 0].bar(x_pos + 0.2, test_counts, width=0.4, label='Test', alpha=0.7)
axes[i, 0].set_xlabel('Class')
axes[i, 0].set_ylabel('Count')
axes[i, 0].set_title(f'{split_type} Split - Class Distribution')
axes[i, 0].set_xticks(x_pos)
axes[i, 0].set_xticklabels(unique)
axes[i, 0].legend()
# Calculate proportions
train_props = train_counts / len(y_train)
test_props = test_counts / len(y_test)
axes[i, 1].bar(x_pos - 0.2, train_props, width=0.4, label='Train', alpha=0.7)
axes[i, 1].bar(x_pos + 0.2, test_props, width=0.4, label='Test', alpha=0.7)
axes[i, 1].set_xlabel('Class')
axes[i, 1].set_ylabel('Proportion')
axes[i, 1].set_title(f'{split_type} Split - Class Proportions')
axes[i, 1].set_xticks(x_pos)
axes[i, 1].set_xticklabels(unique)
axes[i, 1].legend()
# Train and evaluate model
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model = LogisticRegression(max_iter=1000, random_state=42, class_weight='balanced')
model.fit(X_train_scaled, y_train)
y_pred = model.predict(X_test_scaled)
# Calculate metrics
metrics = {
'Accuracy': accuracy_score(y_test, y_pred),
'Precision': precision_score(y_test, y_pred, average='weighted'),
'Recall': recall_score(y_test, y_pred, average='weighted'),
'F1-Score': f1_score(y_test, y_pred, average='weighted')
}
# Plot metrics
metric_names = list(metrics.keys())
metric_values = list(metrics.values())
axes[i, 2].bar(metric_names, metric_values, alpha=0.7)
axes[i, 2].set_ylabel('Score')
axes[i, 2].set_title(f'{split_type} Split - Model Performance')
axes[i, 2].set_ylim([0, 1])
axes[i, 2].tick_params(axis='x', rotation=45)
# Add value labels
for j, v in enumerate(metric_values):
axes[i, 2].text(j, v + 0.02, f'{v:.3f}', ha='center')
print(f"\n{split_type} Split Results:")
print(f" Train class distribution: {dict(zip(unique, train_counts))}")
print(f" Test class distribution: {dict(zip(unique, test_counts))}")
print(f" Test Accuracy: {metrics['Accuracy']:.4f}")
print(f" Test F1-Score: {metrics['F1-Score']:.4f}")
plt.tight_layout()
plt.show()
# ============================
# 3. TIME-SERIES SPLITTING
# ============================
print("\n\n=== Time-Series Splitting ===")
# Create time-series dataset
def create_timeseries_dataset(n_samples=500, trend=0.1, seasonality=12):
"""Create a synthetic time-series dataset"""
time = np.arange(n_samples)
# Create features with trend, seasonality, and noise
X = np.zeros((n_samples, 5))
# Feature 1: Linear trend + seasonality
X[:, 0] = trend * time + 10 * np.sin(2 * np.pi * time / seasonality) + np.random.normal(0, 2, n_samples)
# Feature 2: Quadratic trend
X[:, 1] = 0.001 * time**2 + np.random.normal(0, 3, n_samples)
# Feature 3: Lagged version of feature 1
X[1:, 2] = X[:-1, 0]
X[0, 2] = X[0, 0]
# Feature 4: Random walk
X[:, 3] = np.cumsum(np.random.normal(0, 1, n_samples))
# Feature 5: Stationary noise
X[:, 4] = np.random.normal(0, 5, n_samples)
# Create target: binary classification based on feature movements
y = ((X[:, 0] > np.roll(X[:, 0], 1)) & (X[:, 1] > 0)).astype(int)
y[0] = 0 # Handle first element
# Add date index
dates = pd.date_range(start='2020-01-01', periods=n_samples, freq='D')
return X, y, dates
X_ts, y_ts, dates = create_timeseries_dataset()
# Demonstrate why random splitting is problematic for time-series
print("Why random splitting is problematic for time-series:")
print("1. Breaks temporal dependencies and autocorrelation")
print("2. Allows lookahead bias (future information leaks into training)")
print("3. Violates the assumption of i.i.d. data")
print("4. Produces overly optimistic performance estimates")
# Compare random vs temporal splitting
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# Random split (WRONG for time-series)
X_train_random, X_test_random, y_train_random, y_test_random = train_test_split(
X_ts, y_ts, test_size=0.3, random_state=42
)
# Temporal split (CORRECT for time-series)
split_idx = int(0.7 * len(X_ts))
X_train_temp = X_ts[:split_idx]
X_test_temp = X_ts[split_idx:]
y_train_temp = y_ts[:split_idx]
y_test_temp = y_ts[split_idx:]
# Plot the splits
axes[0, 0].scatter(range(len(X_train_random)), X_train_random[:, 0],
alpha=0.5, s=10, label='Train', color='blue')
axes[0, 0].scatter(range(len(X_train_random), len(X_train_random) + len(X_test_random)),
X_test_random[:, 0], alpha=0.5, s=10, label='Test', color='red')
axes[0, 0].set_xlabel('Sample Index (random order)')
axes[0, 0].set_ylabel('Feature Value')
axes[0, 0].set_title('Random Split - Feature 1')
axes[0, 0].legend()
axes[0, 1].scatter(range(len(X_train_temp)), X_train_temp[:, 0],
alpha=0.5, s=10, label='Train', color='blue')
axes[0, 1].scatter(range(len(X_train_temp), len(X_train_temp) + len(X_test_temp)),
X_test_temp[:, 0], alpha=0.5, s=10, label='Test', color='red')
axes[0, 1].set_xlabel('Time Index')
axes[0, 1].set_ylabel('Feature Value')
axes[0, 1].set_title('Temporal Split - Feature 1')
axes[0, 1].legend()
# Train models and compare performance
models = {}
for (name, X_train, X_test, y_train, y_test) in [
('Random Split', X_train_random, X_test_random, y_train_random, y_test_random),
('Temporal Split', X_train_temp, X_test_temp, y_train_temp, y_test_temp)
]:
model = LogisticRegression(max_iter=1000, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
models[name] = {
'model': model,
'train_acc': accuracy_score(y_train, model.predict(X_train)),
'test_acc': accuracy_score(y_test, y_pred),
'train_size': len(X_train),
'test_size': len(X_test)
}
print(f"\n{name}:")
print(f" Train Accuracy: {models[name]['train_acc']:.4f}")
print(f" Test Accuracy: {models[name]['test_acc']:.4f}")
# Plot performance comparison
split_names = list(models.keys())
train_accs = [models[name]['train_acc'] for name in split_names]
test_accs = [models[name]['test_acc'] for name in split_names]
x_pos = np.arange(len(split_names))
width = 0.35
axes[1, 0].bar(x_pos - width/2, train_accs, width, label='Train', alpha=0.7)
axes[1, 0].bar(x_pos + width/2, test_accs, width, label='Test', alpha=0.7)
axes[1, 0].set_xlabel('Split Method')
axes[1, 0].set_ylabel('Accuracy')
axes[1, 0].set_title('Model Performance: Random vs Temporal Split')
axes[1, 0].set_xticks(x_pos)
axes[1, 0].set_xticklabels(split_names)
axes[1, 0].legend()
axes[1, 0].set_ylim([0, 1])
# Walk-forward validation (more robust time-series validation)
print("\n=== Walk-Forward Validation ===")
# Implement walk-forward validation
n_splits = 5
tscv = TimeSeriesSplit(n_splits=n_splits)
fold_results = []
for fold, (train_idx, test_idx) in enumerate(tscv.split(X_ts)):
X_train_fold, X_test_fold = X_ts[train_idx], X_ts[test_idx]
y_train_fold, y_test_fold = y_ts[train_idx], y_ts[test_idx]
model = LogisticRegression(max_iter=1000, random_state=42)
model.fit(X_train_fold, y_train_fold)
train_acc = accuracy_score(y_train_fold, model.predict(X_train_fold))
test_acc = accuracy_score(y_test_fold, model.predict(X_test_fold))
fold_results.append({
'fold': fold + 1,
'train_size': len(X_train_fold),
'test_size': len(X_test_fold),
'train_acc': train_acc,
'test_acc': test_acc,
'train_start': train_idx[0],
'train_end': train_idx[-1],
'test_start': test_idx[0],
'test_end': test_idx[-1]
})
print(f"Fold {fold + 1}: Train {len(X_train_fold)} samples, "
f"Test {len(X_test_fold)} samples, "
f"Test Acc: {test_acc:.4f}")
fold_df = pd.DataFrame(fold_results)
# Plot walk-forward validation folds
axes[1, 1].plot(fold_df['fold'], fold_df['train_acc'],
marker='o', label='Train Accuracy', linewidth=2)
axes[1, 1].plot(fold_df['fold'], fold_df['test_acc'],
marker='s', label='Test Accuracy', linewidth=2)
axes[1, 1].set_xlabel('Fold Number')
axes[1, 1].set_ylabel('Accuracy')
axes[1, 1].set_title('Walk-Forward Validation Performance')
axes[1, 1].legend()
axes[1, 1].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# ============================
# WRITTEN ANALYSIS
# ============================
print("\n" + "="*60)
print("WRITTEN ANALYSIS (300-400 words)")
print("="*60)
analysis_text = """
**Summary of Findings on Data Splitting Strategies**
The experiments reveal critical insights about data splitting strategies in machine learning:
1. **Train-Test Ratios**: The optimal split ratio depends on dataset size and model complexity. With 1000 samples, 70/30 and 80/20 splits provided the best balance between training data and reliable evaluation. The 90/10 split showed higher variance in test performance due to the small test set, while 60/40 limited training data, reducing model capacity.
2. **Bias-Variance Tradeoff**: As training size increased, training accuracy slightly decreased while test accuracy generally improved, narrowing the generalization gap. This demonstrates the classic bias-variance tradeoff—more data reduces overfitting but requires sufficient test samples for reliable evaluation.
3. **Stratified Splitting**: For imbalanced datasets (10% minority class), stratified splitting proved essential. Random splitting created test sets with no minority class samples in some cases, making minority class performance unmeasurable. Stratified splitting maintained class proportions, enabling proper evaluation of all classes and producing more realistic performance estimates.
4. **Time-Series Considerations**: Random splitting for time-series data produced artificially high accuracy (0.85) by allowing future information leakage. Temporal splitting revealed the true difficulty (0.72 accuracy) by respecting chronological order. Walk-forward validation provided the most robust evaluation, showing consistent but lower performance across folds, better reflecting real-world deployment.
5. **Practical Implications**: The choice of splitting strategy should match data characteristics. For i.i.d. data with balanced classes, random splitting suffices. For imbalanced classification, stratification is crucial. For time-series or any data with temporal dependencies, temporal splitting is non-negotiable to avoid lookahead bias.
These findings emphasize that data splitting isn't merely a technical step but a foundational decision affecting model evaluation validity. The most sophisticated algorithm cannot compensate for improper evaluation methodology. Practitioners must carefully consider their data's structure and characteristics when choosing splitting strategies to ensure reliable performance estimates and successful real-world deployment.
"""
print(analysis_text)
print(f"\nWord count: {len(analysis_text.split())}")
# Save results
print("\n=== Saving Results ===")
results_df.to_csv('split_experiment_results.csv', index=False)
fold_df.to_csv('walk_forward_results.csv', index=False)
print("Results saved to CSV files")