PCA: Print also projection information on the output
[gostyle.git] / pca.py
blob189c582ac0a8f1be593566ba8110e801d2e93afa
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 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']
22 players_all = Data.players_all
23 players = [ p for p in players_all if p not in players_ignore ]
24 #players = Data.player_vector.keys()
26 ### Objects creating input and output vectors when called
27 print >>sys.stderr, "Creating input vector generator from main pat file:", main_pat_filename
28 ivg = InputVectorGenerator(main_pat_filename, num_features)
30 # Create pairs of (input vector, player name)
31 input_vectors = []
32 for name in players:
33 #input_vectors += [ivg( Data.pat_files_folder + name)]
34 input_vectors += [[float(occ) for occ in ivg(Data.pat_files_folder + name)]]
36 if len(input_vectors) == 0:
37 print >>sys.stderr, "No input vectors.", main_pat_filename
38 sys.exit()
40 # Create PCA object, trained on input_vectors
41 pca = PCA(input_vectors, output_dim=10)
42 #pca = PCA(input_vectors, reduce=True)
44 # Perform a PCA on input vectors
45 input_vectors = pca.process_list_of_vectors(input_vectors)
47 ### Now we rescale vectors, so that each component fits on -1.0 to 1.0
48 ### this makes a very nice plot!!
49 r = Rescale(-1.0,1.0)
51 ### Normalize each component separately
52 # We need to transpose input_vectors - a list of per-player-vector-of-pca-component
53 # to get list of vectors of per-component-vector-of-player-data
54 def transpose(list_of_vectors):
55 return zip(*list_of_vectors)
56 input_vectors = transpose([ r(vector) for vector in transpose(input_vectors)])
58 # prints vectors along with player names
59 for name,vector in izip(players, input_vectors):
60 # Substitute ' ' by '_' to allow for gnuplot plotting (recognizing columns correctly)
61 name_to_print = '_'.join(name.split())
63 for p, i in izip(vector, count()):
64 print name_to_print, i+1, p
66 #print name_to_print,
67 #print_vector(vector)
69 print "\nProjection info:"
70 P = pca.get_projection_info()
71 for y in xrange(1, P.shape[0]):
72 for x in xrange(1, P.shape[1]):
73 print y, x, P[y,x], ivg.ovg.stringof(x)
74 #print P
76 print >> sys.stderr, "\nNow print that by:"
77 print >> sys.stderr, 'gnuplot> set xrange[1:%d]'%(pca.pca.output_dim+1)
78 print >> sys.stderr, 'gnuplot> plot "./pca.data" using 2:3:1 with labels font "arial,10" left point pt 4 offset 1,0'