.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/annoy/plot_Annoy_legacy_c_api.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite or Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_annoy_plot_Annoy_legacy_c_api.py: annoy.Annoy legacy c-api with examples ========================================== An example showing the :py:class:`~scikitplot.annoy.Annoy` class. .. seealso:: * :py:obj:`~scikitplot.annoy.Index.from_low_level` * https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled .. GENERATED FROM PYTHON SOURCE LINES 16-24 .. code-block:: Python import random; random.seed(0) # from annoy import Annoy, AnnoyIndex from scikitplot.annoy import Annoy as AnnoyIndex print(AnnoyIndex.__doc__) .. rst-class:: sphx-glr-script-out .. code-block:: none Compiled with GCC/Clang. Using 512-bit AVX instructions. High-performance approximate nearest neighbours (Annoy) C++ core. This module is a low-level backend (``annoylib``). It exposes the C++-powered :class:`Annoy` type. For day-to-day work, prefer the high-level Python API in the :mod:`annoy` package: from annoy import Annoy, AnnoyIndex .. GENERATED FROM PYTHON SOURCE LINES 25-38 .. code-block:: Python # ============================================================= # 1. Construction # ============================================================= idx = AnnoyIndex(0) print("Index dimension:", idx.f) print("Metric :", idx.metric) print(idx) print(idx.info()) # help(idx.info) .. rst-class:: sphx-glr-script-out .. code-block:: none Index dimension: 0 Metric : None Annoy(f=0, metric='unknown', n_items=0, n_trees=0, on_disk_path=None) {'dimension': 0, 'metric': '', 'n_items': 0, 'n_trees': 0, 'memory_usage_byte': 0, 'memory_usage_mib': 0.0, 'on_disk_path': None} .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. code-block:: Python # ============================================================= # 1. Construction # ============================================================= idx = AnnoyIndex(f=3) print("Index dimension:", idx.f) print("Metric :", idx.metric) print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/repo/galleries/examples/annoy/plot_Annoy_legacy_c_api.py:43: FutureWarning: The default argument for metric will be removed in a future version of Annoy. Please pass metric='angular' explicitly. Index dimension: 3 Metric : angular Annoy(f=3, metric='angular', n_items=0, n_trees=0, on_disk_path=None) .. GENERATED FROM PYTHON SOURCE LINES 50-59 .. code-block:: Python # ============================================================= # 1. Construction # ============================================================= idx = AnnoyIndex(f=3, metric="angular") print("Index dimension:", idx.f) print("Metric :", idx.metric) .. rst-class:: sphx-glr-script-out .. code-block:: none Index dimension: 3 Metric : angular .. GENERATED FROM PYTHON SOURCE LINES 60-72 .. code-block:: Python # ============================================================= # 2. Add items # ============================================================= idx.add_item(0, [1, 0, 0]) idx.add_item(1, [0, 1, 0]) idx.add_item(2, [0, 0, 1]) print("Number of items:", idx.get_n_items()) print("Index dimension:", idx.f) print("Metric :", idx.metric) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of items: 3 Index dimension: 3 Metric : angular .. GENERATED FROM PYTHON SOURCE LINES 73-83 .. code-block:: Python # ============================================================= # 1. Construction # ============================================================= idx = AnnoyIndex(10, metric="angular") print("Index dimension:", idx.f) print("Metric :", idx.metric) idx.on_disk_build("annoy_test.annoy") # help(idx.on_disk_build) .. rst-class:: sphx-glr-script-out .. code-block:: none Index dimension: 10 Metric : angular True .. GENERATED FROM PYTHON SOURCE LINES 84-104 .. code-block:: Python # ============================================================= # 2. Add items # ============================================================= f=10 n=10 for i in range(n): if(i % (n//10) == 0): print(f"{i} / {n} = {1.0 * i / n}") # v = [] # for z in range(f): # v.append(random.gauss(0, 1)) v = [random.gauss(0, 1) for _ in range(f)] idx.add_item(i, v) print("Number of items:", idx.get_n_items()) print("Index dimension:", idx.f) print("Metric :", idx.metric) print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 / 10 = 0.0 1 / 10 = 0.1 2 / 10 = 0.2 3 / 10 = 0.3 4 / 10 = 0.4 5 / 10 = 0.5 6 / 10 = 0.6 7 / 10 = 0.7 8 / 10 = 0.8 9 / 10 = 0.9 Number of items: 10 Index dimension: 10 Metric : angular Annoy(f=10, metric='angular', n_items=10, n_trees=0, on_disk_path=annoy_test.annoy) .. GENERATED FROM PYTHON SOURCE LINES 105-116 .. code-block:: Python # ============================================================= # 3. Build index # ============================================================= idx.build(10) print("Trees:", idx.get_n_trees()) print("Memory usage:", idx.memory_usage(), "bytes") print(idx) print(idx.info()) # help(idx.build) .. rst-class:: sphx-glr-script-out .. code-block:: none Trees: 10 Memory usage: 1620 bytes Annoy(f=10, metric='angular', n_items=10, n_trees=10, on_disk_path=annoy_test.annoy) {'dimension': 10, 'metric': 'angular', 'n_items': 10, 'n_trees': 10, 'memory_usage_byte': 1620, 'memory_usage_mib': 0.001544952392578125, 'on_disk_path': 'annoy_test.annoy'} .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: Python idx.unbuild() print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none Annoy(f=10, metric='angular', n_items=10, n_trees=0, on_disk_path=annoy_test.annoy) .. GENERATED FROM PYTHON SOURCE LINES 121-126 .. code-block:: Python idx.build(10) print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none Annoy(f=10, metric='angular', n_items=10, n_trees=10, on_disk_path=annoy_test.annoy) .. GENERATED FROM PYTHON SOURCE LINES 127-136 .. code-block:: Python # ============================================================= # 1. Construction # ============================================================= idx = AnnoyIndex(0, metric="angular") print("Index dimension:", idx.f) print("Metric :", idx.metric) .. rst-class:: sphx-glr-script-out .. code-block:: none Index dimension: 0 Metric : angular .. GENERATED FROM PYTHON SOURCE LINES 137-157 .. code-block:: Python # ============================================================= # 2. Add items # ============================================================= f=10 n=10 for i in range(n): if(i % (n//10) == 0): print(f"{i} / {n} = {1.0 * i / n}") # v = [] # for z in range(f): # v.append(random.gauss(0, 1)) v = [random.gauss(0, 1) for _ in range(f)] idx.add_item(i, v) print("Number of items:", idx.get_n_items()) print("Index dimension:", idx.f) print("Metric :", idx.metric) print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 / 10 = 0.0 1 / 10 = 0.1 2 / 10 = 0.2 3 / 10 = 0.3 4 / 10 = 0.4 5 / 10 = 0.5 6 / 10 = 0.6 7 / 10 = 0.7 8 / 10 = 0.8 9 / 10 = 0.9 Number of items: 10 Index dimension: 10 Metric : angular Annoy(f=10, metric='angular', n_items=10, n_trees=0, on_disk_path=None) .. GENERATED FROM PYTHON SOURCE LINES 158-170 .. code-block:: Python # ============================================================= # 3. Build index # ============================================================= idx.build(10) print("Trees:", idx.get_n_trees()) print("Memory usage:", idx.memory_usage(), "bytes") print(idx) print(idx.info()) # help(idx.get_n_trees) .. rst-class:: sphx-glr-script-out .. code-block:: none Trees: 10 Memory usage: 1880 bytes Annoy(f=10, metric='angular', n_items=10, n_trees=10, on_disk_path=None) {'dimension': 10, 'metric': 'angular', 'n_items': 10, 'n_trees': 10, 'memory_usage_byte': 1880, 'memory_usage_mib': 0.00179290771484375, 'on_disk_path': None} .. GENERATED FROM PYTHON SOURCE LINES 171-185 .. code-block:: Python # ============================================================= # 4. Query — return NNSResult # ============================================================= res = idx.get_nns_by_item( 0, 5, # search_k = -1, include_distances=True, ) print(res) .. rst-class:: sphx-glr-script-out .. code-block:: none ([0, 2, 4, 5, 6], [0.0, 0.8915294408798218, 0.9434009790420532, 1.050995111465454, 1.2712162733078003]) .. GENERATED FROM PYTHON SOURCE LINES 186-198 .. code-block:: Python # ============================================================= # 8. Query using vector # ============================================================= res2 = idx.get_nns_by_vector( [random.gauss(0, 1) for _ in range(f)], 5, include_distances=True ) print("\nQuery by vector:", res2) .. rst-class:: sphx-glr-script-out .. code-block:: none Query by vector: ([4, 9, 0, 6, 8], [0.8781132102012634, 0.9961007237434387, 1.0966964960098267, 1.2096866369247437, 1.2793666124343872]) .. GENERATED FROM PYTHON SOURCE LINES 199-210 .. code-block:: Python # ============================================================= # 9. Low-level (non-result) mode # ============================================================= items = idx.get_nns_by_item(0, 2, include_distances=False) print("\nLow-level items only:", items) items_low, d_low = idx.get_nns_by_item(0, 2, include_distances=True) print("Low-level tuple return:", items_low, d_low) .. rst-class:: sphx-glr-script-out .. code-block:: none Low-level items only: [0, 2] Low-level tuple return: [0, 2] [0.0, 0.8915294408798218] .. GENERATED FROM PYTHON SOURCE LINES 211-225 .. code-block:: Python # ============================================================= # 10. Persistence # ============================================================= print("\n=== Saving with binary annoy ===") print(idx) idx.save("annoy_test.annoy") print(idx) print("Loading...") idx2 = AnnoyIndex(10, metric='angular').load("annoy_test.annoy") print("Loaded index:", idx2) .. rst-class:: sphx-glr-script-out .. code-block:: none === Saving with binary annoy === Annoy(f=10, metric='angular', n_items=10, n_trees=10, on_disk_path=None) Annoy(f=10, metric='angular', n_items=10, n_trees=19, on_disk_path=annoy_test.annoy) Loading... Loaded index: True .. GENERATED FROM PYTHON SOURCE LINES 226-239 .. code-block:: Python # ============================================================= # 11. Raw serialize / deserialize # ============================================================= print("\n=== Raw serialize ===") buf = idx.serialize() new_idx = AnnoyIndex(10, metric='angular') new_idx.deserialize(buf) print("Deserialized index n_items:", new_idx.get_n_items()) print(idx) print(new_idx) .. rst-class:: sphx-glr-script-out .. code-block:: none === Raw serialize === Deserialized index n_items: 10 Annoy(f=10, metric='angular', n_items=10, n_trees=19, on_disk_path=annoy_test.annoy) Annoy(f=10, metric='angular', n_items=10, n_trees=19, on_disk_path=None) .. GENERATED FROM PYTHON SOURCE LINES 240-245 .. code-block:: Python idx.unload() print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none Annoy(f=10, metric='angular', n_items=0, n_trees=0, on_disk_path=None) .. GENERATED FROM PYTHON SOURCE LINES 246-251 .. code-block:: Python # idx.build(10) idx.load("annoy_test.annoy") print(idx) .. rst-class:: sphx-glr-script-out .. code-block:: none Annoy(f=10, metric='angular', n_items=10, n_trees=19, on_disk_path=annoy_test.annoy) .. GENERATED FROM PYTHON SOURCE LINES 252-256 .. tags:: level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.022 seconds) .. _sphx_glr_download_auto_examples_annoy_plot_Annoy_legacy_c_api.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-plots/scikit-plots/main?urlpath=lab/tree/notebooks/auto_examples/annoy/plot_Annoy_legacy_c_api.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/annoy/plot_Annoy_legacy_c_api.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_Annoy_legacy_c_api.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_Annoy_legacy_c_api.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_Annoy_legacy_c_api.zip ` .. include:: plot_Annoy_legacy_c_api.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_