Spaces:
Sleeping
Sleeping
Commit
·
6183395
1
Parent(s):
ff3d566
feat: updated website
Browse files
app.py
CHANGED
@@ -267,13 +267,11 @@ elif page == "Customer Analysis":
|
|
267 |
|
268 |
if not customer_match.empty:
|
269 |
cluster = customer_match['cluster_id'].values[0]
|
270 |
-
st.success(f"Customer {customer_code} belongs to cluster {cluster}")
|
271 |
|
272 |
with st.spinner(f"Loading customer model for cluster {cluster}..."):
|
273 |
# Load the Corresponding Model
|
274 |
model_path = f'models/modelo_cluster_{cluster}.txt'
|
275 |
gbm = lgb.Booster(model_file=model_path)
|
276 |
-
st.success(f"Loaded model for cluster {cluster}")
|
277 |
|
278 |
with st.spinner("Getting the data ready..."):
|
279 |
# Load predict data for that cluster
|
@@ -282,25 +280,19 @@ elif page == "Customer Analysis":
|
|
282 |
# Convert cliente_id to string
|
283 |
predict_data['cliente_id'] = predict_data['cliente_id'].astype(str)
|
284 |
|
285 |
-
with st.spinner("Filtering
|
286 |
|
287 |
# Filter for the specific customer
|
288 |
customer_code_str = str(customer_code)
|
289 |
customer_data = predict_data[predict_data['cliente_id'] == customer_code_str]
|
290 |
-
|
291 |
-
# Add debug statements
|
292 |
-
st.write(f"Unique customer IDs in predict data: {predict_data['cliente_id'].unique()}")
|
293 |
-
st.write(f"Customer code we're looking for: {customer_code_str}")
|
294 |
|
295 |
-
|
296 |
-
st.write(customer_data.head())
|
297 |
-
st.write(f"Shape: {customer_data.shape}")
|
298 |
|
299 |
if not customer_data.empty:
|
300 |
# Define features consistently with the training process
|
301 |
lag_features = [f'precio_total_lag_{lag}' for lag in range(1, 25)]
|
302 |
features = lag_features + ['mes', 'marca_id_encoded', 'año', 'cluster_id']
|
303 |
-
|
304 |
# Prepare data for prediction
|
305 |
X_predict = customer_data[features]
|
306 |
|
@@ -309,34 +301,15 @@ elif page == "Customer Analysis":
|
|
309 |
for feature in categorical_features:
|
310 |
X_predict[feature] = X_predict[feature].astype('category')
|
311 |
|
312 |
-
st.write("### Features for Prediction:")
|
313 |
-
st.write(X_predict.head())
|
314 |
-
st.write(f"Shape: {X_predict.shape}")
|
315 |
-
st.write("Data types:")
|
316 |
-
st.write(X_predict.dtypes)
|
317 |
-
|
318 |
# Make Prediction for the selected customer
|
319 |
y_pred = gbm.predict(X_predict, num_iteration=gbm.best_iteration)
|
320 |
-
|
321 |
-
st.write(f"Type of y_pred: {type(y_pred)}")
|
322 |
-
st.write(f"Shape of y_pred: {y_pred.shape}")
|
323 |
-
st.write("First few predictions:")
|
324 |
-
st.write(y_pred[:5])
|
325 |
-
|
326 |
# Reassemble the results
|
327 |
results = customer_data[['cliente_id', 'marca_id_encoded', 'fecha_mes']].copy()
|
328 |
results['ventas_predichas'] = y_pred
|
329 |
-
st.write("### Results DataFrame:")
|
330 |
-
st.write(results.head())
|
331 |
-
st.write(f"Shape: {results.shape}")
|
332 |
-
|
333 |
-
st.write(f"Predicted total sales for Customer {customer_code}: {results['ventas_predichas'].sum():.2f}")
|
334 |
|
335 |
# Load actual data
|
336 |
actual_sales = df_agg_2024[df_agg_2024['cliente_id'] == customer_code_str]
|
337 |
-
st.write("### Actual Sales DataFrame:")
|
338 |
-
st.write(actual_sales.head())
|
339 |
-
st.write(f"Shape: {actual_sales.shape}")
|
340 |
|
341 |
if not actual_sales.empty:
|
342 |
results = results.merge(actual_sales[['cliente_id', 'marca_id_encoded', 'fecha_mes', 'precio_total']],
|
|
|
267 |
|
268 |
if not customer_match.empty:
|
269 |
cluster = customer_match['cluster_id'].values[0]
|
|
|
270 |
|
271 |
with st.spinner(f"Loading customer model for cluster {cluster}..."):
|
272 |
# Load the Corresponding Model
|
273 |
model_path = f'models/modelo_cluster_{cluster}.txt'
|
274 |
gbm = lgb.Booster(model_file=model_path)
|
|
|
275 |
|
276 |
with st.spinner("Getting the data ready..."):
|
277 |
# Load predict data for that cluster
|
|
|
280 |
# Convert cliente_id to string
|
281 |
predict_data['cliente_id'] = predict_data['cliente_id'].astype(str)
|
282 |
|
283 |
+
with st.spinner("Filtering data..."):
|
284 |
|
285 |
# Filter for the specific customer
|
286 |
customer_code_str = str(customer_code)
|
287 |
customer_data = predict_data[predict_data['cliente_id'] == customer_code_str]
|
|
|
|
|
|
|
|
|
288 |
|
289 |
+
with st.spinner("Generating sales predictions..."):
|
|
|
|
|
290 |
|
291 |
if not customer_data.empty:
|
292 |
# Define features consistently with the training process
|
293 |
lag_features = [f'precio_total_lag_{lag}' for lag in range(1, 25)]
|
294 |
features = lag_features + ['mes', 'marca_id_encoded', 'año', 'cluster_id']
|
295 |
+
|
296 |
# Prepare data for prediction
|
297 |
X_predict = customer_data[features]
|
298 |
|
|
|
301 |
for feature in categorical_features:
|
302 |
X_predict[feature] = X_predict[feature].astype('category')
|
303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
# Make Prediction for the selected customer
|
305 |
y_pred = gbm.predict(X_predict, num_iteration=gbm.best_iteration)
|
306 |
+
|
|
|
|
|
|
|
|
|
|
|
307 |
# Reassemble the results
|
308 |
results = customer_data[['cliente_id', 'marca_id_encoded', 'fecha_mes']].copy()
|
309 |
results['ventas_predichas'] = y_pred
|
|
|
|
|
|
|
|
|
|
|
310 |
|
311 |
# Load actual data
|
312 |
actual_sales = df_agg_2024[df_agg_2024['cliente_id'] == customer_code_str]
|
|
|
|
|
|
|
313 |
|
314 |
if not actual_sales.empty:
|
315 |
results = results.merge(actual_sales[['cliente_id', 'marca_id_encoded', 'fecha_mes', 'precio_total']],
|