Blame view

volia/tsne.py 1.38 KB
36b1fe28e   Mathias   Reduice the reduc...
1
2
3
4
5
6
7
8
9
  '''
  The goal of this script is to display calculate tsne of pvectors.
  '''
  
  import os
  from os.path import isfile
  import argparse
  import numpy as np
  from sklearn.manifold import TSNE
07088d4ec   Quillot Mathias   Just change volia...
10
  from core.data import read_features
36b1fe28e   Mathias   Reduice the reduc...
11

e7d811503   Quillot Mathias   New file architec...
12
  if __name__ == "__main__":
36b1fe28e   Mathias   Reduice the reduc...
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
      # Defining argparse 
      parser = argparse.ArgumentParser(prog='pvector tsne', description='Calculate the tsne representation of pvector in 3 or 2d')
      parser.add_argument('features', type=str,
                          help='the path of the file you want to calculate tsne')
      parser.add_argument('-o', '--outfile', type=str,
                          default='.',
                          help='the path of the output file.')
      parser.add_argument('-n', '--n-comp', type=int, choices=[2, 3],
                          default='2',
                          help='number of components output of tsne')
  
      args = parser.parse_args()
  
      assert isfile(args.features)
  
      features_list = read_features(args.features)
      tuples_key_feat = np.vstack([ (key, feats) for key, feats in features_list.items()])
      keys, features = zip(*tuples_key_feat)
      feat_tsne = TSNE(n_components=args.n_comp).fit_transform(features)
      
      with open(args.outfile, "w") as of:
          for i in range(len(keys)):
              of.write(keys[i] + " " + " ".join([str(feat) for feat in feat_tsne[i]]) + "
  ")
      print("TSNE finished. Check if everything has been done well.")