python实现ab测试例子
时间: 2024-12-18 10:34:06 浏览: 4
在Python中,实现A/B测试通常会涉及到随机分配用户到不同的实验组(A组或B组),然后收集数据并分析结果。这里我们可以使用`random`模块来进行简单的概率选择。下面是一个基本的例子:
```python
import random
def conduct_ab_test(total_users, group_a_size, group_b_size):
# 确定每个用户的分组
def assign_group():
return "Group A" if random.random() < group_a_size / total_users else "Group B"
# 创建空字典存储结果
results = {"Group A": [], "Group B": []}
# 模拟用户并记录其选择
for _ in range(total_users):
user_choice = assign_group()
results[user_choice].append(user_choice)
return results
# 示例:1000个用户,50%进入A组,50%进入B组
total_users = 1000
group_a_size = total_users * 0.5
group_b_size = total_users - group_a_size
results = conduct_ab_test(total_users, group_a_size, group_b_size)
print(f"Results: {results}")
阅读全文