Spaces:
Sleeping
Sleeping
File size: 4,282 Bytes
8cc5633 |
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 |
# eval_model.py
import json
import os
import pickle
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from lightgbm_model.scripts.config_lightgbm import DATA_PATH, RESULTS_DIR
from lightgbm_model.scripts.utils import load_lightgbm_model
# === Ergebnisse-Ordner vorbereiten ===
os.makedirs(RESULTS_DIR, exist_ok=True)
# === Modell und eval_result laden ===
# Modell laden
model = load_lightgbm_model()
# Eval laden
with open(os.path.join(RESULTS_DIR, "lightgbm_eval_result.pkl"), "rb") as f:
eval_result = pickle.load(f)
X_train = pd.read_csv(os.path.join(RESULTS_DIR, "X_train.csv"))
X_test = pd.read_csv(os.path.join(RESULTS_DIR, "X_test.csv"))
y_test = pd.read_csv(os.path.join(RESULTS_DIR, "y_test.csv"))
# === Lernkurve ===
train_rmse = eval_result["training"]["rmse"]
valid_rmse = eval_result["valid_1"]["rmse"]
plt.figure(figsize=(10, 5))
plt.plot(train_rmse, label="Train RMSE")
plt.plot(valid_rmse, label="Valid RMSE")
plt.axvline(model.best_iteration_, color="gray", linestyle="--", label="Best Iteration")
plt.xlabel("Boosting Round")
plt.ylabel("RMSE")
plt.title("LightGBM Learning Curve")
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "lightgbm_learning_curve.png"))
# plt.show()
# === Metriken berechnen ===
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
mape = (
np.mean(
np.abs(
(y_test.values.flatten() - y_pred)
/ np.where(y_test.values.flatten() == 0, 1e-10, y_test.values.flatten())
)
)
* 100
)
r2 = r2_score(y_test, y_pred)
print(f"Test MAPE: {mape:.5f} %")
print(f"Test MAE: {mae:.5f}")
print(f"Test RMSE: {rmse:.5f}")
print(f"Test R2: {r2:.5f}")
metrics = {
"model": "LightGBM",
"MAE": round(mae, 2),
"RMSE": round(rmse, 2),
"MAPE (%)": round(mape, 2),
"R2": round(r2, 4),
"unit": "MW",
}
# Pfad setzen
output_path = os.path.join(RESULTS_DIR, "evaluation_metrics_lightgbm.json")
# Speichern
with open(output_path, "w") as f:
json.dump(metrics, f, indent=4)
print(f"Metriken gespeichert unter {output_path}")
# === Feature Importance ===
feature_importance = pd.DataFrame(
{"Feature": X_train.columns, "Importance": model.feature_importances_}
).sort_values(by="Importance", ascending=False)
plt.figure(figsize=(10, 6))
plt.barh(feature_importance["Feature"], feature_importance["Importance"])
plt.xlabel("Feature Importance")
plt.title("LightGBM Feature Importance")
plt.gca().invert_yaxis()
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "lightgbm_feature_importance.png"))
# plt.show()
# === Vergleichsplots ===
results_df = pd.DataFrame(
{
"True Consumption (MW)": y_test.values.flatten(),
"Predicted Consumption (MW)": y_pred,
}
)
# Timestamps anhängen
full_df = pd.read_csv(DATA_PATH)
test_dates = full_df.iloc[int(len(full_df) * 0.8) :]["date"].reset_index(drop=True)
results_df["Timestamp"] = pd.to_datetime(test_dates)
# Voller Plot
plt.figure(figsize=(15, 6))
plt.plot(
results_df["Timestamp"],
results_df["True Consumption (MW)"],
label="True",
color="darkblue",
)
plt.plot(
results_df["Timestamp"],
results_df["Predicted Consumption (MW)"],
label="Predicted",
color="red",
linestyle="--",
)
plt.title("Predicted vs True Consumption")
plt.xlabel("Timestamp")
plt.ylabel("Consumption (MW)")
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "lightgbm_comparison_plot.png"))
# plt.show()
# Subset Plot
subset = results_df.iloc[: len(results_df) // 10]
plt.figure(figsize=(15, 6))
plt.plot(
subset["Timestamp"], subset["True Consumption (MW)"], label="True", color="darkblue"
)
plt.plot(
subset["Timestamp"],
subset["Predicted Consumption (MW)"],
label="Predicted",
color="red",
linestyle="--",
)
plt.title("Predicted vs True (First decile)")
plt.xlabel("Timestamp")
plt.ylabel("Consumption (MW)")
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "lightgbm_prediction_with_timestamp.png"))
# plt.show()
# === Ens message ===
print("\nEvaluation completed.")
print(f"All Plots stored in:\n→ {RESULTS_DIR}")
|