API Reference

nmslib.init

This function acts act the main entry point into NMS lib. This function should be called first before calling any other method.

nmslib.init(space: unicode=u'cosinesimil', space_params: object=None, method: unicode=u'hnsw', data_type: nmslib.DataType=DataType.DENSE_VECTOR, dtype: nmslib.DistType=DistType.FLOAT) → object

This function initializes a new NMSLIB index

Parameters:
  • space (str optional) – The metric space to create for this index
  • space_params (dict optional) – Parameters for configuring the space
  • method (str optional) – The index method to use
  • data_type (nmslib.DataType optional) – The type of data to index (dense/sparse/string vectors)
  • dist_type (nmslib.DistType optional) – The type of index to create (float/double/int)
Returns:

Return type:

A new NMSLIB Index.

nmslib.FloatIndex

nmslib.dist.FloatIndex

class nmslib.dist.FloatIndex
addDataPoint(self: nmslib.dist.FloatIndex, id: int, data: object) → int

Adds a single datapoint to the index

Parameters:
  • id (int) – The id of the object to add
  • data (object) – The object to add to the index.
Returns:

The position the item was added at

Return type:

int

addDataPointBatch(self: nmslib.dist.FloatIndex, data: object, ids: object=None) → int

Adds multiple datapoints to the index

Parameters:
  • data (object) – The objects to add to the index.
  • ids (array_like optional) – The ids of the object being inserted. If not set will default to the row id of each object in the dataset
Returns:

The number of items added

Return type:

int

createIndex(self: nmslib.dist.FloatIndex, index_params: object=None, print_progress: bool=False) → None

Creates the index, and makes it available for querying

Parameters:
  • index_params (dict optional) – Dictionary of optional parameters to use in indexing
  • print_progress (bool optional) – Whether or not to display progress bar when creating index
getDistance(self: nmslib.dist.FloatIndex, arg0: int, arg1: int) → float
knnQuery(self: nmslib.dist.FloatIndex, vector: object, k: int=10L) → object

Finds the approximate K nearest neighbours of a vector in the index

Parameters:
  • vector (array_like) – A 1D vector to query for.
  • k (int optional) – The number of neighbours to return
Returns:

  • ids (array_like.) – A 1D vector of the ids of each nearest neighbour.
  • distances (array_like.) – A 1D vector of the distance to each nearest neigbhour.

knnQueryBatch(self: nmslib.dist.FloatIndex, queries: object, k: int=10L, num_threads: int=0L) → object

Performs multiple queries on the index, distributing the work over a thread pool :param input: A list of queries to query for :type input: list :param k: The number of neighbours to return :type k: int optional :param num_threads: The number of threads to use :type num_threads: int optional

Returns:A list of tuples of (ids, distances)
Return type:list
loadIndex(self: nmslib.dist.FloatIndex, filename: unicode, print_progress: bool=False) → None
print_progress: bool optional
Whether or not to display progress bar when creating index
saveIndex(self: nmslib.dist.FloatIndex, filename: unicode) → None

Saves the index to disk

Parameters:filename (str) – The filename to save to
setQueryTimeParams(self: nmslib.dist.FloatIndex, params: object=None) → None

Sets parameters used in knnQuery.

Parameters:params (dict) – A dictionary of params to use in querying. Setting params to None will reset

nmslib.DoubleIndex

nmslib.dist.DoubleIndex

class nmslib.dist.DoubleIndex
addDataPoint(self: nmslib.dist.DoubleIndex, id: int, data: object) → int

Adds a single datapoint to the index

Parameters:
  • id (int) – The id of the object to add
  • data (object) – The object to add to the index.
Returns:

The position the item was added at

Return type:

int

addDataPointBatch(self: nmslib.dist.DoubleIndex, data: object, ids: object=None) → int

Adds multiple datapoints to the index

Parameters:
  • data (object) – The objects to add to the index.
  • ids (array_like optional) – The ids of the object being inserted. If not set will default to the row id of each object in the dataset
Returns:

The number of items added

Return type:

int

createIndex(self: nmslib.dist.DoubleIndex, index_params: object=None, print_progress: bool=False) → None

Creates the index, and makes it available for querying

