.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/annoy/plot_simple_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_simple_script.py: Simple annoy.AnnoyIndex with examples ======================================= An example showing the :py:class:`~scikitplot.annoy.AnnoyIndex` class. .. GENERATED FROM PYTHON SOURCE LINES 12-14 from annoy import AnnoyIndex from scikitplot.annoy import AnnoyIndex .. GENERATED FROM PYTHON SOURCE LINES 14-29 .. code-block:: Python from scikitplot.annoy import Index as AnnoyIndex 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) print(idx.get_nns_by_item(0, 100)) print(idx.get_nns_by_vector([1.0, 0.5, 0.5], 100)) idx.info() .. rst-class:: sphx-glr-script-out .. code-block:: none [0, 1, 2] [0, 1, 2] {'f': 3, 'metric': 'angular', 'n_neighbors': 5, 'on_disk_path': None, 'prefault': False, 'seed': None, 'verbose': None, 'schema_version': 0, 'n_items': 3, 'n_trees': 3, 'memory_usage_byte': 272, 'memory_usage_mib': 0.0002593994140625} .. GENERATED FROM PYTHON SOURCE LINES 30-61 .. 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_simple_script_001.png :alt: plot simple script :srcset: /auto_examples/annoy/images/sphx_glr_plot_simple_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-69 .. 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.054 seconds) .. _sphx_glr_download_auto_examples_annoy_plot_simple_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_simple_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_simple_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simple_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simple_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simple_script.zip ` .. include:: plot_simple_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_