本文整理汇总了Python中library.classes.GeneralMethods.weightedChoice方法的典型用法代码示例。如果您正苦于以下问题:Python GeneralMethods.weightedChoice方法的具体用法?Python GeneralMethods.weightedChoice怎么用?Python GeneralMethods.weightedChoice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类library.classes.GeneralMethods
的用法示例。
在下文中一共展示了GeneralMethods.weightedChoice方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: messageSelectionMethod
# 需要导入模块: from library.classes import GeneralMethods [as 别名]
# 或者: from library.classes.GeneralMethods import weightedChoice [as 别名]
def messageSelectionMethod(self, currentTimeStep, user, currentTopics, **conf):
if self.lastObservedTimeStep != currentTimeStep:
self._updateTopicProbabilities(currentTimeStep, currentTopics, **conf)
message = None
if GeneralMethods.trueWith(user.messagingProbability):
if GeneralMethods.trueWith(user.newTopicProbability):
topic = Topic(len(currentTopics))
currentTopics.append(topic)
message = user.generateMessage(currentTimeStep, topic)
else:
if GeneralMethods.trueWith(user.probabilityOfPickingPopularTopic):
if user.topicClass != None:
topicIndex = GeneralMethods.weightedChoice(
[i[1] for i in self.topicProbabilities[user.topicClass]]
)
topic = self.topicProbabilities[user.topicClass][topicIndex][0]
message = user.generateMessage(currentTimeStep, topic)
if not GeneralMethods.trueWith(topic.stickiness):
message = None
else:
topicIndex = GeneralMethods.weightedChoice([i[1] for i in self.topTopics])
topic = self.topTopics[topicIndex][0]
message = user.generateMessage(currentTimeStep, topic)
else:
if user.topicClass != None:
message = user.generateMessage(
currentTimeStep, random.choice(self.topicProbabilities[user.topicClass])[0]
)
else:
topicIndex = GeneralMethods.weightedChoice([i[1] for i in self.allTopics])
topic = self.allTopics[topicIndex][0]
message = user.generateMessage(currentTimeStep, topic)
return message
示例2: hedging_get_best_model
# 需要导入模块: from library.classes import GeneralMethods [as 别名]
# 或者: from library.classes.GeneralMethods import weightedChoice [as 别名]
def hedging_get_best_model(mf_model_id_to_cumulative_losses):
if not mf_model_id_to_cumulative_losses: return random.sample(LIST_OF_MODELS, 1)[0]
else:
total_weight = float(sum(mf_model_id_to_cumulative_losses.values()))
for model in mf_model_id_to_cumulative_losses.keys(): mf_model_id_to_cumulative_losses[model]/=total_weight
id_and_model_id_and_cumulative_losses = [(id, model_id, cumulative_loss)
for id, (model_id, cumulative_loss) in
enumerate(mf_model_id_to_cumulative_losses.iteritems())
]
selected_id = GeneralMethods.weightedChoice(zip(*id_and_model_id_and_cumulative_losses)[2])
return filter(lambda (id, model, _): id==selected_id, id_and_model_id_and_cumulative_losses)[0][1]