[docs]classAnnoyParameters(Parameters):"""Parameters for the Approximate Nearest Neighbors Oh Yeah (Annoy) algorithm, see `here <https://github.com/spotify/annoy>`_ for details. """
[docs]def__init__(self,num_trees:int=50,search_mult:Optional[float]=None,distance:Literal["Euclidean","Manhattan","Cosine"]="Euclidean",):""" Args: num_trees: Number of trees to use to generate the search index. More trees increase accuracy at the cost of more computational work, in terms of both the indexing time and search speed. search_mult: Multiplier for the number of observations to search. Specifically, the product of ``k`` and ``search_mult`` is used to define the number of points to search exhaustively and dictates the balance between search speed and accuracy. If None, this defaults to the value of ``num_trees``. distance: Distance metric for index construction and search. This should be one of ``Euclidean``, ``Manhattan`` or ``Cosine``. """self.num_trees=num_treesself.search_mult=search_multself.distance=distance
@propertydefdistance(self)->str:"""Distance metric, see :meth:`~__init__()`."""returnself._distance@distance.setterdefdistance(self,distance:str):""" Args: distance: Distance metric, see :meth:`~__init__()`. """ifdistancenotin["Euclidean","Manhattan","Cosine"]:raiseValueError("unsupported 'distance'")self._distance=distance@propertydefnum_trees(self)->int:"""Number of trees, see :meth:`~__init__()`."""returnself._num_trees@num_trees.setterdefnum_trees(self,num_trees:int):""" Args: num_trees: Number of trees, see :meth:`~__init__()`. """ifnum_trees<1:raiseValueError("'num_trees' should be a positive integer")self._num_trees=num_trees@propertydefsearch_mult(self)->int:"""Search multiplier, see :meth:`~__init__()`."""returnself._search_mult@search_mult.setterdefsearch_mult(self,search_mult:Optional[float]):""" Args: search_mult: Search multiplier, see :meth:`~__init__()`. """ifsearch_multisNone:search_mult=float(self._num_trees)ifsearch_mult<=1:raiseValueError("'search_mult' should be greater than 1")self._search_mult=search_mult
[docs]classAnnoyIndex(GenericIndex):"""A prebuilt index for the Approximate Nearest Neighbors Oh Yeah (Annoy) algorithm, created by :py:func:`~knncolle.define_builder.define_builder` with a :py:class:`~knncolle.annoy.AnnoyParameters` object. """
[docs]def__init__(self,ptr:int):""" Args: ptr: Address of a ``knncolle_py::WrappedPrebuilt`` containing an Annoy search index, allocated in C++. """super().__init__(ptr)