make_density.py 797 B

123456789101112131415161718192021222324252627282930
  1. import os
  2. import numpy as np
  3. from typing import Any, List
  4. from scipy.stats import gaussian_kde
  5. def main(args: Any):
  6. vectors: np.array = np.load(args.vectors_npy)
  7. kde = gaussian_kde(vectors.T)
  8. density: np.array = kde(vectors.T)
  9. filename: str = os.path.basename(args.vectors_npy)
  10. name_pieces: List[str] = filename.split('.')
  11. name_pieces[0] = f'{name_pieces[0]}-density'
  12. filename = '.'.join(name_pieces)
  13. dirname: str = os.path.dirname(args.vectors_npy)
  14. np.save(os.path.join(dirname, filename), density)
  15. if __name__ == "__main__":
  16. import argparse
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument('vectors_npy', type=str,
  19. help='path to trained word2vec vectors.')
  20. args: Any = parser.parse_args()
  21. main(args)