我有一个叫做约束矩阵的东西,每个用户(muid)最多可以被发送N次内容(策略)。所以我把它表示为一个数据帧:
tactic max_times
100 50
101 35
102 23
103 30
...........
.........
所以每个用户最多只能发送100次,最多50次,最多35次,以此类推。你知道吗
现在我们必须模拟每个用户在不同的迭代中发送特定的内容(策略)。你知道吗
## code to simulate data
import random
muid=range(1,11)
tactic=range(100,110)
##create dataframe for first iteration
temp=DataFrame({'muid':muid,'tactic':tactic})
print temp
muid tactic
0 1 100
1 2 101
2 3 102
3 4 103
4 5 104
##reshuffle the sent channel
tactic1=random.sample(range(100,110),10)
##dataframe for second iteration
temp1=DataFrame({'muid':muid,'tactic':tactic1})
print temp1
muid tactic
0 1 101
1 2 100
2 3 106
3 4 107
4 5 109
5 6 102
6 7 104
7 8 108
8 9 105
9 10 103
###code to calculate how many times user has been sent/exposed to a channel
#groupby for first iteration
groupedby=temp.groupby(['muid','tactic'])['tactic'].agg({'count':'count'})
groupedby.reset_index(inplace=True)
#groupby for second iteration
groupedby1=temp1.groupby(['muid','tactic'])['tactic'].agg({'count':'count'})
groupedby1.reset_index(inplace=True)
appended_output=groupedby.append(groupedby1)
total_counts_at_second_iteration=appended_output.groupby(['muid','tactic'])['tactic'].agg({'total_sent':'count'})
因此,在第二次迭代之后的最终输出如下所示:
##top 8 rows
muid tactic total_sent
0 1 100 1
1 1 101 1
2 2 101 1
3 2 107 1
4 3 102 1
5 3 104 1
6 4 103 2
7 5 102 1
8 5 104 1
上述输出对应于2次迭代。现在我想把上面的代码放在循环中(N次迭代),一旦对任何发送的策略实现了约束,就不应该再发送该策略,并且应该显示一条消息“limitreatedforthetractic”。任何帮助都应该感激。你知道吗
你可以用np.随机选择它有一个关于重量的参数p。然后,如果您在字典中跟踪每个用户的约束,您可以迭代每个策略,确保为每个用户更新每次迭代的约束/权重。大致如下所示:
相关问题 更多 >
编程相关推荐