Commit 36b1fe28e1062bbea78be96668058f52f37b4a5d

Authored by Mathias
1 parent 44060889b7
Exists in master

Reduice the reduction of given features using tsne algorithm

Showing 1 changed file with 37 additions and 0 deletions Side-by-side Diff

scripts/dimreduction/tsne.py
  1 +'''
  2 +The goal of this script is to display calculate tsne of pvectors.
  3 +'''
  4 +
  5 +import os
  6 +from os.path import isfile
  7 +import argparse
  8 +import numpy as np
  9 +from sklearn.manifold import TSNE
  10 +
  11 +from volia.data_io import read_features
  12 +
  13 +if __name__ == "__main__":
  14 + # Defining argparse
  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,
  17 + help='the path of the file you want to calculate tsne')
  18 + parser.add_argument('-o', '--outfile', type=str,
  19 + default='.',
  20 + help='the path of the output file.')
  21 + parser.add_argument('-n', '--n-comp', type=int, choices=[2, 3],
  22 + default='2',
  23 + help='number of components output of tsne')
  24 +
  25 + args = parser.parse_args()
  26 +
  27 + assert isfile(args.features)
  28 +
  29 + features_list = read_features(args.features)
  30 + tuples_key_feat = np.vstack([ (key, feats) for key, feats in features_list.items()])
  31 + keys, features = zip(*tuples_key_feat)
  32 + feat_tsne = TSNE(n_components=args.n_comp).fit_transform(features)
  33 +
  34 + with open(args.outfile, "w") as of:
  35 + for i in range(len(keys)):
  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.")