-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
165 lines (134 loc) · 6.47 KB
/
filters.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
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from imblearn.over_sampling import SMOTE
def games_up_to_2017_tourney_filter(df):
'''Filter for games up to 2017 tourney'''
notourney2018 = (df['GameType'] != 'tourney2018')
noseason2018 = (df['GameType'] != 'season2018')
notourney2017 = (df['GameType'] != 'tourney2017')
games_up_to_2017_tourney = df[notourney2018 & noseason2018 & notourney2017]
return games_up_to_2017_tourney
def games_up_to_2018_season_filter(df):
'''Filter for games up to 2018 season'''
notourney2018 = (df['GameType'] != 'tourney2018')
noseason2018 = (df['GameType'] != 'season2018')
games_up_to_2018_season = df[notourney2018 & noseason2018]
return games_up_to_2018_season
def season2018_filter(df):
'''Filter for 2018 season games'''
season2018cond = (df['GameType'] == 'season2018')
season2018 = df[season2018cond]
return season2018
def games_up_to_2018_tourney_filter(df):
'''Filter for games up to 2018 tourney'''
notourney2018 = (df['GameType'] != 'tourney2018')
games_up_to_2018_tourney = df[notourney2018]
return games_up_to_2018_tourney
def tourney2018_filter(df):
'''Filter for 2018 tourney games'''
tourney2018cond = (df['GameType'] == 'tourney2018')
tourney2018 = df[tourney2018cond]
return tourney2018
def apply_filter(df, filter):
return filter(df)
def pre_matchup_feature_selection(df, feature_set='gamelogs'):
'''
Inputs: Model DataFrame
Outputs: DataFrame with features selected
'''
if feature_set == 'gamelogs':
df = df[['W', 'Tm', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos']]
elif feature_set == 'exp':
df = df[['W', 'Tm', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'exp_factor']]
elif feature_set == 'tcf':
df = df[['W', 'Tm', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'C0', 'C1', 'C2', 'F0', 'F1', 'F2', 'G0', 'G1', 'G2', 'G3']]
elif feature_set == 'exp_tcf':
df = df[['W', 'Tm', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'exp_factor', 'C0', 'C1', 'C2', 'F0', 'F1', 'F2', 'G0', 'G1',
'G2', 'G3']]
elif feature_set == 'odds':
df = df[['W', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'exp_factor', 'C0', 'C1', 'C2', 'F0', 'F1', 'F2', 'G0', 'G1',
'G2', 'G3', 'final_p']]
return df
def post_merge_feature_selection(df, feature_set='gamelogs'):
'''
Inputs: Model DataFrame
Outputs: DataFrame with features selected
'''
if feature_set == 'gamelogs':
df = df[['W', 'GameType', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp', 'OPFTp', 'OPORBpg',
'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg', 'OPTOpg', 'OPPFpg',
'OPsos']]
elif feature_set == 'exp':
df = df[['W', 'GameType', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'exp_factor', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp',
'OPFTp', 'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg',
'OPTOpg', 'OPPFpg', 'OPsos', 'OPexp_factor']]
elif feature_set == 'tcf':
df = df[['W', 'GameType', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp', 'ORBpg',
'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'C0', 'C1', 'C2', 'F0', 'F1', 'F2', 'G0', 'G1', 'G2', 'G3',
'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp', 'OPFTp', 'OPORBpg',
'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg', 'OPTOpg', 'OPPFpg',
'OPsos', 'OPC0', 'OPC1', 'OPC2', 'OPF0', 'OPF1', 'OPF2', 'OPG0',
'OPG1', 'OPG2', 'OPG3']]
elif feature_set == 'exp_tcf':
df = df[['W', 'GameType', 'Wp', 'ppg', 'pApg', 'FGp',
'3Pp', 'FTp', 'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg',
'PFpg', 'sos', 'exp_factor', 'C0', 'C1', 'C2', 'F0', 'F1', 'F2',
'G0', 'G1', 'G2', 'G3', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp',
'OPFTp', 'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg',
'OPTOpg', 'OPPFpg', 'OPsos', 'OPexp_factor', 'OPC0', 'OPC1', 'OPC2',
'OPF0', 'OPF1', 'OPF2', 'OPG0', 'OPG1', 'OPG2', 'OPG3']]
elif feature_set == 'odds':
df = df[['W', 'GameType', 'Wp', 'ppg', 'pApg', 'FGp',
'3Pp', 'FTp', 'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg',
'PFpg', 'sos', 'exp_factor', 'C0', 'C1', 'C2', 'F0', 'F1', 'F2',
'G0', 'G1', 'G2', 'G3', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp',
'OPFTp', 'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg',
'OPTOpg', 'OPPFpg', 'OPsos', 'OPexp_factor', 'OPC0', 'OPC1', 'OPC2',
'OPF0', 'OPF1', 'OPF2', 'OPG0', 'OPG1', 'OPG2', 'OPG3', 'final_p']]
return df
def data_for_model(df, feature_set='gamelogs', train_filter=games_up_to_2018_season_filter, test_filter=season2018_filter):
'''
Inputs: Model DataFrame
Outputs: train and test DataFrames
'''
df = post_merge_feature_selection(df, feature_set=feature_set)
train_df = apply_filter(df, train_filter)
test_df = apply_filter(df, test_filter)
train_df = train_df.drop(['GameType'], axis=1)
test_df = test_df.drop(['GameType'], axis=1)
return train_df, test_df
def set_up_data(train_df, test_df):
'''Set up features and targets'''
X_train = train_df.iloc[:, 1:].values
y_train = train_df.iloc[:, 0].values
X_test = test_df.iloc[:, 1:].values
y_test = test_df.iloc[:, 0].values
'''Balance classes'''
X_train, y_train = SMOTE().fit_sample(X_train, y_train)
'''Standardize data'''
scale = StandardScaler()
scale.fit(X_train)
X_train = scale.transform(X_train)
X_test = scale.transform(X_test)
return X_train, y_train, X_test, y_test
def load_model_data_for_mlp(data_df):
'''Set up features and targets for mlp'''
X = data_df.iloc[:, 1:].values
y = data_df.iloc[:, 0].values
X = X_train.astype(theano.config.floatX)
return X, y