Spaces:
Sleeping
Sleeping
Commit
·
2cd23d8
1
Parent(s):
0cedfeb
bugfix: added import lgbm
Browse files
app.py
CHANGED
@@ -144,30 +144,35 @@ elif page == "Customer Analysis":
|
|
144 |
X_predict_cluster = pd.read_csv(f'predicts/X_predict_cluster_{cluster}.csv')
|
145 |
|
146 |
# Filter for the specific customer and drop the 'cliente_id' column
|
147 |
-
X_cliente = X_predict_cluster[X_predict_cluster['cliente_id'] == customer_code]
|
148 |
|
149 |
if not X_cliente.empty:
|
150 |
# Make Prediction for the selected customer
|
151 |
-
y_pred = gbm.predict(X_cliente, num_iteration=gbm.best_iteration)
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
# Merge with actual data from df_agg_2024
|
155 |
df_agg_2024 = pd.read_csv('predicts/df_agg_2024.csv')
|
156 |
-
actual_sales = df_agg_2024[
|
|
|
157 |
if not actual_sales.empty:
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
# Calculate metrics
|
165 |
-
mae = mean_absolute_error(
|
166 |
-
mape = np.mean(np.abs((
|
167 |
-
rmse = np.sqrt(mean_squared_error(
|
168 |
-
smape_value = smape(
|
169 |
|
170 |
-
st.write(f"Actual sales for Customer {customer_code}: {
|
171 |
st.write(f"MAE: {mae:.2f}")
|
172 |
st.write(f"MAPE: {mape:.2f}%")
|
173 |
st.write(f"RMSE: {rmse:.2f}")
|
|
|
144 |
X_predict_cluster = pd.read_csv(f'predicts/X_predict_cluster_{cluster}.csv')
|
145 |
|
146 |
# Filter for the specific customer and drop the 'cliente_id' column
|
147 |
+
X_cliente = X_predict_cluster[X_predict_cluster['cliente_id'] == customer_code]
|
148 |
|
149 |
if not X_cliente.empty:
|
150 |
# Make Prediction for the selected customer
|
151 |
+
y_pred = gbm.predict(X_cliente.drop(columns=['cliente_id', 'fecha_mes']), num_iteration=gbm.best_iteration)
|
152 |
+
|
153 |
+
# Reassemble the results
|
154 |
+
results = X_cliente[['cliente_id', 'marca_id_encoded', 'fecha_mes']].copy()
|
155 |
+
results['ventas_predichas'] = y_pred
|
156 |
+
|
157 |
+
st.write(f"Predicted total sales for Customer {customer_code}: {results['ventas_predichas'].sum():.2f}")
|
158 |
|
159 |
# Merge with actual data from df_agg_2024
|
160 |
df_agg_2024 = pd.read_csv('predicts/df_agg_2024.csv')
|
161 |
+
actual_sales = df_agg_2024[df_agg_2024['cliente_id'] == customer_code]
|
162 |
+
|
163 |
if not actual_sales.empty:
|
164 |
+
results = results.merge(actual_sales[['cliente_id', 'marca_id_encoded', 'fecha_mes', 'precio_total']],
|
165 |
+
on=['cliente_id', 'marca_id_encoded', 'fecha_mes'],
|
166 |
+
how='left')
|
167 |
+
results.rename(columns={'precio_total': 'ventas_reales'}, inplace=True)
|
168 |
+
|
|
|
169 |
# Calculate metrics
|
170 |
+
mae = mean_absolute_error(results['ventas_reales'], results['ventas_predichas'])
|
171 |
+
mape = np.mean(np.abs((results['ventas_reales'] - results['ventas_predichas']) / results['ventas_reales'])) * 100
|
172 |
+
rmse = np.sqrt(mean_squared_error(results['ventas_reales'], results['ventas_predichas']))
|
173 |
+
smape_value = smape(results['ventas_reales'], results['ventas_predichas'])
|
174 |
|
175 |
+
st.write(f"Actual total sales for Customer {customer_code}: {results['ventas_reales'].sum():.2f}")
|
176 |
st.write(f"MAE: {mae:.2f}")
|
177 |
st.write(f"MAPE: {mape:.2f}%")
|
178 |
st.write(f"RMSE: {rmse:.2f}")
|