From 36b1fe28e1062bbea78be96668058f52f37b4a5d Mon Sep 17 00:00:00 2001 From: Mathias Date: Mon, 14 Sep 2020 14:43:02 +0200 Subject: [PATCH] Reduice the reduction of given features using tsne algorithm --- scripts/dimreduction/tsne.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 scripts/dimreduction/tsne.py diff --git a/scripts/dimreduction/tsne.py b/scripts/dimreduction/tsne.py new file mode 100644 index 0000000..e88a10e --- /dev/null +++ b/scripts/dimreduction/tsne.py @@ -0,0 +1,37 @@ +''' +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 + +from volia.data_io import read_features + +if __name__ == "__main__": + # 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]]) + "\n") + print("TSNE finished. Check if everything has been done well.") \ No newline at end of file -- 1.8.2.3