Python Trueskill - Using Dictionaries

房东的猫 提交于 2020-02-07 06:58:09

问题


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
.......

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?

-- EDIT, USING DICTIONARY --

So using dictionaries I can do

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])

回答1:


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

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))

The result I got:

{'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)}


来源:https://stackoverflow.com/questions/24723975/python-trueskill-using-dictionaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!