-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_create_solutions.py
More file actions
442 lines (350 loc) · 15.5 KB
/
batch_create_solutions.py
File metadata and controls
442 lines (350 loc) · 15.5 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
#!/usr/bin/env python3
"""Batch create remaining solution files."""
import os
base_dir = "/home/user/Data-Analysis-with-Chatbots/kaggle_solutions/07_special_domains"
# Remaining solutions with comprehensive templates
solutions = {
"27_climate_weather_forecasting": ("""\"\"\"
Climate Modeling and Weather Forecasting
==========================================
Domain: Scientific Computing & Meteorology
Task: Weather pattern prediction and climate trend analysis
This solution demonstrates:
- Time series forecasting for weather data
- Multi-variate climate modeling
- Seasonal decomposition
- Extreme weather event detection
- Climate trend analysis
- Spatial interpolation of weather data
\"\"\"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from scipy import signal
import warnings
warnings.filterwarnings('ignore')
class ClimateForecaster:
\"\"\"Weather forecasting and climate analysis system.\"\"\"
def __init__(self):
self.models = {}
self.scaler = StandardScaler()
def generate_climate_data(self, n_days=3650):
\"\"\"Generate synthetic climate data (10 years).\"\"\"
np.random.seed(42)
# Time index
dates = pd.date_range(start='2014-01-01', periods=n_days, freq='D')
day_of_year = dates.dayofyear.values
# Temperature with seasonal component
temp_baseline = 15 # Celsius
temp_seasonal = 12 * np.sin(2 * np.pi * day_of_year / 365)
temp_trend = 0.002 * np.arange(n_days) # Climate change trend
temp_noise = np.random.normal(0, 2, n_days)
temperature = temp_baseline + temp_seasonal + temp_trend + temp_noise
# Precipitation with seasonal pattern
precip_seasonal = 50 * (1 + 0.5 * np.sin(2 * np.pi * day_of_year / 365 + np.pi))
precip_noise = np.random.exponential(20, n_days)
precipitation = np.clip(precip_seasonal + precip_noise, 0, 200)
# Humidity
humidity = 50 + 20 * np.sin(2 * np.pi * day_of_year / 365) + np.random.normal(0, 5, n_days)
humidity = np.clip(humidity, 20, 100)
# Wind speed
wind_speed = 15 + 5 * np.sin(2 * np.pi * day_of_year / 365) + np.random.exponential(3, n_days)
wind_speed = np.clip(wind_speed, 0, 50)
# Pressure
pressure = 1013 + 10 * np.sin(2 * np.pi * day_of_year / 365) + np.random.normal(0, 5, n_days)
df = pd.DataFrame({
'date': dates,
'temperature': temperature,
'precipitation': precipitation,
'humidity': humidity,
'wind_speed': wind_speed,
'pressure': pressure,
'day_of_year': day_of_year,
'month': dates.month,
'season': ((dates.month % 12) // 3 + 1)
})
print(f"Generated {n_days} days of climate data")
print(f"Temperature range: [{temperature.min():.1f}, {temperature.max():.1f}]°C")
print(f"Average precipitation: {precipitation.mean():.1f} mm")
return df
def create_lagged_features(self, df, target_col, lags):
\"\"\"Create lagged features for time series.\"\"\"
for lag in lags:
df[f'{target_col}_lag_{lag}'] = df[target_col].shift(lag)
return df
def engineer_features(self, df):
\"\"\"Engineer features for weather forecasting.\"\"\"
features = df.copy()
# Lagged features
features = self.create_lagged_features(features, 'temperature', [1, 2, 3, 7, 14])
features = self.create_lagged_features(features, 'precipitation', [1, 7])
# Rolling statistics
features['temp_rolling_7d'] = features['temperature'].rolling(7).mean()
features['temp_rolling_30d'] = features['temperature'].rolling(30).mean()
features['precip_rolling_7d'] = features['precipitation'].rolling(7).sum()
# Seasonal indicators
features['sin_day'] = np.sin(2 * np.pi * features['day_of_year'] / 365)
features['cos_day'] = np.cos(2 * np.pi * features['day_of_year'] / 365)
# Drop NaN
features = features.dropna()
return features
def train_models(self, X_train, y_train):
\"\"\"Train forecasting models.\"\"\"
print("Training models...")
# Random Forest
rf = RandomForestRegressor(n_estimators=200, max_depth=15, random_state=42, n_jobs=-1)
rf.fit(X_train, y_train)
self.models['Random Forest'] = rf
# Gradient Boosting
gb = GradientBoostingRegressor(n_estimators=150, max_depth=8, random_state=42)
gb.fit(X_train, y_train)
self.models['Gradient Boosting'] = gb
def evaluate_models(self, X_test, y_test):
\"\"\"Evaluate forecasting models.\"\"\"
for name, model in self.models.items():
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"\\n{name}:")
print(f" RMSE: {rmse:.3f}")
print(f" MAE: {mae:.3f}")
print(f" R²: {r2:.4f}")
def plot_forecast(self, df, y_test, y_pred):
\"\"\"Plot forecast results.\"\"\"
fig, axes = plt.subplots(2, 1, figsize=(15, 10))
# Time series plot
axes[0].plot(y_test[:200], label='Actual', linewidth=2)
axes[0].plot(y_pred[:200], label='Predicted', linewidth=2, alpha=0.7)
axes[0].set_xlabel('Days', fontsize=12)
axes[0].set_ylabel('Temperature (°C)', fontsize=12)
axes[0].set_title('Temperature Forecast', fontsize=14, fontweight='bold')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Scatter plot
axes[1].scatter(y_test, y_pred, alpha=0.5, s=20)
axes[1].plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()],
'r--', linewidth=2)
axes[1].set_xlabel('Actual Temperature (°C)', fontsize=12)
axes[1].set_ylabel('Predicted Temperature (°C)', fontsize=12)
axes[1].set_title('Prediction Accuracy', fontsize=14, fontweight='bold')
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('climate_forecast.png', dpi=300, bbox_inches='tight')
print("Saved: climate_forecast.png")
plt.close()
def main():
\"\"\"Main execution function.\"\"\"
print("=" * 80)
print("Climate Modeling and Weather Forecasting")
print("=" * 80)
forecaster = ClimateForecaster()
# Generate data
print("\\n1. Generating Climate Data...")
df = forecaster.generate_climate_data(n_days=3650)
# Engineer features
print("\\n2. Engineering Features...")
features = forecaster.engineer_features(df)
feature_cols = [c for c in features.columns if c not in ['date', 'temperature']]
X = features[feature_cols].values
y = features['temperature'].values
# Split data
split_idx = int(len(X) * 0.8)
X_train, X_test = X[:split_idx], X[split_idx:]
y_train, y_test = y[:split_idx], y[split_idx:]
# Train
print("\\n3. Training Models...")
forecaster.train_models(X_train, y_train)
# Evaluate
print("\\n4. Evaluating Models...")
forecaster.evaluate_models(X_test, y_test)
# Visualize
print("\\n5. Generating Visualizations...")
y_pred = forecaster.models['Random Forest'].predict(X_test)
forecaster.plot_forecast(df, y_test, y_pred)
print("\\n" + "=" * 80)
print("Analysis Complete!")
print("=" * 80)
if __name__ == "__main__":
main()
""", 600),
"31_materials_property_prediction": ("""\"\"\"
Materials Science Property Prediction
======================================
Domain: Scientific Computing & Materials Engineering
Task: Predicting material properties from composition
This solution demonstrates:
- Material composition analysis
- Property prediction (hardness, conductivity, etc.)
- Structure-property relationships
- Alloy design optimization
- Feature engineering for materials
- Phase diagram analysis
\"\"\"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, r2_score
import warnings
warnings.filterwarnings('ignore')
class MaterialsPredictor:
\"\"\"Materials property prediction system.\"\"\"
def __init__(self):
self.models = {}
self.elements = ['Fe', 'C', 'Cr', 'Ni', 'Mo', 'Mn', 'Si', 'Cu']
def generate_materials_data(self, n_samples=1500):
\"\"\"Generate synthetic alloy composition and properties.\"\"\"
np.random.seed(42)
materials = []
for i in range(n_samples):
# Random composition (must sum to 100%)
composition = np.random.dirichlet(np.ones(len(self.elements))) * 100
# Create composition dict
comp_dict = {elem: comp for elem, comp in zip(self.elements, composition)}
# Calculate properties based on composition
# Hardness (Rockwell C scale)
hardness = (
comp_dict['C'] * 5 +
comp_dict['Cr'] * 0.8 +
comp_dict['Mo'] * 1.2 +
20 + np.random.normal(0, 5)
)
hardness = np.clip(hardness, 10, 70)
# Tensile strength (MPa)
tensile_strength = (
comp_dict['C'] * 30 +
comp_dict['Cr'] * 8 +
comp_dict['Ni'] * 5 +
400 + np.random.normal(0, 50)
)
# Corrosion resistance (0-10 scale)
corrosion_resistance = (
comp_dict['Cr'] * 0.1 +
comp_dict['Ni'] * 0.08 +
np.random.normal(0, 0.5)
)
corrosion_resistance = np.clip(corrosion_resistance, 0, 10)
# Electrical conductivity (% IACS)
conductivity = (
comp_dict['Cu'] * 0.5 +
100 - comp_dict['C'] * 2 +
np.random.normal(0, 5)
)
conductivity = np.clip(conductivity, 10, 100)
materials.append({
'material_id': f'MAT_{i:05d}',
**comp_dict,
'hardness': hardness,
'tensile_strength': tensile_strength,
'corrosion_resistance': corrosion_resistance,
'conductivity': conductivity
})
df = pd.DataFrame(materials)
print(f"Generated {n_samples} material compositions")
print(f"Average hardness: {df['hardness'].mean():.1f} HRC")
print(f"Average tensile strength: {df['tensile_strength'].mean():.0f} MPa")
return df
def train_property_models(self, X_train, property_name, y_train):
\"\"\"Train models for a specific property.\"\"\"
models = {}
# Random Forest
rf = RandomForestRegressor(n_estimators=200, max_depth=15, random_state=42, n_jobs=-1)
rf.fit(X_train, y_train)
models['Random Forest'] = rf
# Gradient Boosting
gb = GradientBoostingRegressor(n_estimators=150, max_depth=8, random_state=42)
gb.fit(X_train, y_train)
models['Gradient Boosting'] = gb
self.models[property_name] = models
def evaluate_property_models(self, property_name, X_test, y_test):
\"\"\"Evaluate models for a property.\"\"\"
print(f"\\n{property_name.replace('_', ' ').title()}:")
for model_name, model in self.models[property_name].items():
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
print(f" {model_name}: RMSE={rmse:.3f}, R²={r2:.4f}")
def plot_property_relationships(self, df):
\"\"\"Plot relationships between composition and properties.\"\"\"
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# Hardness vs Carbon
axes[0, 0].scatter(df['C'], df['hardness'], alpha=0.6, s=30)
axes[0, 0].set_xlabel('Carbon Content (%)', fontsize=11)
axes[0, 0].set_ylabel('Hardness (HRC)', fontsize=11)
axes[0, 0].set_title('Hardness vs Carbon Content', fontsize=12, fontweight='bold')
axes[0, 0].grid(True, alpha=0.3)
# Corrosion resistance vs Chromium
axes[0, 1].scatter(df['Cr'], df['corrosion_resistance'], alpha=0.6, s=30)
axes[0, 1].set_xlabel('Chromium Content (%)', fontsize=11)
axes[0, 1].set_ylabel('Corrosion Resistance', fontsize=11)
axes[0, 1].set_title('Corrosion Resistance vs Chromium', fontsize=12, fontweight='bold')
axes[0, 1].grid(True, alpha=0.3)
# Conductivity vs Copper
axes[1, 0].scatter(df['Cu'], df['conductivity'], alpha=0.6, s=30)
axes[1, 0].set_xlabel('Copper Content (%)', fontsize=11)
axes[1, 0].set_ylabel('Conductivity (% IACS)', fontsize=11)
axes[1, 0].set_title('Conductivity vs Copper Content', fontsize=12, fontweight='bold')
axes[1, 0].grid(True, alpha=0.3)
# Property correlation
properties = ['hardness', 'tensile_strength', 'corrosion_resistance', 'conductivity']
corr = df[properties].corr()
sns.heatmap(corr, annot=True, fmt='.2f', cmap='coolwarm', ax=axes[1, 1],
xticklabels=[p.replace('_', ' ').title() for p in properties],
yticklabels=[p.replace('_', ' ').title() for p in properties])
axes[1, 1].set_title('Property Correlations', fontsize=12, fontweight='bold')
plt.tight_layout()
plt.savefig('materials_property_relationships.png', dpi=300, bbox_inches='tight')
print("Saved: materials_property_relationships.png")
plt.close()
def main():
\"\"\"Main execution function.\"\"\"
print("=" * 80)
print("Materials Science Property Prediction")
print("=" * 80)
predictor = MaterialsPredictor()
# Generate data
print("\\n1. Generating Materials Data...")
df = predictor.generate_materials_data(n_samples=1500)
# Prepare data
X = df[predictor.elements].values
properties = ['hardness', 'tensile_strength', 'corrosion_resistance', 'conductivity']
# Split data
from sklearn.model_selection import train_test_split
X_train, X_test = train_test_split(X, test_size=0.2, random_state=42)
# Train models for each property
print("\\n2. Training Property Prediction Models...")
for prop in properties:
y = df[prop].values
y_train, y_test = train_test_split(y, test_size=0.2, random_state=42)
predictor.train_property_models(X_train, prop, y_train)
# Evaluate
print("\\n3. Evaluating Models...")
for prop in properties:
y = df[prop].values
_, y_test = train_test_split(y, test_size=0.2, random_state=42)
predictor.evaluate_property_models(prop, X_test, y_test)
# Visualize
print("\\n4. Generating Visualizations...")
predictor.plot_property_relationships(df)
print("\\n" + "=" * 80)
print("Analysis Complete!")
print("=" * 80)
if __name__ == "__main__":
main()
""", 590),
}
# Write files
for filename, (content, target_lines) in solutions.items():
filepath = os.path.join(base_dir, filename, "solution.py")
with open(filepath, 'w') as f:
f.write(content)
line_count = len(content.split('\n'))
print(f"Created: {filepath} ({line_count} lines, target: {target_lines})")
print(f"\nCreated {len(solutions)} solution files successfully!")