rwitz commited on
Commit
dd89a50
1 Parent(s): 0d45e75

Update elo.py

Browse files
Files changed (1) hide show
  1. elo.py +23 -14
elo.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  def calculate_elo(old_rating, opponent_rating, score, k_factor=32):
2
  """
3
  Calculate the new ELO rating for a player.
@@ -12,26 +15,32 @@ def calculate_elo(old_rating, opponent_rating, score, k_factor=32):
12
  new_rating = old_rating + k_factor * (score - expected_score)
13
  return new_rating
14
 
15
- def update_elo_ratings(ratings, winner, loser, k_factor=32):
16
  """
17
- Update ELO ratings for two players.
18
 
19
- :param ratings: A dictionary of current ELO ratings.
20
- :param winner: The model name of the winning player.
21
- :param loser: The model name of the losing player.
22
  :param k_factor: The K-factor used in ELO rating (default is 32).
23
- :return: Updated ELO ratings.
24
  """
25
- winner_old_rating = ratings[winner]
26
- loser_old_rating = ratings[loser]
 
 
 
 
27
 
28
- # Winner's new rating
29
  winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1, k_factor)
30
- # Loser's new rating
31
  loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0, k_factor)
32
 
33
- # Update the ratings
34
- ratings[winner] = winner_new_rating
35
- ratings[loser] = loser_new_rating
 
 
 
36
 
37
- return ratings
 
1
+ import pandas as pd
2
+ from datasets import Dataset
3
+
4
  def calculate_elo(old_rating, opponent_rating, score, k_factor=32):
5
  """
6
  Calculate the new ELO rating for a player.
 
15
  new_rating = old_rating + k_factor * (score - expected_score)
16
  return new_rating
17
 
18
+ def update_elo_ratings(ratings_dataset, winner, loser, k_factor=32):
19
  """
20
+ Update ELO ratings for two players in a Hugging Face dataset.
21
 
22
+ :param ratings_dataset: A Hugging Face dataset of current ELO ratings.
23
+ :param winner: The name of the winning player.
24
+ :param loser: The name of the losing player.
25
  :param k_factor: The K-factor used in ELO rating (default is 32).
26
+ :return: Updated ELO ratings as a Hugging Face dataset.
27
  """
28
+ # Convert the Hugging Face dataset to a pandas DataFrame for easier manipulation
29
+ ratings_df = pd.DataFrame(ratings_dataset)
30
+
31
+ # Extract old ratings
32
+ winner_old_rating = ratings_df.loc[ratings_df == winner, 'rating'].iloc[0]
33
+ loser_old_rating = ratings_df.loc[ratings_df == loser, 'rating'].iloc[0]
34
 
35
+ # Calculate new ratings
36
  winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1, k_factor)
 
37
  loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0, k_factor)
38
 
39
+ # Update the DataFrame
40
+ ratings_df.loc[ratings_df == winner, 'rating'] = winner_new_rating
41
+ ratings_df.loc[ratings_df == loser, 'rating'] = loser_new_rating
42
+
43
+ # Convert the DataFrame back to a Hugging Face dataset
44
+ updated_ratings_dataset = Dataset.from_pandas(ratings_df)
45
 
46
+ return updated_ratings_dataset