5 from itertools
import izip
8 from data_about_players
import Data
10 if __name__
== '__main__':
11 root_dir
= '../pdb-gtl/'
12 main_pat_filename
= root_dir
+ 'all.pat'
13 player_vector
= Data
.strength_linear_vector
16 ### Object creating input vector when called
17 print >>sys
.stderr
, "Creating input vector generator from main pat file:", main_pat_filename
18 i
= InputVectorGenerator(main_pat_filename
, num_features
)#, rescale=LogRescale)
20 #raw = root_dir + 'testpat_files'
22 import os
, random
, shutil
23 ranks
= os
.listdir(raw
)
26 plays
= os
.listdir(raw
+ '/'+ rank
)
28 tot
[raw
+ '/' + rank
+ '/' + play
] = rank
31 train_set_dir
= root_dir
+ 'train_set'
32 test_set_dir
= root_dir
+ 'rawpat_files_merged_test'
33 train_dict
= list_dir(train_set_dir
)
34 test_dict
= list_dir(test_set_dir
)
37 input_vectors_train
= []
38 for f
, rank
in train_dict
.items():
40 input_vectors_train
+= [i(f
)]
45 input_vectors_test
= []
48 for f
, rank
in test_dict
.items():
50 input_vectors_test
+= [i(f
)]
58 #if len(input_vectors_train) == 0:
59 # print >>sys.stderr, "No reference vectors."
61 if len(input_vectors_test
) == 0:
62 print >>sys
.stderr
, "No vectors to process."
66 # Change this to False, if you do not want to use PCA
69 # Create PCA object, trained on input_vectors
70 print >>sys
.stderr
, "Running PCA."
71 pca
= PCA(input_vectors_train
+ input_vectors_test
, reduce=True)
72 # Perform a PCA on input vectors
73 if input_vectors_train
:
74 input_vectors_train
= pca
.process_list_of_vectors(input_vectors_train
)
75 if input_vectors_test
:
76 input_vectors_test
= pca
.process_list_of_vectors(input_vectors_test
)
77 # Creates a Composed object that first generates an input vector
78 # and then performs a PCA analysis on it.
81 ### Object creating output vector when called;
83 for name
, input_vector
in zip(train_pl
, input_vectors_train
):
84 ref_dict
[tuple(input_vector
)] = player_vector
[name
]
86 print "creating the knn"
88 oknn
= KNNOutputVectorGenerator(ref_dict
, k
=4, weight_param
=0.9, dist_mult
=6.6)
90 #oknn = KNNOutputVectorGenerator(ref_dict, k=5, weight_param=0.2, dist_mult=10)
93 return [ (1-x
) * 16.5 - 3.0 for x
in vec
]
96 return list(2.0*numpy
.random
.random(k
)-1.0)
99 # Create list of output vectors using weighted kNN algorithm approximating output_vector
100 #output_vectors= [ revnorm(oknn(input_vector)) for input_vector in input_vectors_test ]
101 output_vectors
= [ revnorm(rand_vect(1)) for _
in input_vectors_test
]
102 desired_vectors
= [ revnorm(player_vector
[rank
]) for rank
in test_pl
]
105 for f
, out
, des
in zip(test_files
, output_vectors
, desired_vectors
):
108 print f
, "%2.3f ; %2.3f"%(out
[0], des
[0])
111 diff
= [ abs(x
[0] - y
[0]) for x
,y
in zip(output_vectors
,desired_vectors
) ]
112 zips
= zip(diff
, test_files
)
115 print a
, " %2.3f"%(diff
,)
118 for o
,d
in zip(output_vectors
, desired_vectors
):
125 mean
= numpy
.array(errs
).mean()
126 print "Mean square err: " + "%2.3f ( = sd %2.3f) "%(mean
, sqrt(mean
))