Python Trueskill-使用字典 [英] Python Trueskill - Using Dictionaries

查看:136
本文介绍了Python Trueskill-使用字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python的 Trueskill 程序包,并弄清楚了基础知识,

I'm trying out the Trueskill package from Python and have the basics worked out,

对于两个玩家我可以做到

For a two player match up I can do

alice = Rating()
bob = Rating()
alice, bob = rate_1vs1(alice, bob)

对于多个玩家对决,我可以做

And for multiple player matchups I can do

alice = Rating()
bob = Rating()
eve = Rating()

alice, bob, eve = rate([(alice,),(bob,),(eve,)], ranks=[0, 1, 2])

我目前有一个数据库,其结构如下,我希望对...进行评级

I currently have a database with is structured as follows which I'd like to perform ratings on...

game participant rank ....(various game stats)
1    alice       2
1    bob         1
1    eve         3
2    alice       1
2    eve         1
3    bob         1
3    carol       2
3    alice       3
3    ted         4
3    eve         5
.......

这是一个简化的版本,因为某些游戏最多可容纳2位参与者,最多可容纳20位参与者.我要做的是逐个游戏地阅读数据库游戏,读取参与者并根据结果进行更新.我不确定执行此操作的最佳方法,因为我知道动态创建变量是一个很大的缺点-但是什么是正确的方法?

This is a simplified version as some of the games feature 2 participants, some up to 20. What I want to do is read through the database game by game, reading the participants in and performing updates based on the results. I'm not sure of the best way to do this as I know dynamically creating variables is a big no - but what's the right way?

-编辑,使用字典-

因此,我可以使用字典

ratings = {}
k = 0
for participant in results:
    ratings[k] = Rating
    k += 1

但是我不知道如何对玩家进行评分,因为以下内容不起作用,因为字典将是一个评分组,而不是单个参与者

However I can't figure out how to rate the players, as the following doesn't work as the dictionary would be a rating group, rather than individual participants

new_ratings = rate(ratings.values(),ranks=[0-k])

推荐答案

我在申请电视节目时做过类似的事情:

I did similar thing for applying to a TV show: https://gist.github.com/sublee/4967876

defaultdictgroupby可以帮助您实现所需的目标:

defaultdict and groupby may help you to achieve what you want:

from collections import defaultdict
from itertools import groupby
from pprint import pprint
from trueskill import Rating, rate

results = [(1, 'alice', 2),
           (1, 'bob', 1),
           (1, 'eve', 3),
           (2, 'alice', 1),
           (2, 'eve', 1),
           (3, 'bob', 1),
           (3, 'carol', 2),
           (3, 'alice', 3),
           (3, 'ted', 4),
           (3, 'eve', 5)]
ratings = defaultdict(Rating)

for game_id, result in groupby(results, lambda x: x[0]):
    result = list(result)
    rating_groups = [(ratings[name],) for game_id, name, rank in result]
    ranks = [rank for game_id, name, rank in result]
    transformed_groups = rate(rating_groups, ranks=ranks)
    for x, (game_id, name, rank) in enumerate(result):
        ratings[name], = transformed_groups[x]

pprint(dict(ratings))

我得到的结果:

{'alice': trueskill.Rating(mu=23.967, sigma=4.088),
 'bob': trueskill.Rating(mu=36.119, sigma=5.434),
 'carol': trueskill.Rating(mu=29.226, sigma=5.342),
 'eve': trueskill.Rating(mu=16.740, sigma=4.438),
 'ted': trueskill.Rating(mu=21.013, sigma=5.150)}

这篇关于Python Trueskill-使用字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