entropy25 commited on
Commit
3dbb5ae
·
verified ·
1 Parent(s): f8c0999

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -51
app.py CHANGED
@@ -431,24 +431,32 @@ class B2BCustomerAnalytics:
431
 
432
  def create_visualizations(self):
433
  """Create comprehensive visualizations"""
434
- if self.processed_df is None:
 
435
  return None, None, None, None
436
 
437
  try:
 
 
438
  # 1. Customer Segment Distribution
439
  segment_data = self.processed_df.groupby('customer_id')['Segment'].first().value_counts().reset_index()
440
  segment_data.columns = ['Segment', 'Count']
 
441
 
442
- fig1 = px.pie(
443
- segment_data,
444
- values='Count',
445
- names='Segment',
446
- title='Customer Segment Distribution',
447
- hole=0.4,
448
- color_discrete_sequence=['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899']
449
- )
450
- fig1.update_traces(textposition='inside', textinfo='percent+label')
451
- fig1.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
 
 
 
 
452
 
453
  # 2. RFM Analysis
454
  customer_rfm = self.processed_df.groupby('customer_id').agg({
@@ -457,67 +465,89 @@ class B2BCustomerAnalytics:
457
  'monetary': 'first',
458
  'Segment': 'first'
459
  }).reset_index()
 
460
 
461
- fig2 = px.scatter(
462
- customer_rfm,
463
- x='recency_days',
464
- y='frequency',
465
- size='monetary',
466
- color='Segment',
467
- title='RFM Customer Behavior Matrix',
468
- labels={
469
- 'recency_days': 'Days Since Last Purchase',
470
- 'frequency': 'Purchase Frequency',
471
- 'monetary': 'Total Revenue'
472
- }
473
- )
474
- fig2.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
 
 
 
 
475
 
476
  # 3. Churn Risk Distribution
477
- if self.predictions is not None:
 
478
  fig3 = px.histogram(
479
  self.predictions,
480
  x='churn_probability',
481
  nbins=20,
482
  title='Churn Probability Distribution',
483
- labels={'churn_probability': 'Churn Probability', 'count': 'Number of Customers'}
 
484
  )
485
  fig3.add_vline(x=0.5, line_dash="dash", line_color="red",
486
  annotation_text="High Risk Threshold")
487
  else:
488
  risk_data = self.processed_df.groupby('customer_id')['Churn_Risk'].first().value_counts().reset_index()
489
  risk_data.columns = ['Risk_Level', 'Count']
490
- colors_map = {'High': '#ef4444', 'Medium': '#f59e0b', 'Low': '#10b981'}
491
- fig3 = px.bar(
492
- risk_data,
493
- x='Risk_Level',
494
- y='Count',
495
- title='Customer Churn Risk Distribution',
496
- color='Risk_Level',
497
- color_discrete_map=colors_map
498
- )
499
-
500
- fig3.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
 
 
 
 
 
501
 
502
  # 4. Revenue Trends
503
- self.processed_df['order_month'] = self.processed_df['order_date'].dt.to_period('M')
504
- monthly_revenue = self.processed_df.groupby('order_month')['amount'].sum().reset_index()
505
- monthly_revenue['order_month'] = monthly_revenue['order_month'].astype(str)
506
-
507
- fig4 = px.line(
508
- monthly_revenue,
509
- x='order_month',
510
- y='amount',
511
- title='Monthly Revenue Trends',
512
- labels={'amount': 'Revenue ($)', 'order_month': 'Month'}
513
- )
514
- fig4.update_traces(line_color='#6366f1', line_width=3)
515
- fig4.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
 
 
 
 
 
 
 
 
516
 
517
  return fig1, fig2, fig3, fig4
518
 
519
  except Exception as e:
520
  print(f"Error creating visualizations: {e}")
 
 
521
  return None, None, None, None
522
 
523
  def create_customer_table(self):
 
431
 
432
  def create_visualizations(self):
433
  """Create comprehensive visualizations"""
434
+ if self.processed_df is None or len(self.processed_df) == 0:
435
+ print("No processed data available for visualization")
436
  return None, None, None, None
437
 
438
  try:
439
+ print(f"Creating visualizations with {len(self.processed_df)} rows")
440
+
441
  # 1. Customer Segment Distribution
442
  segment_data = self.processed_df.groupby('customer_id')['Segment'].first().value_counts().reset_index()
