-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_residuals.py
More file actions
50 lines (41 loc) · 1.11 KB
/
plot_residuals.py
File metadata and controls
50 lines (41 loc) · 1.11 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
# Load dataset
df = pd.read_csv("final_dataset.csv")
X = df[["rtt_avg_ms", "jitter_ms"]]
y = df["throughput_mbps"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# Scaling
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Best model
model = RandomForestRegressor(
n_estimators=150,
max_depth=6,
min_samples_leaf=10,
random_state=42
)
model.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
residuals = y_test - y_pred
# Plot
plt.figure(figsize=(6,5))
plt.scatter(y_pred, residuals, alpha=0.7)
plt.axhline(0, linestyle="--")
plt.xlabel("Predicted Throughput (Mbps)")
plt.ylabel("Residual Error (Mbps)")
plt.title("Residual Plot")
plt.tight_layout()
# Save
plt.savefig("results/residual_plot.png")
plt.close()
print("✅ Saved: results/residual_plot.png")