rknl commited on
Commit
cae1c9c
·
verified ·
1 Parent(s): 2130e8d

fixes plot

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +33 -41
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Example2
3
  emoji: 🐨
4
  colorFrom: blue
5
  colorTo: yellow
 
1
  ---
2
+ title: CausalAI
3
  emoji: 🐨
4
  colorFrom: blue
5
  colorTo: yellow
app.py CHANGED
@@ -281,6 +281,7 @@ def calculate_incremental_metrics(data, uplift_scores, treatment, threshold):
281
 
282
 
283
  def build_models_and_display(selected_features):
 
284
  """
285
  Build uplift models for all discount levels and display results.
286
 
@@ -293,6 +294,7 @@ def build_models_and_display(selected_features):
293
  Returns:
294
  tuple: Contains model information, feature importance plot, and uplift plot
295
  """
 
296
  global rct_results, generated_data, uplift_models, last_used_features
297
  if rct_results is None or generated_data is None:
298
  return "Please generate customer data and run RCT simulation first.", None, None
@@ -316,9 +318,8 @@ def build_models_and_display(selected_features):
316
  all_feature_importance = []
317
  uplift_models = {} # Store models for each treatment
318
 
319
- # Create subplots for train and test
320
- fig_uplift = make_subplots(rows=2, cols=1, subplot_titles=("Train Set Performance", "Test Set Performance"),
321
- vertical_spacing=0.1, row_heights=[0.5, 0.5])
322
 
323
  for treatment, color in zip(treatments, colors):
324
  model, train_uplift_scores, feature_importance_df, X_train = build_uplift_model(train_df, selected_features, treatment, 'Control')
@@ -341,8 +342,18 @@ def build_models_and_display(selected_features):
341
  thresholds = np.linspace(np.min(uplift_scores), np.max(uplift_scores), 100)
342
  inc_purchases, inc_profits = zip(*[calculate_incremental_metrics(dataset, uplift_scores, treatment, threshold) for threshold in thresholds])
343
 
344
- fig_uplift.add_trace(go.Scatter(x=inc_purchases, y=inc_profits, mode='lines', name=f'{treatment} Model', line=dict(color=color, width=2)), row=i+1, col=1)
345
- fig_uplift.add_trace(go.Scatter(x=[0, inc_purchases[0]], y=[0, inc_profits[0]], mode='lines', name=f'{treatment} Random', line=dict(color=color, width=2, dash='dash')), row=i+1, col=1)
 
 
 
 
 
 
 
 
 
 
346
 
347
  # Create feature importance plot
348
  fig_importance, ax = plt.subplots(figsize=(12, 8))
@@ -358,19 +369,6 @@ def build_models_and_display(selected_features):
358
  ax.set_ylabel('Feature')
359
  plt.tight_layout()
360
 
