.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/annoy/plot_mmap_script.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_mmap_script.py: Mmap annoy.AnnoyIndex with examples ===================================== An example showing the :py:class:`~scikitplot.annoy.AnnoyIndex` class. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python from __future__ import print_function import random; random.seed(0) import time # from annoy import AnnoyIndex # from scikitplot.annoy import AnnoyIndex from scikitplot.annoy import Index as AnnoyIndex .. GENERATED FROM PYTHON SOURCE LINES 22-32 .. code-block:: Python idx = AnnoyIndex( f=3, metric='angular', ) idx.add_item(0, [1, 0, 0]) idx.add_item(1, [0, 1, 0]) idx.add_item(2, [0, 0, 1]) idx.build(-1) idx.save('test.annoy') .. raw:: html
Annoydev|0.4
Parameters
ParameterValue
f3
metric'angular'
n_neighbors5
on_disk_path'test.annoy'
prefaultFalse
seedNone
verboseNone
schema_version0


.. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: Python b = AnnoyIndex( f=3, metric='angular', ) b.load('test.annoy') .. raw:: html
Annoydev|0.4
Parameters
ParameterValue
f3
metric'angular'
n_neighbors5
on_disk_path'test.annoy'
prefaultFalse
seedNone
verboseNone
schema_version0


.. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python print(b.get_nns_by_item(0, 100)) print(b.get_nns_by_vector([1.0, 0.5, 0.5], 100)) .. rst-class:: sphx-glr-script-out .. code-block:: none [0, 1, 2] [0, 1, 2] .. GENERATED FROM PYTHON SOURCE LINES 44-75 .. code-block:: Python def plot(idx, y=None, **kwargs): import numpy as np import matplotlib.pyplot as plt import scikitplot.cexternals._annoy._plotting as utils single = np.zeros(idx.get_n_items(), dtype=int) if y is None: double = np.random.uniform(0, 1, idx.get_n_items()).round() # single vs double fig, ax = plt.subplots(ncols=2, figsize=(12, 5)) alpha = kwargs.pop("alpha", 0.8) y2 = utils.plot_annoy_index( idx, dims = list(range(idx.f)), plot_kwargs={"draw_legend": False}, ax=ax[0], )[0] utils.plot_annoy_knn_edges( idx, y2, k=1, line_kwargs={"alpha": alpha}, ax=ax[1], ) # idx.unbuild() # idx.build(10) plot(idx) .. image-sg:: /auto_examples/annoy/images/sphx_glr_plot_mmap_script_001.png :alt: plot mmap script :srcset: /auto_examples/annoy/images/sphx_glr_plot_mmap_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-83 .. tags:: model-type: classification model-workflow: impute plot-type: bar level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.157 seconds) .. _sphx_glr_download_auto_examples_annoy_plot_mmap_script.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/maintenance/0.4.X?urlpath=lab/tree/notebooks/auto_examples/annoy/plot_mmap_script.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_mmap_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mmap_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mmap_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mmap_script.zip ` .. include:: plot_mmap_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_