使用pandas在python中工作,我试图将控制和治疗组分配给不同的客户组。
我有一个大型数据集。让我向您展示数据,而不是给出数据的示例,因为这总结了最重要的数据。
pd.pivot_table(df,index=['Test Group'],values=["Customer_ID"],aggfunc=lambda x: len(x.unique()))
我明白这些 测试组Customer_ID
Innovators 4634
Early Adopters 2622
Early Majority 8653
Late Majority 7645
Laggards 7645
Lost 4354
Prospective 653
我运行以下代码:
percentages = {'Innovators':[0.0,1.0],\
'Early Adopters':[0.2,0.8], \
'Early Majority':[0.1,0.9],\
'Late Majority':[0.0,1.0],\
'Laggards':[0.2,0.8],\
'Lost':[0.1,0.9],\
'Prospective':[0.1,0.9]}
def assigner(gp):
...: group = gp['Test Group'].iloc[0]
...: cut = pd.qcut(
np.arange(gp.shape[0]),
q=np.cumsum([0] + percentages[group]),
labels=range(len(percentages[group]))
).get_values()
...: return pd.Series(cut[np.random.permutation(gp.shape[0])], index=gp.index, name='flag')
df['flag'] = df.groupby('Test Group', group_keys=False).apply(assigner)
ValueError: Bin edges must be unique: array([ 0, 0, 2621], dtype=int64).
You can drop duplicate edges by setting the 'duplicates' kwarg
...并继续收到此错误
我找到了这个答案,这可能会有所帮助How to qcut with non unique bin edges?;但排名不适用于np
def assigner(gp):
...: group = gp['Campaign Test Description'].iloc[0]
...: cut = pd.qcut(
np.arange(gp.shape[0]).rank(method='first'),
q=np.cumsum([0] + percentages[group]),
labels=range(len(percentages[group]))
).get_values()
...: return pd.Series(cut[np.random.permutation(gp.shape[0])], index=gp.index, name='flag')
AttributeError: 'numpy.ndarray' object has no attribute 'rank'
我尝试删除重复项
def assigner(gp):
...: group = gp['Campaign Test Description'].iloc[0]
...: cut = pd.qcut(
np.arange(gp.shape[0]),
q=np.cumsum([0] + percentages[group]),
labels=range(len(percentages[group])),duplicates='drop'
).get_values()
...: return pd.Series(cut[np.random.permutation(gp.shape[0])], index=gp.index, name='flag')
ValueError: Bin labels must be one fewer than the number of bin edges
仍然出现错误
答案 0 :(得分:1)
您正在进行火车/测试拆分,这通常用于机器学习。这是一种方法(仔细检查我的百分比是否正确):
df_pct = pd.DataFrame({ 'ID': ['Innovators','Early Adopters' ,'Early Majority','Late Majority','Laggards','Lost','Prospective'], 'test_cutoff':[1,0.8,0.9,0.1,0.8,0.9,0.9]})
df=df.merge(df_pct)
df['is_test'] = np.random.uniform(0, 1, len(df)) >= df['test_cutoff']
此外,您的“延迟多数”百分比不等于100。