Parameters:
  • index_params (dict optional) – Dictionary of optional parameters to use in indexing
  • print_progress (bool optional) – Whether or not to display progress bar when creating index
getDistance(self: nmslib.dist.DoubleIndex, arg0: int, arg1: int) → float
knnQuery(self: nmslib.dist.DoubleIndex, vector: object, k: int=10L) → object

Finds the approximate K nearest neighbours of a vector in the index

Parameters:
  • vector (array_like) – A 1D vector to query for.
  • k (int optional) – The number of neighbours to return
Returns:

  • ids (array_like.) – A 1D vector of the ids of each nearest neighbour.
  • distances (array_like.) – A 1D vector of the distance to each nearest neigbhour.

knnQueryBatch(self: nmslib.dist.DoubleIndex, queries: object, k: int=10L, num_threads: int=0L) → object

Performs multiple queries on the index, distributing the work over a thread pool :param input: A list of queries to query for :type input: list :param k: The number of neighbours to return :type k: int optional :param num_threads: The number of threads to use :type num_threads: int optional

Returns:A list of tuples of (ids, distances)
Return type:list
loadIndex(self: nmslib.dist.DoubleIndex, filename: unicode, print_progress: bool=False) → None
print_progress: bool optional
Whether or not to display progress bar when creating index
saveIndex(self: nmslib.dist.DoubleIndex, filename: unicode) → None

Saves the index to disk

Parameters:filename (str) – The filename to save to
setQueryTimeParams(self: nmslib.dist.DoubleIndex, params: object=None) → None

Sets parameters used in knnQuery.

Parameters:params (dict) – A dictionary of params to use in querying. Setting params to None will reset

nmslib.IntIndex

nmslib.dist.IntIndex

class nmslib.dist.IntIndex
addDataPoint(self: nmslib.dist.IntIndex, id: int, data: object) → int

Adds a single datapoint to the index

Parameters:
  • id (int) – The id of the object to add
  • data (object) – The object to add to the index.
Returns:

The position the item was added at

Return type:

int

addDataPointBatch(self: nmslib.dist.IntIndex, data: object, ids: object=None) → int

Adds multiple datapoints to the index

Parameters:
  • data (object) – The objects to add to the index.
  • ids (array_like optional) – The ids of the object being inserted. If not set will default to the row id of each object in the dataset
Returns:

The number of items added

Return type:

int

createIndex(self: nmslib.dist.IntIndex, index_params: object=None, print_progress: bool=False) → None

Creates the index, and makes it available for querying

Parameters:
  • index_params (dict optional) – Dictionary of optional parameters to use in indexing
  • print_progress (bool optional) – Whether or not to display progress bar when creating index
getDistance(self: nmslib.dist.IntIndex, arg0: int, arg1: int) → int
knnQuery(self: nmslib.dist.IntIndex, vector: object, k: int=10L) → object

Finds the approximate K nearest neighbours of a vector in the index

Parameters:
  • vector (array_like) – A 1D vector to query for.
  • k (int optional) – The number of neighbours to return
Returns:

  • ids (array_like.) – A 1D vector of the ids of each nearest neighbour.
  • distances (array_like.) – A 1D vector of the distance to each nearest neigbhour.

knnQueryBatch(self: nmslib.dist.IntIndex, queries: object, k: int=10L, num_threads: int=0L) → object

Performs multiple queries on the index, distributing the work over a thread pool :param input: A list of queries to query for :type input: list :param k: The number of neighbours to return :type k: int optional :param num_threads: The number of threads to use :type num_threads: int optional

Returns:A list of tuples of (ids, distances)
Return type:list
loadIndex(self: nmslib.dist.IntIndex, filename: unicode, print_progress: bool=False) → None
print_progress: bool optional
Whether or not to display progress bar when creating index
saveIndex(self: nmslib.dist.IntIndex, filename: unicode) → None

Saves the index to disk

Parameters:filename (str) – The filename to save to
setQueryTimeParams(self: nmslib.dist.IntIndex, params: object=None) → None

Sets parameters used in knnQuery.

Parameters:params (dict) – A dictionary of params to use in querying. Setting params to None will reset