-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFeatures_Extraction_Head_To_Head_Balance.py
237 lines (148 loc) · 5.03 KB
/
Features_Extraction_Head_To_Head_Balance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
matches_df = pd.read_csv('final_df1.csv')
# In[2]:
matches_df
# In[3]:
#head to head balance
#transform player_win : t -> 1, f -> 0
matches_df['player_1_v'] = 0
matches_df['player_2_v'] = 0
for i in range(0,len(matches_df)):
if matches_df.iloc[i]['player_1_victory'] == 't':
matches_df.at[i,'player_1_v'] = 1
if matches_df.iloc[i]['player_1_victory'] == 'f':
matches_df.at[i,'player_1_v'] = 0
if matches_df.iloc[i]['player_2_victory'] == 't':
matches_df.at[i,'player_2_v'] = 1
if matches_df.iloc[i]['player_2_victory'] == 'f':
matches_df.at[i,'player_2_v'] = 0
# In[ ]:
#let's check the frequency of player_1_v and player_2_v (we do this because one of these will be our target variable)
#we want the target to be more or less balanced, in order to avoid bias in our model
# In[5]:
import seaborn as sns
sns.displot(matches_df['player_1_v'],aspect=3)
# In[6]:
sns.displot(matches_df['player_2_v'],aspect=3)
# In[ ]:
#data is balanced so we have one less worry
# In[4]:
#get player id's
players = []
for item in matches_df['player_id'].unique():
players.append(item)
for item in matches_df['opponent_id'].unique():
if item not in players:
players.append(item)
players
# In[19]:
def head_to_head(player,opponent,data):
temp = data.loc[((data['player_id']==player)&(data['opponent_id']==opponent))
|((data['player_id']==opponent)&(data['opponent_id']==player))]
if len(temp) == 0:
return
balance = []
reference_player = temp.iloc[0]['player_id']
balance.append(0)
for i in range(0,len(temp)):
if data.iloc[i]['player_id'] == reference_player:
if data.iloc[i]['player_1_v'] == 1:
balance.append(1)
if data.iloc[i]['player_1_v'] == 0:
balance.append(-1)
if data.iloc[i]['opponent_id'] == reference_player:
if data.iloc[i]['player_1_v'] == 0:
balance.append(1)
if data.iloc[i]['player_1_v'] == 1:
balance.append(-1)
#now set hth for reference player
#first game
data.at[temp.index[0],'hth_1'] = balance[0]
data.at[temp.index[0],'hth_2'] = balance[0]
#games after first
for i in range(1,len(temp)):
if temp.iloc[i]['player_id'] == reference_player:
data.at[temp.index[i],'hth_1'] = sum(balance[:i+1])
data.at[temp.index[i],'hth_2'] = -sum(balance[:i+1])
if temp.iloc[i]['opponent_id'] == reference_player:
data.at[temp.index[i],'hth_2'] = sum(balance[:i+1])
data.at[temp.index[i],'hth_1'] = -sum(balance[:i+1])
return
# In[21]:
#create list of players
hth_list = []
for player in players:
for opponent in players:
if opponent == player:
continue
string = player + ':' + opponent
hth_list.append(string)
# In[22]:
len(hth_list)
# In[26]:
hth_series = pd.Series(hth_list)
hth_series = hth_series.unique()
# In[29]:
matches_df['hth_1'] = 0
matches_df['hth_2'] = 0
i = 0
#this way it would take approximately 32 days to complete, find a different solution
for player in players:
for opponent in players:
if player == opponent:
continue
head_to_head(player,opponent,matches_df)
i = i + 1
print('iteration number : ',i,' of 94410372')
# In[6]:
#differnt solution
#for every match look in the past for head to head encounters
#compute balance and assign
def get_balance(row,player,opponent,data):
temp = data.iloc[:row-1]
temp = temp.loc[((temp['player_id']==player)&(temp['opponent_id']==opponent))
|((temp['player_id']==opponent)&(temp['opponent_id']==player))]
if len(temp)==0:
return 0
balance = 0
for i in range(0,len(temp)):
if temp.iloc[i]['player_id'] == player:
if temp.iloc[i]['player_1_v'] == 1:
balance = balance + 1
if temp.iloc[i]['player_1_v'] == 0:
balance = balance - 1
if temp.iloc[i]['player_id'] == opponent:
if temp.iloc[i]['player_1_v'] == 1:
balance = balance - 1
if temp.iloc[i]['player_1_v'] == 0:
balance = balance + 1
return balance
# In[11]:
#function check
matches_df.loc[((matches_df['player_id']=='roger-federer')&(matches_df['opponent_id']=='carlos-moya'))
|((matches_df['player_id']=='carlos-moya')&(matches_df['opponent_id']=='roger-federer'))]
# In[8]:
get_balance(40000,'roger-federer','carlos-moya',matches_df)
# In[ ]:
#the function works
# In[9]:
#compute and assign head to head balances for every row of data
matches_df['hth_1'] = 0
matches_df['hth_2'] = 0
for i in range(0,len(matches_df)):
temp_player = matches_df.iloc[i]['player_id']
temp_opponent = matches_df.iloc[i]['opponent_id']
bal = get_balance(i,temp_player,temp_opponent,matches_df)
matches_df.at[i,'hth_1'] = bal
matches_df.at[i,'hth_2'] = -bal
print('iteration number : ',i,' of ',len(matches_df))
# In[10]:
matches_df
# In[12]:
#save
matches_df.to_csv('final_df1.csv')
# In[ ]: