-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_maker.py
161 lines (114 loc) · 5.7 KB
/
env_maker.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
#!/usr/bin/env python
# coding: utf-8
# This scripts export states and actions, and prepares the array of inputs to the neural networks for behavioral cloning
import tensorflow as tf
tf.test.gpu_device_name()
import numpy
#from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
# fix random seed for reproducibility
numpy.random.seed(7)
import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
# fix random seed for reproducibility
numpy.random.seed(7)
from sklearn.model_selection import train_test_split
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
import pandas as pd
import numpy as np
from google.colab import files
uploaded = files.upload()
import os
import io
path_to_add = os.path.dirname(os.path.abspath(__file__)) + '/..'
sys.path = [path_to_add] + sys.path
directory= 'soccer_dataset/'
df_state_features = pd.read_excel(directory +'expert_team_features.xlsx')
expert_actions = pd.read_excel(directory +'expert_actions.xlsx')
# extracting possession ending indexes: possession can be transferred if and only if the team is not in
#the possession of the ball over two consecutive events. Thus, the unsuccessful
#touches of the opponent in fewer than 3 consecutive actions are not considered
#as a possession loss.
def End_list_index_maker(expert_actions):
End_list_index=[]
for index, row in expert_actions.iterrows():
if (expert_actions['short_team_name'].iloc[index] != expert_actions['short_team_name'].shift(-1).iloc[index]) and (expert_actions['short_team_name'].iloc[index] != expert_actions['short_team_name'].shift(-2).iloc[index]) and ((expert_actions['short_team_name'].iloc[index] == expert_actions['short_team_name'].shift(2).iloc[index]) or (expert_actions['short_team_name'].iloc[index] == expert_actions['short_team_name'].shift(1).iloc[index])) :
End_list_index.append(index)
return (End_list_index)
def Expert_list_index_maker(expert_actions):
Expert_End_list_index=[]
for i in End_list_index:
# change short_team_name to the team of interest
if Expert_actions['short_team_name'].iloc[i]== "Bayern München" :
Expert_End_list_index.append(i)
return (Expert_End_list_index)
def Opponent_list_index_maker(expert_actions):
Opponent_End_list_index=[]
for i in End_list_index:
if expert_actions['short_team_name'].iloc[i]!= "Bayern München" :
Opponent_End_list_index.append(i)
return (Opponent_End_list_index)
def expert_possession_list_maker(expert_actions, Expert_End_list_index, Opponent_End_list_index):
all_possessions=[]
for delays in range(0,len(Expert_End_list_index)):
df= df_state_features.iloc[Opponent_End_list_index[delays]+1:Expert_End_list_index[delays]+1]
pos= df.to_numpy()
all_possessions.append(pos)
return all_possessions
def Opponent_possession_list_maker(expert_actions, Expert_End_list_index, Opponent_End_list_index):
all_possessions2=[]
for delays in range(0,len(Expert_End_list_index)):
df= df_state_features.iloc[Expert_End_list_index[delays]+1:Opponent_End_list_index[delays+1]+1]
pos= df.to_numpy()
all_possessions2.append(pos)
return all_possessions2
expert_possessions= expert_possession_list_maker(expert_actions, Expert_End_list_index, Opponent_End_list_index)
opponent_possessions= Opponent_possession_list_maker(expert_actions, Expert_End_list_index, Opponent_End_list_index)
# extracting ending actions: (shot for expert team possessions), (tackles, interceptions, clearances for opponent teams possessions)
def offensive_label_list_maker(expert_actions, Expert_End_list_index):
possessions_label_offensive=[]
for i in Expert_End_list_index:
off_labels= expert_actions['type_name'].iloc[i]
possessions_label_offensive.append(off_labels)
return possessions_label_offensive
#offensive actions
offensive_possession_labels=offensive_label_list_maker(expert_actions, Expert_End_list_index)
def defensive_label_list_maker(expert_actions, Opponent_End_list_index):
possessions_label_defensive=[]
for i in Opponent_End_list_index:
labels_defensive= expert_actions['type_name'].iloc[i]
possessions_label_defensive.append(labels_defensive)
return possessions_label_defensive
#defensive acttions
defensive_possession_labels=defensive_label_list_maker(expert_actions, Opponent_End_list_index)
#convert to array
all_Opponent_possessions_array= np.array(Opponent_possessions)
all_Expert_possessions_array= np.array(expert_possessions)
for index, item in enumerate(offensive_possession_labels):
if item == 'shot':
offensive_possession_labels[index] = 1
elif item != 'shot':
offensive_possession_labels[index] = 0
for index, item in enumerate(defensive_possession_labels):
if item == 'tackle':
defensive_possession_labels[index] = 0
elif item == 'clearance':
defensive_possession_labels[index] = 1
elif item == 'interception':
defensive_possession_labels[index] = 2
else:
defensive_possession_labels[index] = 3
# saving the states and actions
pickle.dump(defensive_possession_labels, open(directory + 'defensive_actions.pkl', 'wb'))
pickle.dump(offensive_possession_labels, open(directory + 'offensive_actions.pkl', 'wb'))
pickle.dump(all_Opponent_possessions_array, open(directory + 'opponent_states.pkl', 'wb'))
pickle.dump(all_Expert_possessions_array, open(directory + 'expert_states.pkl', 'wb'))