-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
145 lines (120 loc) · 5.4 KB
/
train.py
File metadata and controls
145 lines (120 loc) · 5.4 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
import pandas as pd
import numpy as np
import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import os
# 1. Generate Synthetic Dataset
def generate_data(n_samples=2000):
np.random.seed(42)
locations = ['AB1', 'AB2', 'AB3', 'Library', 'Food Court', 'Sports Complex', 'Hostel D1', 'Hostel D2']
weathers = ['Sunny', 'Cloudy', 'Rainy', 'Overcast']
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
data = {
'location': np.random.choice(locations, n_samples),
'hour': np.random.randint(0, 24, n_samples),
'day_of_week': np.random.choice(days, n_samples),
'is_weekend': np.random.randint(0, 2, n_samples),
'is_holiday': np.random.choice([0, 1], n_samples, p=[0.9, 0.1]),
'is_event': np.random.choice([0, 1], n_samples, p=[0.95, 0.05]),
'weather': np.random.choice(weathers, n_samples),
'distance_to_tower': np.random.uniform(5, 150, n_samples),
'num_users': np.random.randint(1, 500, n_samples),
'signal_strength': np.random.uniform(-90, -30, n_samples),
'network_traffic': np.random.uniform(0.1, 1.0, n_samples)
}
df = pd.DataFrame(data)
# Target variable: download_speed (Mbps)
# Base speed 100
df['download_speed'] = 100.0
# Apply factors
# Signal strength factor
df['download_speed'] *= (1.0 - (abs(df['signal_strength'] + 30) / 60.0))
# Distance factor
df['download_speed'] *= (1.0 / (1.0 + (df['distance_to_tower'] / 50.0)**2))
# Congestion factor
df['download_speed'] *= (1.0 - (df['num_users'] / 500.0) * df['network_traffic'])
# Time factor (Peak hours)
df.loc[(df['hour'] >= 10) & (df['hour'] <= 16), 'download_speed'] *= 0.6
df.loc[(df['hour'] >= 19) & (df['hour'] <= 22), 'download_speed'] *= 0.7
# Event factor
df.loc[df['is_event'] == 1, 'download_speed'] *= 0.3
# Weather factor
df.loc[df['weather'] == 'Rainy', 'download_speed'] *= 0.7
# Add noise
df['download_speed'] *= np.random.uniform(0.9, 1.1, n_samples)
df['download_speed'] = df['download_speed'].clip(lower=0.5)
return df
# 2. Feature Engineering
def feature_engineering(df):
# Peak hours: 10-16 and 19-22
df['is_peak_hour'] = df['hour'].apply(lambda x: 1 if (10 <= x <= 16) or (19 <= x <= 22) else 0)
# Night time: 0-6
df['is_night'] = df['hour'].apply(lambda x: 1 if (0 <= x <= 6) else 0)
# Lunch time: 12-14
df['is_lunch_time'] = df['hour'].apply(lambda x: 1 if (12 <= x <= 14) else 0)
# Crowd level: Low, Medium, High
df['crowd_level'] = pd.cut(df['num_users'], bins=[0, 100, 300, 501], labels=[0, 1, 2]).astype(int)
# Signal quality: Poor, Fair, Good, Excellent
df['signal_quality'] = pd.cut(df['signal_strength'], bins=[-91, -80, -70, -60, -29], labels=[0, 1, 2, 3]).astype(int)
return df
# 3. Main Training Script
def train():
print("Generating synthetic data...")
df = generate_data()
print("Performing feature engineering...")
df = feature_engineering(df)
# 4. Encoding Categorical Variables
print("Encoding categorical variables...")
le_location = LabelEncoder()
df['location_enc'] = le_location.fit_transform(df['location'])
le_weather = LabelEncoder()
df['weather_enc'] = le_weather.fit_transform(df['weather'])
le_day = LabelEncoder()
df['day_enc'] = le_day.fit_transform(df['day_of_week'])
# Save encoders
joblib.dump(le_location, 'le_location.pkl')
joblib.dump(le_weather, 'le_weather.pkl')
joblib.dump(le_day, 'le_day.pkl')
# 5. Split Dataset
features = [
'location_enc', 'hour', 'day_enc', 'is_weekend', 'is_holiday',
'is_event', 'weather_enc', 'distance_to_tower', 'num_users',
'signal_strength', 'network_traffic', 'is_peak_hour',
'is_night', 'is_lunch_time', 'crowd_level', 'signal_quality'
]
X = df[features]
y = df['download_speed']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 6. Train Models
models = {
'Linear Regression': LinearRegression(),
'Random Forest': RandomForestRegressor(n_estimators=100, random_state=42),
'XGBoost': XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)
}
best_model = None
best_r2 = -float('inf')
results = {}
print("\nEvaluating Models:")
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
results[name] = {'MAE': mae, 'RMSE': rmse, 'R2': r2}
print(f"{name}: MAE={mae:.4f}, RMSE={rmse:.4f}, R2={r2:.4f}")
if r2 > best_r2:
best_r2 = r2
best_model = model
best_model_name = name
# 7. Save Best Model
print(f"\nBest Model: {best_model_name} with R2={best_r2:.4f}")
joblib.dump(best_model, 'network_speed_model.pkl')
print("Model saved as network_speed_model.pkl")
if __name__ == "__main__":
train()