-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_residual_plot.py
More file actions
56 lines (45 loc) · 1.71 KB
/
generate_residual_plot.py
File metadata and controls
56 lines (45 loc) · 1.71 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from data_loader import DataLoader
from feature_engineering import FeatureEngineer
from forecast_model import ForecastModel
def generate_residual_plot():
file_path = "Walmart.csv"
if not os.path.exists(file_path):
file_path = "../Walmart.csv"
if not os.path.exists(file_path):
# Fallback to temp_data.csv if available
file_path = "temp_data.csv"
print(f"Loading data from {file_path}...")
loader = DataLoader(file_path)
raw_df = loader.load_data()
print("Engineering features...")
fe = FeatureEngineer()
processed_df = fe.create_features(raw_df)
print("Splitting data...")
X_train, X_test, y_train, y_test, feature_names = fe.split_data(processed_df)
print("Training model...")
model = ForecastModel()
model.train(X_train[feature_names], y_train)
print("Generating predictions...")
y_pred = model.predict(X_test[feature_names])
print("Calculating residuals...")
residuals = y_test - y_pred
print("Plotting residual plot...")
plt.figure(figsize=(10, 6))
plt.scatter(y_pred, residuals, alpha=0.6, s=30, color='teal')
plt.axhline(y=0, color='red', linestyle='--', linewidth=2, label='Zero Error')
plt.xlabel("Predicted Demand")
plt.ylabel("Residuals (Actual - Predicted)")
plt.title("Residual Plot of Demand Forecasting Model")
plt.legend()
plt.grid(True)
print("Saving plot...")
plt.tight_layout()
plt.savefig('residual_plot.png', bbox_inches='tight')
plt.close()
print("Success: Plot saved to residual_plot.png")
if __name__ == "__main__":
generate_residual_plot()