自动检查阈值上限的条件

2024-09-23 08:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个叫做约束矩阵的东西,每个用户(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”。任何帮助都应该感激。你知道吗


Tags: to用户forcountrange策略tempsent
1条回答
网友
1楼 · 发布于 2024-09-23 08:28:19

你可以用np.随机选择它有一个关于重量的参数p。然后,如果您在字典中跟踪每个用户的约束,您可以迭代每个策略,确保为每个用户更新每次迭代的约束/权重。大致如下所示:

import pandas as pd
import numpy as np

muid=range(1,11)
tactic=range(100,110) 

# individual max_constraints for each tactic 
# (Ordering must be consistent with tactic!)
max_times=[50,35,23,30,10,50,5,12,17,22] 

total_times = sum(max_times) # total tactics given out

# keeps track of the number of available tactics (constraints) for each user_id
user_constraint_dict = {user_id:{idn:max_tactic for idn, max_tactic in 
zip(tactic,max_times)} for user_id in muid }

def constraint_to_prob(constraint_dict):

    '''
    convert a dictionary containing weights to probabilities
    for use by np.random.choice
    '''

    total_remaining = float(sum(constraint_dict.values()))
    prob_dict = {tactic:constraint_num/total_remaining for tactic, constraint_num in constraint_dict.iteritems()}

    return prob_dict

tactic_df = pd.DataFrame(index=muid,columns=['tactic_'+str(i) for i in range(total_times)])

for tactic_iter in range(total_times):

    for user_id in tactic_df.index:

        user_weight_dict = constraint_to_prob(user_constraint_dict[user_id])
        user_weights = [user_weight_dict[tac] for tac in tactic]
        chosen_tactic = np.random.choice(tactic,p=user_weights)

        tactic_df.loc[user_id,'tactic_'+str(tactic_iter)] = chosen_tactic

        # update constraints for the given user
        user_constraint_dict[user_id][chosen_tactic]-=1

相关问题 更多 >