361
- # Improve uplift plot appearance
362
- fig_uplift.update_layout(
363
- height=1200, width=1000,
364
- title={'text': 'Incremental Profit vs Incremental Purchases (All Treatments)', 'y':0.98, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'},
365
- legend=dict(orientation='v', yanchor='bottom', y=0.02, xanchor='left', x=0.02, bgcolor='rgba(255, 255, 255, 0.5)')
366
- )
367
-
368
- for i in range(1, 3):
369
- fig_uplift.update_xaxes(title_text="Incremental Purchases", row=i, col=1)
370
- fig_uplift.update_yaxes(title_text="Incremental Profit", row=i, col=1)
371
- fig_uplift.update_xaxes(showgrid=True, gridwidth=1, gridcolor='lightgray', row=i, col=1)
372
- fig_uplift.update_yaxes(showgrid=True, gridwidth=1, gridcolor='lightgray', row=i, col=1)
373
-
374
  last_used_features = selected_features # Store the last used features
375
 
376
  info = f"Uplift models built using {len(selected_features)} features.\n"
@@ -379,7 +377,6 @@ def build_models_and_display(selected_features):
379
  info += f"Displaying results for both Train and Test sets"
380
 
381
  return info, fig_importance, fig_uplift
382
-
383
 
384
  def run_targeting_policy(discount_level, target_percentage, experiment_duration):
385
  """
@@ -485,19 +482,18 @@ def run_targeted_simulation(df, experiment_duration, discount_level):
485
  return pd.DataFrame(transactions)
486
 
487
  def analyze_targeting_results(assignment_df, transactions_df):
 
488
  """
489
  Analyze the results of the targeting policy experiment.
490
-
491
  This function calculates various metrics for each variant, including conversion rates,
492
  average revenue and profit per customer, and incremental purchases and profits.
493
-
494
  Args:
495
  assignment_df (pandas.DataFrame): The DataFrame containing variant assignments
496
  transactions_df (pandas.DataFrame): The DataFrame containing transaction data
497
-
498
  Returns:
499
  tuple: Contains a DataFrame with variant metrics and a plotly Figure object
500
  """
 
501
  # Calculate metrics for assigned customers
502
  assigned_customers = assignment_df.groupby('experiment_variant')['customer_id'].nunique().reset_index()
503
  assigned_customers.columns = ['variant', 'assigned_customers']
@@ -524,30 +520,27 @@ def analyze_targeting_results(assignment_df, transactions_df):
524
  variant_metrics['incremental_purchases'] = variant_metrics['purchasing_customers'] - control_metrics['purchasing_customers']
525
  variant_metrics['incremental_profit'] = variant_metrics['profit'] - control_metrics['profit']
526
 
527
- # Create visualization
528
- fig = go.Figure()
529
 
530
  colors = {'Control': 'blue', '5% discount': 'green', '10% discount': 'orange',
531
  '15% discount': 'red', 'Targeted': 'purple'}
532
 
533
  for variant in variant_metrics['variant']:
534
  variant_data = variant_metrics[variant_metrics['variant'] == variant]
535
- fig.add_trace(go.Scatter(
536
- x=[variant_data['incremental_purchases'].values[0]],
537
- y=[variant_data['incremental_profit'].values[0]],
538
- mode='markers+text',
539
- name=variant,
540
- text=[variant],
541
- textposition="top center",
542
- marker=dict(size=12, color=colors.get(variant, 'gray'))
543
- ))
544
-
545
- fig.update_layout(
546
- title='Incremental Profit vs Incremental Purchases by Variant',
547
- xaxis_title='Incremental Purchases',
548
- yaxis_title='Incremental Profit',
549
- showlegend=True
550
- )
551
 
552
  return variant_metrics, fig
553
 
@@ -656,7 +649,6 @@ with gr.Blocks() as demo:
656
  with gr.Tab("Build Uplift Model"):
657
  gr.Markdown("## Build Uplift Models for All Discount Levels")
658
 
659
- # Feature selection
660
  feature_checklist = gr.CheckboxGroup(
661
  choices=['age', 'gender', 'region', 'preferred_language', 'newsletter_subscription',
662
  'preferred_payment_method', 'loyalty_level', 'main_browsing_device',
 
281
 
282
 
283
  def build_models_and_display(selected_features):
284
+
285
  """
286
  Build uplift models for all discount levels and display results.
287
 
 
294
  Returns:
295
  tuple: Contains model information, feature importance plot, and uplift plot
296
  """
297
+
298
  global rct_results, generated_data, uplift_models, last_used_features
299
  if rct_results is None or generated_data is None:
300
  return "Please generate customer data and run RCT simulation first.", None, None
 
318
  all_feature_importance = []
319
  uplift_models = {} # Store models for each treatment
320
 
321
+ # Create Matplotlib figure for uplift plots
322
+ fig_uplift, axs = plt.subplots(2, 1, figsize=(10, 12))
 
323
 
324
  for treatment, color in zip(treatments, colors):
325
  model, train_uplift_scores, feature_importance_df, X_train = build_uplift_model(train_df, selected_features, treatment, 'Control')
 
342
  thresholds = np.linspace(np.min(uplift_scores), np.max(uplift_scores), 100)
343
  inc_purchases, inc_profits = zip(*[calculate_incremental_metrics(dataset, uplift_scores, treatment, threshold) for threshold in thresholds])
344
 
345
+ axs[i].plot(inc_purchases, inc_profits, label=f'{treatment} Model', color=color)
346
+ axs[i].plot([0, inc_purchases[0]], [0, inc_profits[0]], label=f'{treatment} Random', color=color, linestyle='--')
347
+
348
+ # Customize uplift plots
349
+ for i, title in enumerate(["Train Set Performance", "Test Set Performance"]):
350
+ axs[i].set_title(title)
351
+ axs[i].set_xlabel("Incremental Purchases")
352
+ axs[i].set_ylabel("Incremental Profit")
353
+ axs[i].legend()
354
+ axs[i].grid(True)
355
+
356
+ plt.tight_layout()
357
 
358
  # Create feature importance plot
359
  fig_importance, ax = plt.subplots(figsize=(12, 8))
 
369
  ax.set_ylabel('Feature')
370
  plt.tight_layout()
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
  last_used_features = selected_features # Store the last used features
373
 
374
  info = f"Uplift models built using {len(selected_features)} features.\n"
 
377
  info += f"Displaying results for both Train and Test sets"
378
 
379
  return info, fig_importance, fig_uplift
 
380
 
381
  def run_targeting_policy(discount_level, target_percentage, experiment_duration):
382
  """
 
482
  return pd.DataFrame(transactions)
483
 
484
  def analyze_targeting_results(assignment_df, transactions_df):
485
+
486
  """
487
  Analyze the results of the targeting policy experiment.
 
488
  This function calculates various metrics for each variant, including conversion rates,
489
  average revenue and profit per customer, and incremental purchases and profits.
 
490
  Args:
491
  assignment_df (pandas.DataFrame): The DataFrame containing variant assignments
492
  transactions_df (pandas.DataFrame): The DataFrame containing transaction data
 
493
  Returns:
494
  tuple: Contains a DataFrame with variant metrics and a plotly Figure object
495
  """
496
+
497
  # Calculate metrics for assigned customers
498
  assigned_customers = assignment_df.groupby('experiment_variant')['customer_id'].nunique().reset_index()
499
  assigned_customers.columns = ['variant', 'assigned_customers']
 
520
  variant_metrics['incremental_purchases'] = variant_metrics['purchasing_customers'] - control_metrics['purchasing_customers']
521
  variant_metrics['incremental_profit'] = variant_metrics['profit'] - control_metrics['profit']
522
 
523
+ # Create visualization using Matplotlib
524
+ fig, ax = plt.subplots(figsize=(10, 6))
525
 
526
  colors = {'Control': 'blue', '5% discount': 'green', '10% discount': 'orange',
527
  '15% discount': 'red', 'Targeted': 'purple'}
528
 
529
  for variant in variant_metrics['variant']:
530
  variant_data = variant_metrics[variant_metrics['variant'] == variant]
531
+ ax.scatter(variant_data['incremental_purchases'], variant_data['incremental_profit'],
532
+ label=variant, color=colors.get(variant, 'gray'))
533
+ ax.annotate(variant, (variant_data['incremental_purchases'].values[0],
534
+ variant_data['incremental_profit'].values[0]),
535
+ xytext=(5, 5), textcoords='offset points')
536
+
537
+ ax.set_title('Incremental Profit vs Incremental Purchases by Variant')
538
+ ax.set_xlabel('Incremental Purchases')
539
+ ax.set_ylabel('Incremental Profit')
540
+ ax.legend(loc='lower left')
541
+ ax.grid(True)
542
+
543
+ plt.tight_layout()
 
 
 
544
 
545
  return variant_metrics, fig
546
 
 
649
  with gr.Tab("Build Uplift Model"):
650
  gr.Markdown("## Build Uplift Models for All Discount Levels")
651
 
 
652
  feature_checklist = gr.CheckboxGroup(
653
  choices=['age', 'gender', 'region', 'preferred_language', 'newsletter_subscription',
654
  'preferred_payment_method', 'loyalty_level', 'main_browsing_device',