-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (107 loc) · 3.8 KB
/
Copy pathmain.py
File metadata and controls
140 lines (107 loc) · 3.8 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
import os
import pandas as pd
import joblib
from src.data_preprocessing import load_data, preprocess
from src.feature_engineering import create_features
from src.forecasting import train_model, predict
from src.inventory import calculate_safety_stock, reorder_point
from src.visualization import plot_sales
# -----------------------------
# Step 1: Fix file path
# -----------------------------
base_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(base_dir, "data", "raw", "sales.csv")
# -----------------------------
# Step 2: Load & preprocess data
# -----------------------------
df = load_data(file_path)
df = preprocess(df)
# Keep original Product_ID
df['Product_ID_Original'] = df['Product_ID']
# Encode Product_ID for model
df['Product_ID_Encoded'] = df['Product_ID'].astype('category').cat.codes
# Save processed data
processed_path = os.path.join(base_dir, "data", "processed", "cleaned_sales.csv")
df.to_csv(processed_path, index=False)
# -----------------------------
# Step 3: Feature engineering
# -----------------------------
df = create_features(df)
# -----------------------------
# Step 4: Train model
# -----------------------------
X = df[['day', 'month', 'weekday', 'Product_ID_Encoded']]
y = df['Sales']
model = train_model(X, y)
# Save model
model_path = os.path.join(base_dir, "models", "model.pkl")
joblib.dump(model, model_path)
# -----------------------------
# Step 5: Predictions
# -----------------------------
predictions = predict(model, X)
# Save forecast output
forecast_df = pd.DataFrame({
"Date": df["Date"],
"Product_ID": df["Product_ID_Original"],
"Actual": y,
"Predicted": predictions
})
forecast_path = os.path.join(base_dir, "outputs", "forecasts.csv")
forecast_df.to_csv(forecast_path, index=False)
# -----------------------------
# Step 6: Inventory calculations (USING PREDICTIONS)
# -----------------------------
avg_demand = predictions.mean()
std_demand = predictions.std()
lead_time = 5
safety = calculate_safety_stock(std_demand, lead_time)
reorder = reorder_point(avg_demand, lead_time, safety)
# Save inventory output
inventory_df = pd.DataFrame({
"Average Demand": [avg_demand],
"Demand Std Dev": [std_demand],
"Safety Stock": [safety],
"Reorder Point": [reorder]
})
inventory_path = os.path.join(base_dir, "outputs", "inventory_plan.csv")
inventory_df.to_csv(inventory_path, index=False)
# -----------------------------
# Step 7: Visualization
# -----------------------------
plot_sales(df)
# -----------------------------
# Step 8: Print results
# -----------------------------
print("Average Demand:", avg_demand)
print("Demand Std Dev:", std_demand)
print("Safety Stock:", safety)
print("Reorder Point:", reorder)
# -----------------------------
# Step 9: Future Forecasting (FIXED)
# -----------------------------
future_days = 7
last_date = df['Date'].max()
future_data = []
products = df['Product_ID_Original'].unique()
for product in products:
for i in range(1, future_days + 1):
future_date = last_date + pd.Timedelta(days=i)
future_data.append({
"Date": future_date,
"Product_ID_Original": product
})
future_df = pd.DataFrame(future_data)
# ✅ IMPORTANT: encode product SAME as training
future_df['Product_ID_Encoded'] = future_df['Product_ID_Original'].astype('category').cat.codes
# Feature engineering
future_df['day'] = future_df['Date'].dt.day
future_df['month'] = future_df['Date'].dt.month
future_df['weekday'] = future_df['Date'].dt.weekday
# Prediction input MUST match training features
X_future = future_df[['day', 'month', 'weekday', 'Product_ID_Encoded']]
future_df['Predicted_Sales'] = model.predict(X_future)
# Save output
future_path = os.path.join(base_dir, "outputs", "future_forecast.csv")
future_df.to_csv(future_path, index=False)
print("Future Forecast Saved!")