-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiments.py
More file actions
1347 lines (1146 loc) · 63 KB
/
Copy pathexperiments.py
File metadata and controls
1347 lines (1146 loc) · 63 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
from glob import glob
from os import makedirs
import os
from os.path import exists, join as join_path
from pickle import dump , load
import pickle
from scipy.stats import ttest_rel
from sklearn.cross_validation import cross_val_score
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.grid_search import RandomizedSearchCV
from sklearn.learning_curve import learning_curve
from sklearn.multiclass import OneVsRestClassifier as OVRC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.dummy import DummyClassifier
from tabulate import tabulate
import warnings
from data.cross_validation import CrossValidation
from utils.argumet_parsers import ExperimentArgsParser
from utils.constants import LEAGUES, MAX_YEAR, MIN_YEAR
from utils.decorators import timed, move_to_root_dir
class Experiment():
"""
Abstract Experiment class.
Experiments classes provide basic functionality to running an experiment-
allows saving and loading the results
automatically loads data for cross validation
"""
def __init__(self,dir_name,test=False):
"""
Creates a new Experiment instance.
Requires a dir_name (to save results) and a flag to indicate whether it's test environment or not.
"""
self._dir_name = dir_name
self.results_dir = join_path('Results',dir_name)
self._test = test
self._loaded_data = None
def save(self,data):
"""
Saves data into the results dir under the special format {experiment_name}.results
"""
if not exists(self.results_dir):
makedirs(self.results_dir)
with open(join_path(self.results_dir,self.name+'.results'),'w') as _f:
dump(data, _f)
def load(self):
"""
Loads results from previous runs into _loaded_data attribute.
"""
_path = glob(join_path(self.results_dir,'%s.results'%self.name)).pop()
with open(_path,'r') as _f:
self._loaded_data = load(_f)
def get_data(self):
"""
Loads all the examples and tags needed for the experiment.
Loads the all of the examples and tags, and also creates a cross validation for using in the estimators \ searches.
"""
self.cv = CrossValidation(test=self._test)
lookback = 2 if self._test else 15
self.cv.load_data(lookback)
self.X = self.cv.complete_examples
self.y = self.cv.complete_tags
def run(self):
"""
Runs the experiment.
You should override this function in derived classes to configure the experiment
"""
self.get_data()
def load_params(self):
"""
Loads the parameters of the experiment.
You should override this in derived classes to configure the experiment parameters
"""
raise NotImplementedError
def t_test(self,original_measurements, measurements_after_alteration):
"""
This is the paired T-test (for repeated measurements):
Given two sets of measurements on the SAME data points (folds) before and after some change,
Checks whether or not they come from the same distribution.
This T-test assumes the measurements come from normal distributions.
Returns:
The probability the measurements come the same distributions
A flag that indicates whether the result is statically significant
A flag that indicates whether the new measurements are better than the old measurements
"""
SIGNIFICANCE_THRESHOLD= 0.05
test_value, probability= ttest_rel(original_measurements, measurements_after_alteration)
is_significant= probability/2 < SIGNIFICANCE_THRESHOLD
is_better= sum(original_measurements) < sum(measurements_after_alteration) #should actually compare averages, but there's no need since it's the same number of measurments.
return probability/2 if is_better else 1-probability/2, is_significant, is_better
def _load_prev_experiment(self,exp):
"""
A function to load a previous experiment.
"""
try:
exp.load()
return True
except Exception as e:
print 'Failed to load previous {ex} experiment\n. If you would like to run the {ex} experiment, Please type:\n Yes I am sure'.format(ex=exp.name)
ans = raw_input('>>>')
if ans == 'Yes I am sure':
exp.run()
else:
return False
def report(self,verbosity,outfile):
"""
A method to report the experiment's results.
"""
if self._loaded_data is None:
try:
self.load()
except:
if not self._load_prev_experiment(self):
print 'Can not report, must run experiment before!'
return
print self._begining_report
if verbosity == 0:
print self._no_detail
elif verbosity == 1:
print self._detail
elif verbosity == 2:
print self._more_detail
print self._ending_report
class _ResultsDistExperiments(Experiment):
"""
Helper experiment to show the distribution of the final results of the games we use for data.
"""
def __init__(self,dir_name,test=False):
Experiment.__init__(self,dir_name,test)
self.name = 'Results_Dist'
def get_data(self):
"""
Loads all the examples and tags needed for the experiment.
Because in this experiment we only check the distribution of the final results, we set the lookback to 1 - for faster generation of the examples and tags
"""
self.cv = CrossValidation(test=self._test)
lookback = 1
self.cv.load_data(lookback)
self.X = self.cv.complete_examples
self.y = self.cv.complete_tags
def run(self):
"""
Create examples and calculate the distribution of the final results
"""
self.get_data()
from collections import Counter
self._loaded_data = {}
self._loaded_data['Results_Dist'] = Counter(self.y)
self.save(self._loaded_data)
_begining_report = """This experiments calculates the final results distribution in all leagues for the seasons of 2010-2014."""
_ending_report = ''
@property
def _no_detail(self):
"""
Reporting on low verbosity - print the distribution
"""
_denominator = float(sum(self._loaded_data['Results_Dist'].values()))
return 'Home Win %: {0:.4f}%, Draw %: {1:.4f}%, Away Win %: {2:.4f}%'.format(self._loaded_data['Results_Dist'][1]/_denominator,
self._loaded_data['Results_Dist'][0]/_denominator,
self._loaded_data['Results_Dist'][-1]/_denominator)
@property
def _detail(self):
"""
Reporting on medium verbosity - plot pie charts of results distribution
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# The slices will be ordered and plotted counter-clockwise.
labels = 'Home Win','Draw','Away Win'
sizes = [self._loaded_data['Results_Dist'][1],self._loaded_data['Results_Dist'][0],self._loaded_data['Results_Dist'][-1]]
colors = ['yellowgreen', 'gold', 'lightcoral']
explode = (0, 0.1, 0) # only "explode" the 2nd slice (i.e. 'Draw', to show how many games didn't finish in defeat of one of the teams)
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.title('Final game results distribution for all league in season 2010-2014')
plt.show()
return ''
class BestParamsExperiment(Experiment):
"""
An experiment class to search for the best hyperparameters for an estimator.
The search is done using RandomGridSearchCV, on the DecisionTreeClassifer estimator and the
RandomForestClassifier estimator.
"""
def __init__(self,dir_name,test=False):
Experiment.__init__(self,dir_name,test)
self.name = 'Best_Params'
def load_params(self):
"""
Loads the parameters for the experiment - the grids to search on.
To change parameters or values for one of the estimators - change to correct field.
"""
if not self._test:
return {'DTC':{'criterion':['gini','entropy'],\
'max_depth':range(5,61,5),\
'max_leaf_nodes':[None]+range(10,51,5),\
'min_samples_leaf':range(15,100,15),\
'min_samples_split':range(2,51,2),\
'splitter':['random','best'],\
'max_features':[None,'auto','log2']+range(10,61,5)},\
'RFC':{'criterion':['gini','entropy'],\
'max_depth':range(5,61,5),\
'max_leaf_nodes':[None]+range(10,51,5),\
'min_samples_leaf':range(15,100,15),\
'min_samples_split':range(2,51,2),\
'n_estimators':range(50,401,50),\
'max_features':[None,'auto','log2']+range(10,61,5),\
'n_jobs':[-1]}}
else:
return {'DTC':{'criterion':['gini'],\
'max_features':[None,'auto','log2']+range(10,61,25)},\
'RFC':[{'criterion':['gini'],\
'max_features':[None,'auto','log2']+range(10,61,25),\
'n_jobs':[-1]}]}
_begining_report = """This experiment performed a Randomized Search for 1000 iterations upon the hyper parameters grid for both the \
Decision Tree classifier and the Random Forest classifier."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity - only tables
"""
_def_exp = DefaultParamsExperiment('Default_Params')
try:
self._load_prev_experiment(_def_exp)
except:
pass
tree_cross_scores = max(self._loaded_data['Tree'].grid_scores_,key= lambda x: x[1])[2]
forest_cross_scores = max(self._loaded_data['Forest'].grid_scores_,key= lambda x: x[1])[2]
def_tree_scores = _def_exp._loaded_data['Default_Tree']
def_forest_scores = _def_exp._loaded_data['Default_Forest']
def _evaluate(before,after):
prob , is_sig , is_better = self.t_test(before[0], after[0])
res = '\n'.join(['Paired T Test result between {before_name} and {after_name}: {proba:.5f}%'.format(before_name = before[1],after_name=after[1],proba=prob*100),\
'The results {sig} statically significant, while {before_name} is {better} with score {before_score:.4f}% than {after_name} with score {after_score:.4f}%'.format(before_name = before[1],after_name=after[1],prob=prob,before_score=before[0].mean()*100,
after_score=after[0].mean()*100,sig = 'are' if is_sig else "aren't",
better= 'better' if not is_better else 'worse')])
return res
trees_t_test = _evaluate((def_tree_scores,'Decision Tree before search'), (tree_cross_scores,'Decision Tree after search'))
forests_t_test = _evaluate((def_forest_scores,'Random Forest before search'), (forest_cross_scores,'Random Forest after search'))
tree_forest_test = _evaluate((tree_cross_scores,'Decision Tree after search'), (forest_cross_scores,'Random Forest after search'))
_res = '\n'.join(['Decision Tree before search accuracy score: {0:.4f}%'.format(def_tree_scores.mean()*100),\
'Decision Tree after search accuracy score: {0:.4f}%'.format(self._loaded_data['Tree'].best_score_*100),\
trees_t_test,'Best Decision Tree hyper parameters:\n'+str(self._loaded_data['Tree'].best_params_),
'Random Forest before search accuracy score: {0:.4f}%'.format(def_forest_scores.mean()*100),\
'Random Forest after search accuracy score: {0:.4f}%'.format(self._loaded_data['Forest'].best_score_*100),forests_t_test,\
'Best Random Forest hyper parameters:\n'+str(self._loaded_data['Forest'].best_params_),tree_forest_test])
return _res
@property
def _detail(self):
"""
Plots the Decision Tree classifier after search and after fitting all of the data.
Also plots to screen bar charts of results.
Saves in results folder, in pdf format.
"""
bayes_exp = BayesExperiment('Best_Params')
if not self._load_prev_experiment(bayes_exp):
print "Can not report - failed to load previous experiment"
return ''
def_exp = DefaultParamsExperiment('Default_Params')
if not self._load_prev_experiment(def_exp):
print "Can not report - failed to load previous experiment"
return ''
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import numpy as np
import matplotlib.pyplot as plt
N = 3
before_means = (bayes_exp._loaded_data['Bayes'].mean()*100,def_exp._loaded_data['Default_Tree'].mean()*100,\
def_exp._loaded_data['Default_Forest'].mean()*100)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
# rects1 = ax.bar(ind+width, before_means, width, color='r')
after_means = (bayes_exp._loaded_data['Bayes'].mean()*100,self._loaded_data['Tree'].best_score_*100,\
self._loaded_data['Forest'].best_score_*100)
rects2 = ax.bar(ind, after_means, width, color='b')
# add some text for labels, title and axes ticks
ax.set_ylabel('Accuracy Scores %')
ax.set_title('Scores of classifiers after hyperparameters calibration')
ax.set_xticks(ind + width)
ax.set_xticklabels(('Naive classifier', 'Naive Bayes', 'Decision Tree', 'Random Forest'))
# ax.legend((rects1[0], rects2[0]), ('Before Search', 'After Search'),loc=2)
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'',
ha='center', va='bottom')
# autolabel(rects1)
autolabel(rects2)
plt.show()
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
import pydot
_names = ['avg_AccCrosses_by_all_pos_by_all_HA',\
'avg_AccCrosses_by_all_pos_by_home',\
'avg_AccLB_by_all_pos_by_all_HA',\
'avg_AccLB_by_all_pos_by_home',\
'avg_AccThB_by_all_pos_by_all_HA',\
'avg_AccThB_by_all_pos_by_home',\
'avg_AerialsWon_by_all_pos_by_all_HA',\
'avg_AerialsWon_by_all_pos_by_home',\
'avg_BlockedShots_by_def_pos_by_all_HA',\
'avg_BlockedShots_by_def_pos_by_home',\
'avg_Clearances_by_def_pos_by_all_HA',\
'avg_Clearances_by_def_pos_by_home',\
'avg_Crosses_by_all_pos_by_all_HA',\
'avg_Crosses_by_all_pos_by_home',\
'avg_Disp_by_att_pos_by_all_HA',\
'avg_Disp_by_att_pos_by_home',\
'avg_Dribbles_by_att_pos_by_all_HA',\
'avg_Dribbles_by_att_pos_by_home',\
'avg_Fouled_by_att_pos_by_all_HA',\
'avg_Fouled_by_att_pos_by_home',\
'avg_Fouls_by_def_pos_by_all_HA',\
'avg_Fouls_by_def_pos_by_home',\
'avg_Interceptions_by_def_pos_by_all_HA',\
'avg_Interceptions_by_def_pos_by_home',\
'avg_KeyPasses_by_att_pos_by_all_HA',\
'avg_KeyPasses_by_att_pos_by_home',\
'avg_LB_by_all_pos_by_all_HA',\
'avg_LB_by_all_pos_by_home',\
'avg_Offsides_by_att_pos_by_all_HA',\
'avg_Offsides_by_att_pos_by_home',\
'avg_PA%_by_all_pos_by_all_HA',\
'avg_PA%_by_all_pos_by_home',\
'avg_Passes_by_all_pos_by_all_HA',\
'avg_Passes_by_all_pos_by_home',\
'avg_ShotsOT_by_att_pos_by_all_HA',\
'avg_ShotsOT_by_att_pos_by_home',\
'avg_Shots_by_att_pos_by_all_HA',\
'avg_Shots_by_att_pos_by_home',\
'avg_ThB_by_all_pos_by_all_HA',\
'avg_ThB_by_all_pos_by_home',\
'avg_TotalTackles_by_def_pos_by_all_HA',\
'avg_TotalTackles_by_def_pos_by_home',\
'avg_Touches_by_all_pos_by_all_HA',\
'avg_Touches_by_all_pos_by_home',\
'avg_UnsTouches_by_att_pos_by_all_HA',\
'avg_UnsTouches_by_att_pos_by_home',\
'avg_Goals_by_fix_by_all_HA',\
'avg_Goals_by_fix_by_all_HA_specific',\
'avg_Goals_by_fix_by_home',\
'avg_Goals_by_fix_by_home_specific',\
'avg_Possession_rate_by_all_HA',\
'avg_Possession_rate_by_all_HA_specific',\
'avg_Possession_rate_by_home',\
'avg_Possession_rate_by_home_specific',\
'avg_Success_rate_by_all_HA',\
'avg_Success_rate_by_all_HA_specific',\
'avg_Success_rate_by_home',\
'avg_Success_rate_by_home_specific',\
'avg_received_Goals_by_fix_by_all_HA',\
'avg_received_Goals_by_fix_by_all_HA_specific',\
'avg_received_Goals_by_fix_by_home',\
'avg_received_Goals_by_fix_by_home_specific',\
'relative_all_pos',\
'relative_att_pos',\
'relative_def_pos']
dot_data = StringIO()
export_graphviz(self._loaded_data['Tree'].best_estimator_, out_file=dot_data,rounded=True,class_names=['Win (Home)','Draw','Win (Away)'],\
feature_names=_names)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf(join_path(self.results_dir,"best_tree.pdf"))
return 'The plotted Decision Tree classifier is saved in {0}.'.format(os.path.abspath(join_path(self.results_dir,"best_tree.pdf")))
def run(self):
"""
Runs a RandomizedSearch on both DecisionTree and RandomForest classifiers.
"""
Experiment.run(self)
_grids = self.load_params()
grid_tree = RandomizedSearchCV(DTC(), _grids['DTC'], n_jobs=-1, cv=self.cv.leagues_cross_validation,n_iter=1000)
grid_tree.fit(self.cv.complete_examples,self.cv.complete_tags)
grid_forest = RandomizedSearchCV(RFC(), _grids['RFC'], n_jobs=-1, cv=self.cv.leagues_cross_validation,n_iter=1000)
grid_forest.fit(self.cv.complete_examples,self.cv.complete_tags)
self._loaded_data = {'Tree':grid_tree,'Forest':grid_forest}
self.save(self._loaded_data)
class BayesExperiment(Experiment):
"""
An experiment to test Naive Bayes classifier.
"""
def __init__(self,dir_name,test=False):
Experiment.__init__(self,dir_name,test)
self.name = 'Bayes'
def run(self):
"""
Runs a cross validation on Naive Bayes Classfier.
"""
Experiment.run(self)
bayes_score = cross_val_score(GaussianNB(), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
self._loaded_data = {'Bayes':bayes_score}
self.save(self._loaded_data)
_begining_report = """This experiment tried a Naive Bayes classifier."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity - only results.
"""
return '\n'.join(['Gaussian Naive Bayes accuracy score: {0:.4f}%'.format(self._loaded_data['Bayes'].mean()*100)])
class OneVsRestExperiment(Experiment):
"""
An experiment to test Naive Bayes classifier.
"""
def __init__(self,dir_name,test=False):
Experiment.__init__(self,dir_name,test)
self.name = 'OneVsRest'
def load_params(self):
"""
The load_params for this experiment loads the best params for the decision tree and random forest and also the best lookback.
This params found by the experiment best_param, lookback by best_lookback.
"""
best_param_exp = BestParamsExperiment("Best_Params", self._test)
if not self._load_prev_experiment(best_param_exp): return False
best_lookback_exp = BestLookbackExperimet("Best_Params", self._test)
if not self._load_prev_experiment(best_lookback_exp): return False
self.estimators_params = {'DTC':best_param_exp._loaded_data['Tree'].best_params_,'RFC':best_param_exp._loaded_data['Forest'].best_params_,\
'Lookback':int(max([(best_lookback_exp._loaded_data[_lk][1].mean(),_lk) for _lk in best_lookback_exp._loaded_data])[1])}
return True
def get_data(self):
"""
Loads all the examples and tags needed for the experiment - building the examples based on the lookback found in previous experiments.
Loads the all of the examples and tags, and also creates cross validation for the classifiers..
"""
self.cv = CrossValidation(test=self._test,remote=False)
lookback = self.estimators_params['Lookback']
self.cv.load_data(lookback)
self.X = self.cv.complete_examples
self.y = self.cv.complete_tags
@timed
def run(self):
if not self.load_params():
print 'Can not run- must load previous experiment'
return
Experiment.run(self)
ovr_tree_score = cross_val_score(OVRC(DTC(**self.estimators_params['DTC']),-1),self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
ovr_forest_score = cross_val_score(OVRC(RFC(**self.estimators_params['RFC']),-1),self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
self._loaded_data = {'OvR Tree':ovr_tree_score,'OvR Forest':ovr_forest_score}
self.save(self._loaded_data)
_begining_report = """This experiment tried a OneVsRest classifier with both Decision Tree and Random Forest as base estimators."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity - only results.
"""
best_exp = BestParamsExperiment('Best_Params')
try:
self._load_prev_experiment(best_exp)
except:
pass
tree_cross_scores = max(best_exp._loaded_data['Tree'].grid_scores_,key= lambda x: x[1])[2]
forest_cross_scores = max(best_exp._loaded_data['Forest'].grid_scores_,key= lambda x: x[1])[2]
def _evaluate(before,after):
prob , is_sig , is_better = self.t_test(before[0], after[0])
res = '\n'.join(['Paired T Test result between {before_name} and {after_name}: {proba:.5f}%'.format(before_name = before[1],after_name=after[1],proba=prob*100),\
'The results {sig} statically significant, while {before_name} is {better} with score {before_score:.4f}% than {after_name} with score {after_score:.4f}%'.format(before_name = before[1],after_name=after[1],prob=prob,before_score=before[0].mean()*100,
after_score=after[0].mean()*100,sig = 'are' if is_sig else "aren't",
better= 'better' if not is_better else 'worse')])
return res
trees_t_test = _evaluate((tree_cross_scores,'Decision Tree before using OneVsRest'), (self._loaded_data['OvR Tree'],'Decision Tree after using OneVsRest'))
forests_t_test = _evaluate((forest_cross_scores,'Random Forest before using OneVsRest'), (self._loaded_data['OvR Forest'],'Random Forest after using OneVsRest'))
return '\n'.join(['One Vs Rest with Decision Tree accuracy score: {0:.4f}%'.format(self._loaded_data['OvR Tree'].mean()*100),\
trees_t_test,\
'One Vs Rest with Random Forest accuracy score: {0:.4f}%'.format(self._loaded_data['OvR Forest'].mean()*100),forests_t_test])
class DefaultParamsExperiment(Experiment):
"""
An experiment to test Decision Tree and Random Forest classifiers without adjusting their respective hyper parameters, as well as a random choosing classifier as a baseline for scores.
"""
def __init__(self,dir_name,test=False):
Experiment.__init__(self,dir_name,test)
self.name = 'Default_Params'
def run(self):
"""
Runs cross validation against Decision Tree and Random Forest classifiers, and against a random choosing classifier.
"""
Experiment.run(self)
tree_score = cross_val_score(DTC(), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
forest_score = cross_val_score(RFC(), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
random_score = cross_val_score(DummyClassifier('uniform'), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
self._loaded_data = {'Default_Tree':tree_score,'Default_Forest':forest_score,'Random':random_score}
self.save(self._loaded_data)
_begining_report = """This experiment tried both the Decision Tree and the Random Forest classifiers with default hyper parameters, as well as a random choosing classifier as a base line."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity - only results.
"""
return '\n'.join(['Decision Tree with default hyper parameters accuracy score: {0:.4f}%'.format(self._loaded_data['Default_Tree'].mean()*100),\
'Random Forest with default hyper parameters accuracy score: {0:.4f}%'.format(self._loaded_data['Default_Forest'].mean()*100),\
'Random classifier accuracy score: {0:.4f}%'.format(self._loaded_data['Random'].mean()*100)])
@property
def _detail(self):
"""
Reporting on medium verbosity - plot bar charts of results
"""
bayes_exp = BayesExperiment('Best_Params')
if not self._load_prev_experiment(bayes_exp):
print "Can not report - failed to load previous experiment"
return ''
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import numpy as np
import matplotlib.pyplot as plt
N = 4
means = (self._loaded_data['Random'].mean()*100, bayes_exp._loaded_data['Bayes'].mean()*100,self._loaded_data['Default_Tree'].mean()*100,\
self._loaded_data['Default_Forest'].mean()*100)
ind = np.arange(N) # the x locations for the groups
width = 0.45 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, means, width, color='b')
# add some text for labels, title and axes ticks
ax.set_ylabel('Accuracy Scores %')
ax.set_title('Scores of baseline classifiers')
ax.set_xticks(ind + width)
ax.set_xticklabels(('Naive classifier', 'Naive Bayes', 'Decision Tree', 'Random Forest'))
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'',
ha='center', va='bottom')
autolabel(rects1)
plt.show()
return ''
class LearningCurveExperiment(Experiment):
"""
An experiment to test the learning curves of the classifiers (Decision Tree, Random Forest, Naive Bayes).
The learning curve shows the classifier's accuracy correlation with the size of the training data.
"""
def __init__(self,dir_name,test=False):
Experiment.__init__(self,dir_name,test)
self.name = 'Learning_Curve'
def run(self):
"""
Runs learning curve on all classifiers.
"""
Experiment.run(self)
best_param_exp = BestParamsExperiment(self._dir_name, self._test)
self._load_prev_experiment(best_param_exp)
tree_curve = learning_curve(DTC(**best_param_exp._loaded_data['Tree'].best_params_), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
forest_curve = learning_curve(RFC(**best_param_exp._loaded_data['Forest'].best_params_), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
bayes_curve = learning_curve(GaussianNB(), self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
self._loaded_data = {'Tree_Curve':tree_curve,'Forest_Curve':forest_curve,'Bayes_Curve':bayes_curve}
self.save(self._loaded_data)
_begining_report = """This experiment checks the learning curve for all the classifiers. \n
Will plot the learning curves on screen."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity- plots the learning curves
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import numpy as np
import matplotlib.pyplot as plt
def plot_learning_curve(title, ylim=None,_type='Tree'):
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = self._loaded_data[_type]
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.legend(loc="best")
return plt
plot_learning_curve("Learning Curves (Decision Tree)",_type='Tree_Curve')
plot_learning_curve("Learning Curves (Random Forest)",_type='Forest_Curve')
plot_learning_curve("Learning Curves (Naive Bayes)",_type='Bayes_Curve')
plt.show()
return ''
class BestLookbackExperimet(Experiment):
"""
An experiment to test what is thebest lookback for creating the examples.
The lookback parameter defines how many previous games we are taking in consideration while generating the examples.
"""
def __init__(self, dir_name, test=False):
Experiment.__init__(self, dir_name, test=test)
self.name = 'Best_Lookback'
def load_params(self,estimators=[]):
"""
The load_params for this experiment loads the best params for the decision tree and random forest.
This params found by the experiment best_param.
"""
best_param_exp = BestParamsExperiment(self._dir_name, self._test)
if not self._load_prev_experiment(best_param_exp): return False
self.estimators = [DTC(**best_param_exp._loaded_data['Tree'].best_params_),\
RFC(**best_param_exp._loaded_data['Forest'].best_params_)]
return True
def get_data(self,lookback):
self.cv = CrossValidation(test=self._test,remote=self._remote)
self.cv.load_data(lookback)
self.X = self.cv.complete_examples
self.y = self.cv.complete_tags
def run(self):
"""
For each fixture in range [1,66] (in jumps of 5), run a cross validation on both Decision Tree and Random Forest.
"""
if self._test:
self.ranges = [15,30]
else:
self.ranges = range(1,70,5)
self.load_params()
results = {str(i):0 for i in self.ranges}
self._remote = True
for lookback in self.ranges:
self.get_data(lookback)
self._remote = False
dtc_score = cross_val_score(self.estimators[0], self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
rfc_score = cross_val_score(self.estimators[1], self.X, self.y, cv=self.cv.leagues_cross_validation,n_jobs=-1)
results[str(lookback)] = (dtc_score,rfc_score)
self._loaded_data = results
self.save(self._loaded_data)
_begining_report = """This experiment checks both classifier's accuracy correlation with the lookback parameter \
for the creation on the examples."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity - only tables
"""
_dtc_scores = {int(_lk):self._loaded_data[_lk][0].mean()*100 for _lk in self._loaded_data}
_rfc_scores = {int(_lk):self._loaded_data[_lk][1].mean()*100 for _lk in self._loaded_data}
_table = tabulate([['Decision Tree']+[value for (key, value) in sorted(_dtc_scores.items())],\
['Random Forest']+[value for (key, value) in sorted(_rfc_scores.items())]],\
headers=['Classifier / Lookback']+sorted(_dtc_scores),tablefmt="fancy_grid",floatfmt=".4f")
return 'Cross validation scores for each classifier by lookback:\n%s\n'%_table
@property
def _detail(self):
"""
Medium verbosity - show plotted graphs
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import numpy as np
import matplotlib.pyplot as plt
def plot_lookback_curve(title, ylim=None):
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Number of fixtures to look back")
plt.ylabel("Score")
_dtc_scores = {int(_lk):self._loaded_data[_lk][0] for _lk in self._loaded_data}
_rfc_scores = {int(_lk):self._loaded_data[_lk][1] for _lk in self._loaded_data}
train_sizes = sorted(_dtc_scores.keys())
train_scores = [value for (_, value) in sorted(_dtc_scores.items())]
test_scores = [value for (_, value) in sorted(_rfc_scores.items())]
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Decision Tree score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Random Forest score")
plt.legend(loc="best")
return plt
plot_lookback_curve('Best Look Back')
plt.show()
return ''
class BestProbaForDecision(Experiment):
"""
An experiment to test what is the best threshold from which we want to make the decision.
"""
def __init__(self, dir_name, test=False):
Experiment.__init__(self, dir_name, test=test)
self.name = 'Best_Proba'
def load_params(self):
"""
The load_params for this experiment loads the best params for the decision tree and random forest and also the best lookback.
This params found by the experiment best_param, lookback by best_lookback.
"""
best_param_exp = BestParamsExperiment("Best_Params", self._test)
if not self._load_prev_experiment(best_param_exp): return False
best_lookback_exp = BestLookbackExperimet("Best_Params", self._test)
if not self._load_prev_experiment(best_lookback_exp): return False
self.estimators_params = {'DTC':best_param_exp._loaded_data['Tree'].best_params_,'RFC':best_param_exp._loaded_data['Forest'].best_params_,\
'Lookback':int(max([(best_lookback_exp._loaded_data[_lk][1].mean(),_lk) for _lk in best_lookback_exp._loaded_data])[1])}
return True
def get_data(self):
"""
Loads all the examples and tags needed for the experiment - building the examples based on the lookback found in previous experiments.
Loads the all of the examples and tags, and also creates cross validation for the classifiers..
"""
self.cv = CrossValidation(test=self._test)
lookback = self.estimators_params['Lookback']
self.cv.load_data(lookback)
self.X = self.cv.complete_examples
self.y = self.cv.complete_tags
def run(self):
"""
For each probability p in [0.34,0.65] (in jumps of 0.01), make the decision only if classifier's probability is greater or equal to p.
For each probability we calculate the amount of games that qulified and the score will be calculated by this amount.
"""
if not self.load_params():
print 'Can not run- must load previous experiment'
return
Experiment.run(self)
if self._test:
self.ranges = [0.34,0.35]
else:
self.ranges = [(float(_i)/100) for _i in range(34,66)]
self._loaded_data = {}
self._loaded_data['DTC'] = {k:(0,0) for k in self.ranges}
self._loaded_data['RFC'] = {k:(0,0) for k in self.ranges}
for _range in self.ranges:
dt_decision_result = 0.0
dt_score = 0
dt_curr_decisions = 0
rf_decision_result = 0.0
rf_score = 0
rf_curr_decisions = 0
tot_games = 0
for train , test in self.cv._leagues_cross_validation():
clf_dt = DTC(**self.estimators_params['DTC'])
clf_dt = clf_dt.fit(train[0],train[1])
clf_rf = RFC(**self.estimators_params['RFC'])
clf_rf = clf_rf.fit(train[0],train[1])
dt_res_tags = clf_dt.predict(test[0])
dt_res_proba = clf_dt.predict_proba(test[0])
rf_res_tags = clf_rf.predict(test[0])
rf_res_proba = clf_rf.predict_proba(test[0])
for i in range(len(dt_res_tags)):
tot_games += 1
if max(dt_res_proba[i]) >= _range:
dt_curr_decisions += 1
if dt_res_tags[i] == test[1][i]:
dt_score += 1
for i in range(len(rf_res_tags)):
if max(rf_res_proba[i]) >= _range:
rf_curr_decisions += 1
if rf_res_tags[i] == test[1][i]:
rf_score += 1
dt_decision_result = (dt_score*1.0)/dt_curr_decisions
rf_decision_result = (rf_score*1.0)/rf_curr_decisions
self._loaded_data['DTC'][_range] = (dt_curr_decisions,dt_decision_result)
self._loaded_data['RFC'][_range] = (rf_curr_decisions,rf_decision_result)
self._loaded_data["AG"] = tot_games
self.save(self._loaded_data)
_begining_report = """This experiment checks the best probability given by the Decision Tree from which \
we start making the decisions."""
_ending_report = ""
@property
def _no_detail(self):
"""
Reporting on low verbosity - only tables
"""
_proba_scores = {float(_k):(self._loaded_data['DTC'][_k],self._loaded_data['RFC'][_k]) for _k in self._loaded_data['DTC'].keys()}
_inner_table = [[key,tup[0][0],tup[0][1]*100,(float(tup[0][0])/self._loaded_data["AG"])*tup[0][1],tup[1][0],tup[1][1]*100,(float(tup[1][0])/self._loaded_data["AG"])*tup[1][1]] for (key, tup) in sorted(_proba_scores.items())]
_table = tabulate([data for data in _inner_table],\
headers=['Probability','#Examples Tree','Score Tree','AS DT','#Examples Forest','Score Forest','AS RF'],tablefmt="fancy_grid",floatfmt=".4f")
return 'Results :\n%s\n'%_table
@property
def _detail(self):
"""
Medium verbosity - show plotted graphs
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import matplotlib.pyplot as plt
def plot_lookback_curve(title, ylim=None,_dtc_scores=[],_rfc_scores=[],kwargs={}):
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel(kwargs['xlabel'])
plt.ylabel(kwargs['ylabel'])
tree_train_sizes = sorted(_dtc_scores.keys())
forest_train_sizes = sorted(_rfc_scores.keys())
train_scores_mean = [value for (_, value) in sorted(_dtc_scores.items())]
test_scores_mean = [value for (_, value) in sorted(_rfc_scores.items())]
plt.grid()
plt.plot(tree_train_sizes, train_scores_mean, 'o-', color="r",
label=kwargs['tree'])
plt.plot(forest_train_sizes, test_scores_mean, 'o-', color="g",
label='forest')
plt.legend(loc="best")
return plt
_dtc_scores = {float(_k):self._loaded_data['DTC'][_k][0] for _k in self._loaded_data['DTC']}
_rfc_scores = {float(_k):self._loaded_data['RFC'][_k][0] for _k in self._loaded_data['RFC']}
kwargs = {'xlabel':'Probability','ylabel':'#Examples who has a class with greater probability',\
'tree':'Decision Tree Classifier','forest':'Random Forest Classifer'}
plot_lookback_curve("Number of examples by probability",None,_dtc_scores,_rfc_scores,kwargs)
_dtc_scores = {self._loaded_data['DTC'][_k][0]:self._loaded_data['DTC'][_k][1] for _k in self._loaded_data['DTC']}
_rfc_scores = {self._loaded_data['RFC'][_k][0]:self._loaded_data['RFC'][_k][1] for _k in self._loaded_data['RFC']}
kwargs = {'xlabel':'Number of examples','ylabel':'Mean score',\
'tree':'Decision Tree Classifier','forest':'Random Forest Classifer'}
plot_lookback_curve("Mean score by number of examples",None,_dtc_scores,_rfc_scores,kwargs)
plt.show()
return ''
class BestProbaDiffForDrawDecision(Experiment):
"""
An experiment to test what is the best threshold from which we want to make the decision about a draw.
"""
def __init__(self, dir_name, test=False):
Experiment.__init__(self, dir_name, test=test)
self.name = 'Best_Proba_Draw'
def load_params(self):
"""
The load_params for this experiment loads the best params for the decision tree and random forest and also the best lookback.
This params found by the experiment best_param, lookback by best_lookback.
"""
best_param_exp = BestParamsExperiment("Best_Params", self._test)
if not self._load_prev_experiment(best_param_exp): return False
best_lookback_exp = BestLookbackExperimet("Best_Params", self._test)
if not self._load_prev_experiment(best_lookback_exp): return False
best_proba_exp = BestProbaForDecision("Best_Proba", self._test)
if not self._load_prev_experiment(best_proba_exp): return False
self.estimators_params = {'DTC':best_param_exp._loaded_data['Tree'].best_params_,'RFC':best_param_exp._loaded_data['Forest'].best_params_,\
'Lookback':int(max([(best_lookback_exp._loaded_data[_lk][1].mean(),_lk) for _lk in best_lookback_exp._loaded_data])[1]),\
'Best_Proba':0.65}
return True
def get_data(self):
"""
Loads all the examples and tags needed for the experiment - building the examples based on the lookback found in previous experiments.
Loads the all of the examples and tags, and also creates cross validation for the classifiers..
"""
self.cv = CrossValidation(test=self._test)
lookback = self.estimators_params['Lookback']
self.cv.load_data(lookback)
self.X = self.cv.complete_examples
self.y = self.cv.complete_tags
def run(self):
"""
Using the predict_proba methods of the classifiers, we wish to test a new decision rule that will allow us to tag games as draw.