Spaces:
Runtime error
Runtime error
Update elo.py
Browse files
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(
|
16 |
"""
|
17 |
-
Update ELO ratings for two players.
|
18 |
|
19 |
-
:param
|
20 |
-
:param winner: The
|
21 |
-
:param loser: The
|
22 |
:param k_factor: The K-factor used in ELO rating (default is 32).
|
23 |
-
:return: Updated ELO ratings.
|
24 |
"""
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
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
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
-
return
|
|
|
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
|