Merge branch 'master' of ssh://repo.or.cz/srv/git/gostyle
[gostyle.git] / pca.py
blobd847f6bea6123ddf3f2dad0580e4a34225dd14eb
1 #!/usr/bin/python
2 """
3 This code creates input vectors and performs PCA on it. Each pca'd vector is then printed along with
4 the player name, suitable e.g. to plot using gnuplot.
5 """
6 # not true currently
7 """
8 OUTPUT FORMAT
9 player_name first_principal_component_of_player's_input_vector second_principal_component ...
10 second_player_name ...
11 ...
12 """
13 import sys
14 from gostyle import print_vector, OccurenceVectorGenerator, Rescale, PlayerStrategyIdentificator, PCA, InputVectorGenerator
15 from itertools import izip, count
16 from data_about_players import Data
18 if __name__ == '__main__':
19 main_pat_filename = Data.main_pat_filename
20 num_features = 500
21 filename_pca = 'pca.data'
22 players_ignore = [ 'Cho Tae-hyeon', 'Shao Zhenzhong', 'Wu Songsheng', 'Honinbo Shusaku', 'Kuwahara Shusaku', 'Yasuda Shusaku', 'Go Seigen', 'Suzuki Goro', 'Jie Li' ] #, 'Cho Chikun', 'Takemiya Masaki']
23 players_all = Data.players_all
24 players = [ p for p in players_all if p not in players_ignore ]
25 #players = Data.player_vector.keys()
27 ### Objects creating input and output vectors when called
28 print >>sys.stderr, "Creating input vector generator from main pat file:", main_pat_filename
29 ivg = InputVectorGenerator(main_pat_filename, num_features)
31 # Create pairs of (input vector, player name)
32 input_vectors = []
33 for name in players:
34 #input_vectors += [ivg( Data.pat_files_folder + name)]
35 input_vectors += [[float(occ) for occ in ivg(Data.pat_files_folder + name)]]
37 if len(input_vectors) == 0:
38 print >>sys.stderr, "No input vectors.", main_pat_filename
39 sys.exit()
41 # Create PCA object, trained on input_vectors
42 pca = PCA(input_vectors, output_dim=10)
43 #pca = PCA(input_vectors, reduce=True)
45 # Perform a PCA on input vectors
46 input_vectors = pca.process_list_of_vectors(input_vectors)
48 ### Now we rescale vectors, so that each component fits on -1.0 to 1.0
49 ### this makes a very nice plot!!
50 r = Rescale(-1.0,1.0)
52 ### Normalize each component separately
53 # We need to transpose input_vectors - a list of per-player-vector-of-pca-component
54 # to get list of vectors of per-component-vector-of-player-data
55 def transpose(list_of_vectors):
56 return zip(*list_of_vectors)
57 input_vectors = transpose([ r(vector) for vector in transpose(input_vectors)])
60 print >> sys.stderr, "Writing output to file: ", filename_pca
61 # prints vectors along with player names
62 f=open(filename_pca, 'w')
63 for name,vector in izip(players, input_vectors):
64 # Substitute ' ' by '_' to allow for gnuplot plotting (recognizing columns correctly)
65 name_to_print = '_'.join(name.split())
66 for p, i in izip(vector, count()):
67 print >> f, name_to_print, i+1, p
68 f.close()
70 print "\nProjection info:"
71 P = pca.get_projection_info()
72 for y in xrange(1, P.shape[0]):
73 for x in xrange(1, P.shape[1]):
74 print y, x, P[y,x], ivg.ovg.stringof(x)
75 #print P
77 print >> sys.stderr, "\nNow print that by:"
78 print >> sys.stderr, 'gnuplot> set xrange[1:%d]'%(pca.pca.output_dim+1)
79 print >> sys.stderr, 'plot "%s" using 2:3:1 with labels font "arial,10" left point pt 4 offset 1,0'%(filename_pca,)