Skip to main content

vdict

GitHub tests PyPI version MIT license

This a very thin wrapper around hnswlib to make it look like a python dictionary whose keys are numpy arrays. Install with pip install vdict.

from vdict import vdict
import numpy as np

data = vdict()
v1 = np.random.rand(32)
v2 = np.random.rand(32)
data[v1] = 'hello'
data[v2] = 32
assert data[v1] == 'hello'

You can have it throw IndexErrors if you try to access a key that doesn't exist:

data = vdict(tol=0.001)
v1 = np.random.rand(32)
v2 = np.random.rand(32)
data[v1] = 'hello'
# this will throw an IndexError because we didn't add yet!
print(data[v2])

The default tolerance is 1 (generally do not throw errors), but you can set it to a smaller value to make it more strict.

Details

  • All vectors must be the same length
  • Accessing with a vector gives the closest value keyed by the closest vector
  • The algorithm is approximate nearest neighbor search. You can tune the accuracy (see below)
  • You can have millions of vectors in the dictionary
  • If you know the approximate size, pass est_nelements to vidct() to reduce how often things are resized

Usage

The vdict class has some reasonable defaults, but you may need to tune for your use case. These are adjustable in the constructor. You can read about the parameters at the hnswlib. Briefly, the most important ones are:

  • M - the number of neighbors to consider when building the graph (higher M means more accurate, but more memory). 12-48 is typical.
  • space - the distance metric to use. The default is l2, but you can also use cosine or ip (inner product).
  • ef_construction - parameter that controls speed/accuracy trade-off during the index construction - 50 - 200 is typical.
from vdict import vdict
data = vdict(M=16, space='cosine', ef_construction=100)

# add some vectors
data[np.random.rand(32)] = 'hello'
data[np.random.rand(32)] = 'world'

License

MIT

Author

Andrew White

Metadata

Release files for vdict 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vdict 0.1.0
File Size Uploaded
vdict-0.1.0.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for vdict 0.1.0
File Interpreter ABI Platform
vdict-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / vdict-0.1.0.tar.gz

Download URL vdict-0.1.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4f8d2083b6587b682b6dfc930f183c8ffd3da4e918982c3835854059f220ff83
BLAKE2b-256 checksum
How to use checksums
13d4e051236105e6a265613c0b3aea3cdb24e0cf65d63b3a1a6cb5887a72b7bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release files / vdict-0.1.0-py3-none-any.whl

Download URL vdict-0.1.0-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e39bc767f5be37ed680c2a19e47fdd0c8c2d4c6d514e00ec4bb07e64fbc7f4f
BLAKE2b-256 checksum
How to use checksums
7d95d73121e8bea5fa3566f4253fd2fe09c42fbaa3e3dd4f0c7c3ad30b994e74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page