Skip to content

Instantly share code, notes, and snippets.

@danoneata
Last active January 19, 2024 20:24
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danoneata/49a807f47656fedbb389 to your computer and use it in GitHub Desktop.
Save danoneata/49a807f47656fedbb389 to your computer and use it in GitHub Desktop.
Reading fvecs format in Python
import numpy as np
def fvecs_read(filename, c_contiguous=True):
fv = np.fromfile(filename, dtype=np.float32)
if fv.size == 0:
return np.zeros((0, 0))
dim = fv.view(np.int32)[0]
assert dim > 0
fv = fv.reshape(-1, 1 + dim)
if not all(fv.view(np.int32)[:, 0] == dim):
raise IOError("Non-uniform vector sizes in " + filename)
fv = fv[:, 1:]
if c_contiguous:
fv = fv.copy()
return fv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment