Commit 07088d4ec503010cac0269a501574acc48e2b3c8
1 parent
9fd9bc58ff
Exists in
master
Just change volia importing lines
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
volia/tsne.py
1 | ''' | 1 | ''' |
2 | The goal of this script is to display calculate tsne of pvectors. | 2 | The goal of this script is to display calculate tsne of pvectors. |
3 | ''' | 3 | ''' |
4 | 4 | ||
5 | import os | 5 | import os |
6 | from os.path import isfile | 6 | from os.path import isfile |
7 | import argparse | 7 | import argparse |
8 | import numpy as np | 8 | import numpy as np |
9 | from sklearn.manifold import TSNE | 9 | from sklearn.manifold import TSNE |
10 | 10 | ||
11 | from volia.data_io import read_features | 11 | from core.data import read_features |
12 | 12 | ||
13 | if __name__ == "__main__": | 13 | if __name__ == "__main__": |
14 | # Defining argparse | 14 | # Defining argparse |
15 | parser = argparse.ArgumentParser(prog='pvector tsne', description='Calculate the tsne representation of pvector in 3 or 2d') | 15 | parser = argparse.ArgumentParser(prog='pvector tsne', description='Calculate the tsne representation of pvector in 3 or 2d') |
16 | parser.add_argument('features', type=str, | 16 | parser.add_argument('features', type=str, |
17 | help='the path of the file you want to calculate tsne') | 17 | help='the path of the file you want to calculate tsne') |
18 | parser.add_argument('-o', '--outfile', type=str, | 18 | parser.add_argument('-o', '--outfile', type=str, |
19 | default='.', | 19 | default='.', |
20 | help='the path of the output file.') | 20 | help='the path of the output file.') |
21 | parser.add_argument('-n', '--n-comp', type=int, choices=[2, 3], | 21 | parser.add_argument('-n', '--n-comp', type=int, choices=[2, 3], |
22 | default='2', | 22 | default='2', |
23 | help='number of components output of tsne') | 23 | help='number of components output of tsne') |
24 | 24 | ||
25 | args = parser.parse_args() | 25 | args = parser.parse_args() |
26 | 26 | ||
27 | assert isfile(args.features) | 27 | assert isfile(args.features) |
28 | 28 | ||
29 | features_list = read_features(args.features) | 29 | features_list = read_features(args.features) |
30 | tuples_key_feat = np.vstack([ (key, feats) for key, feats in features_list.items()]) | 30 | tuples_key_feat = np.vstack([ (key, feats) for key, feats in features_list.items()]) |
31 | keys, features = zip(*tuples_key_feat) | 31 | keys, features = zip(*tuples_key_feat) |
32 | feat_tsne = TSNE(n_components=args.n_comp).fit_transform(features) | 32 | feat_tsne = TSNE(n_components=args.n_comp).fit_transform(features) |
33 | 33 | ||
34 | with open(args.outfile, "w") as of: | 34 | with open(args.outfile, "w") as of: |
35 | for i in range(len(keys)): | 35 | for i in range(len(keys)): |
36 | of.write(keys[i] + " " + " ".join([str(feat) for feat in feat_tsne[i]]) + "\n") | 36 | of.write(keys[i] + " " + " ".join([str(feat) for feat in feat_tsne[i]]) + "\n") |
37 | print("TSNE finished. Check if everything has been done well.") | 37 | print("TSNE finished. Check if everything has been done well.") |