443
  segment_data.columns = ['Segment', 'Count']
444
+ print(f"Segment data: {segment_data}")
445
 
446
+ if len(segment_data) == 0:
447
+ print("No segment data found")
448
+ fig1 = None
449
+ else:
450
+ fig1 = px.pie(
451
+ segment_data,
452
+ values='Count',
453
+ names='Segment',
454
+ title='Customer Segment Distribution',
455
+ hole=0.4,
456
+ color_discrete_sequence=['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899']
457
+ )
458
+ fig1.update_traces(textposition='inside', textinfo='percent+label')
459
+ fig1.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
460
 
461
  # 2. RFM Analysis
462
  customer_rfm = self.processed_df.groupby('customer_id').agg({
 
465
  'monetary': 'first',
466
  'Segment': 'first'
467
  }).reset_index()
468
+ print(f"RFM data shape: {customer_rfm.shape}")
469
 
470
+ if len(customer_rfm) == 0:
471
+ print("No RFM data found")
472
+ fig2 = None
473
+ else:
474
+ fig2 = px.scatter(
475
+ customer_rfm,
476
+ x='recency_days',
477
+ y='frequency',
478
+ size='monetary',
479
+ color='Segment',
480
+ title='RFM Customer Behavior Matrix',
481
+ labels={
482
+ 'recency_days': 'Days Since Last Purchase',
483
+ 'frequency': 'Purchase Frequency',
484
+ 'monetary': 'Total Revenue'
485
+ }
486
+ )
487
+ fig2.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
488
 
489
  # 3. Churn Risk Distribution
490
+ if self.predictions is not None and len(self.predictions) > 0:
491
+ print(f"Using predictions data with {len(self.predictions)} rows")
492
  fig3 = px.histogram(
493
  self.predictions,
494
  x='churn_probability',
495
  nbins=20,
496
  title='Churn Probability Distribution',
497
+ labels={'churn_probability': 'Churn Probability', 'count': 'Number of Customers'},
498
+ color_discrete_sequence=['#6366f1']
499
  )
500
  fig3.add_vline(x=0.5, line_dash="dash", line_color="red",
501
  annotation_text="High Risk Threshold")
502
  else:
503
  risk_data = self.processed_df.groupby('customer_id')['Churn_Risk'].first().value_counts().reset_index()
504
  risk_data.columns = ['Risk_Level', 'Count']
505
+ print(f"Risk data: {risk_data}")
506
+
507
+ if len(risk_data) == 0:
508
+ print("No risk data found")
509
+ fig3 = None
510
+ else:
511
+ colors_map = {'High': '#ef4444', 'Medium': '#f59e0b', 'Low': '#10b981'}
512
+ fig3 = px.bar(
513
+ risk_data,
514
+ x='Risk_Level',
515
+ y='Count',
516
+ title='Customer Churn Risk Distribution',
517
+ color='Risk_Level',
518
+ color_discrete_map=colors_map
519
+ )
520
+ fig3.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'}, showlegend=False)
521
 
522
  # 4. Revenue Trends
523
+ try:
524
+ self.processed_df['order_month'] = self.processed_df['order_date'].dt.to_period('M')
525
+ monthly_revenue = self.processed_df.groupby('order_month')['amount'].sum().reset_index()
526
+ monthly_revenue['order_month'] = monthly_revenue['order_month'].astype(str)
527
+ print(f"Monthly revenue data: {monthly_revenue.head()}")
528
+
529
+ if len(monthly_revenue) == 0:
530
+ fig4 = None
531
+ else:
532
+ fig4 = px.line(
533
+ monthly_revenue,
534
+ x='order_month',
535
+ y='amount',
536
+ title='Monthly Revenue Trends',
537
+ labels={'amount': 'Revenue ($)', 'order_month': 'Month'}
538
+ )
539
+ fig4.update_traces(line_color='#6366f1', line_width=3)
540
+ fig4.update_layout(height=400, title={'x': 0.5, 'xanchor': 'center'})
541
+ except Exception as e:
542
+ print(f"Error creating revenue chart: {e}")
543
+ fig4 = None
544
 
545
  return fig1, fig2, fig3, fig4
546
 
547
  except Exception as e:
548
  print(f"Error creating visualizations: {e}")
549
+ import traceback
550
+ traceback.print_exc()
551
  return None, None, None, None
552
 
553
  def create_customer_table(self):