Merge branch 'master' of ssh://repo.or.cz/srv/git/gostyle
[gostyle.git] / cross_validation_strength.py
bloba55d998dff03f6f755ff283a0b1e2aeb6633d63a
1 #!/usr/bin/python
2 import sys
3 import subprocess
4 import os
5 from gostyle import *
6 from math import sqrt
7 import numpy
9 from data_about_players import Data
11 from knn import KNNOutputVectorGenerator
13 class NeuralNet:
14 def __init__( self, filename ):
15 self.null = open('/dev/null','w')
17 s = "./gnet/gnet_train -l 3 -n 30 -p 30 -e 0.00005 -o gonet.net ./"+filename
18 args = s.split()
19 ret = subprocess.call(args)#,stdout=self.null)
20 s = "./gnet/gnet_run gonet.net"
21 args = s.split()
22 self.p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.null)
23 def __call__(self, vector):
24 self.p.stdin.write(' '.join([str(a) for a in vector]) + '\n')
25 a = self.p.stdout.readline()
26 return [ float(num) for num in a.split()]
27 def close(self):
28 self.p.stdin.close()
29 self.p.stdout.close()
30 self.null.close()
32 if __name__ == '__main__':
33 main_pat_filename = Data.main_pat_filename
34 num_features = 400
36 # Neural net
37 typ = 'knn'
38 #typ = 'knn'
39 # random
40 #typ = 'rnd'
41 #typ = 'joint_nn_knn'
43 player_vector = Data.strength_linear_vector
44 # players_ignore = [ "Yi Ch'ang-ho 2004-" ]#, "Fujisawa Hideyuki","Yuki Satoshi", "Otake Hideo", "Yi Ch'ang-ho 2005+","Takao Shinji","Hane Naoki","Kobayashi Koichi" ]
45 players_ignore = [ ]#,"Takao Shinji","Hane Naoki","Kobayashi Koichi" ]
46 players_all = [ p for p in player_vector.keys() if p not in players_ignore ]
48 ### Object creating input vector when called
49 print "Creating input vector generator from main pat file:", main_pat_filename
50 i = InputVectorGenerator(main_pat_filename, num_features)
52 # Create list of input vectors
53 input_vectors = []
54 for name in players_all:
55 input_vectors += [i(Data.pat_files_folder + name)]
57 #print '"%s"'%(players_all[2],)
58 #print input_vectors[2]
60 if len(input_vectors) == 0:
61 print >>sys.stderr, "No reference vectors."
62 sys.exit()
64 ### PCA example usage
65 # Change this to False, if you do not want to use PCA
66 use_pca = True
67 if use_pca:
68 # Create PCA object, trained on input_vectors
69 print >>sys.stderr, "Running PCA."
70 pca = PCA(input_vectors, reduce=True)
71 # Perform a PCA on input vectors
72 input_vectors = pca.process_list_of_vectors(input_vectors)
73 # Creates a Composed object that first generates an input vector
74 # and then performs a PCA analysis on it.
75 i = Compose(i, pca)
77 ### n/4-fold cross validation
78 #bounds = random.sample(range(1,len(players_all)), len(players_all) / 10 )
79 bounds=[]
80 for x in range(1,len(players_all)/4):
81 bounds += [4*x for _ in [1] if 4*x < len(players_all)]
82 if not bounds:
83 print >>sys.stderr, "Pop too small."
84 sys.exit()
85 bounds.sort()
87 def rand_vect(k):
88 return list(2.0*numpy.random.random(k)-1.0)
90 print >>sys.stderr, "Running Cross-validation."
91 print
92 errs=[ [] for _ in xrange(len(players_all)) ]
93 es=[]
94 esps=[[],[],[],[]]
95 sentinel=len(players_all)
96 number_runs = 20
97 for _ in xrange(number_runs):
98 pairs = zip(players_all, input_vectors)
99 random.shuffle(pairs)
100 players_all = [ a for a, b in pairs ]
101 input_vectors = [ b for a, b in pairs ]
102 prev=0
103 for b in bounds+[sentinel]:
104 validation_set = range(prev, b)
105 reference_set = range(0,prev) + range(b,sentinel)
106 if False:
107 print "Reference set :",
108 for pr in range(0, prev):
109 print "R",
110 for pr in validation_set:
111 print "_",
112 for pr in range(b, sentinel):
113 print "R",
114 print
115 prev = b
116 if typ == 'nn':
117 data =[]
118 for index in reference_set:
119 data.append( (input_vectors[index], player_vector[players_all[index]]) )
122 ### We can enlarge the data set by adding linear combinations of input and output vectors
123 use_lin_combinations = False
124 if use_lin_combinations:
125 data += Combinator().combine(data)
127 print_set_to_file(data,'nn_cross.data')
129 nn = NeuralNet('nn_cross.data')
130 # Create list of output vectors using weighted kNN algorithm approximating output_vector
131 output_vectors = [ nn(input_vectors[index]) for index in validation_set ]
132 nn.close()
133 elif typ == 'knn':
134 ### Object creating output vector when called;
135 ref_dict = {}
136 for index in reference_set:
137 ref_dict[tuple(input_vectors[index])] = player_vector[players_all[index]]
140 # best pro InputVectorGenerator rescale=Rescale
141 oknn = KNNOutputVectorGenerator(ref_dict, k=4, weight_param=0.91, dist_mult=21)
142 #oknn = KNNOutputVectorGenerator(ref_dict, k=3, weight_param=0.99, dist_mult=1400)
144 # Create list of output vectors using weighted kNN algorithm approximating output_vector
145 output_vectors = [ oknn(input_vectors[index]) for index in validation_set ]
146 elif typ == 'rnd':
147 output_vectors = [ rand_vect(4) for index in validation_set ]
149 desired_vectors = [ player_vector[players_all[index]] for index in validation_set ]
151 if False:
152 for vec_set,text in [(output_vectors, "Output: "), (desired_vectors, "Desired:")]:
153 print text,
154 for o in vec_set:
155 for x in o:
156 print "%02.3f"%(x,),
157 print "; ",
158 print
160 for num1, (o,d) in zip(validation_set, zip(output_vectors, desired_vectors)):
161 err = 0.0
162 for num,(x,y) in enumerate(zip(o,d)):
163 e = (1.0*x-1.0*y)**2
164 esps[num]+=[e]
165 es += [e]
166 err += e
167 errs[num1] += [err]
169 if typ == 'joint_nn_knn':
170 print "Joint classifier:"
171 elif typ == 'knn':
172 print "k-NN classifier:"
173 elif typ == 'nn':
174 print "Neural network classifier:"
175 elif typ == 'rnd':
176 print "Random classifier:"
177 #print "Total square err: %2.3f"%( sum(errs) / number_runs,)
178 # mar = numpy.array(errs)
179 # mean = mar.mean()
180 # print "Mean square err per player: " + "%2.3f ( = sd %2.3f) "%(mean, sqrt(mean))
181 mean = numpy.array(es).mean()
182 print "Mean square err per style: " + "%2.3f ( = sd %2.3f) "%(mean, sqrt(mean))
184 #mean = numpy.array(es).mean()
185 #print "%2.3f &"%(mean),
186 #print "%2.3f \\\\\\hline"%(11.776 / mean)
188 #print
189 #print "Players sorted by mean square error:"
190 #p = zip([numpy.array(errs[p]).mean() for p in xrange(len(players_all)) ], players_all)
191 #p.sort()
192 #for err, name in p:
193 # print "%2.3f %s"%(err,name)
194 # #print "%s"%(name,)
195 # sys.exit()