Skip to content

Instantly share code, notes, and snippets.

@BlueRayi
Created October 11, 2019 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlueRayi/8cfec72998f39719d3f00e500b696cc3 to your computer and use it in GitHub Desktop.
Save BlueRayi/8cfec72998f39719d3f00e500b696cc3 to your computer and use it in GitHub Desktop.
TrueSkill によるフィッティング
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from pathlib import Path
# In[2]:
import pandas as pd
import trueskill
from trueskill import rate_1vs1
# In[3]:
def expose_and_clip(rating, env=None, minimum=0., maximum=50.):
env = env if env else trueskill.global_env()
return min(max(minimum, env.expose(rating)), maximum)
# In[4]:
env = trueskill.TrueSkill()
# In[5]:
players = {
'Emu': env.create_rating(),
'Parado': env.create_rating(),
'Niko': env.create_rating(),
'Taiga': env.create_rating(),
'Hiiro': env.create_rating(),
'Kiriya': env.create_rating(),
'Kuroto': env.create_rating(),
'Poppy': env.create_rating(),
}
# In[6]:
players_names = ['Emu', 'Parado', 'Niko', 'Taiga', 'Hiiro', 'Kiriya', 'Kuroto', 'Poppy']
# In[7]:
simulation_result_csv = Path('./simulation_result.csv')
result_df = pd.read_csv(simulation_result_csv)
result_df
# In[8]:
rating_rows = []
mu_rows = []
sigma_rows = []
# In[9]:
rating_row = {}
mu_row = {}
sigma_row = {}
for players_name in players_names:
rating_row[players_name] = expose_and_clip(players[players_name], env=env)
mu_row[players_name] = players[players_name].mu
sigma_row[players_name] = players[players_name].sigma
rating_rows.append(pd.Series(rating_row))
mu_rows.append(pd.Series(mu_row))
sigma_rows.append(pd.Series(sigma_row))
# In[10]:
for row_tuple in result_df.iterrows():
row = row_tuple[1]
for i in range(4):
winner_name = row[f'Match {i + 1} Winner']
loser_name = row[f'Match {i + 1} Loser']
winner = players[winner_name]
loser = players[loser_name]
new_winner,new_loser = rate_1vs1(winner, loser, env=env)
players[winner_name] = new_winner
players[loser_name] = new_loser
rating_row = {}
mu_row = {}
sigma_row = {}
for players_name in players_names:
rating_row[players_name] = expose_and_clip(players[players_name], env=env)
mu_row[players_name] = players[players_name].mu
sigma_row[players_name] = players[players_name].sigma
rating_rows.append(pd.Series(rating_row))
mu_rows.append(pd.Series(mu_row))
sigma_rows.append(pd.Series(sigma_row))
# In[11]:
rating_df = pd.DataFrame(rating_rows).reset_index()
rating_df
# In[12]:
mu_df = pd.DataFrame(mu_rows).reset_index()
sigma_df = pd.DataFrame(sigma_rows).reset_index()
# In[13]:
trueskill_rating_csv = Path('./trueskill_rating.csv')
trueskill_mu_csv = Path('./trueskill_mu.csv')
trueskill_sigma_csv = Path('./trueskill_sigma.csv')
rating_df.to_csv(trueskill_rating_csv, index=False)
mu_df.to_csv(trueskill_mu_csv, index=False)
sigma_df.to_csv(trueskill_sigma_csv, index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment