.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/annoy/plot_precision_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_precision_script.py: annoy with examples ================================== An example showing the :py:func:`~scikitplot.cexternals.annoy` function. .. GENERATED FROM PYTHON SOURCE LINES 7-11 .. code-block:: Python :lineno-start: 8 # Authors: Spotify AB # SPDX-License-Identifier: Apache-2.0 .. GENERATED FROM PYTHON SOURCE LINES 12-60 .. code-block:: Python :lineno-start: 12 from __future__ import print_function import random, time # from annoy import AnnoyIndex from scikitplot.cexternals.annoy import AnnoyIndex try: xrange except NameError: # Python 3 compat xrange = range n, f = 100000, 40 t = AnnoyIndex(f, 'angular') for i in xrange(n): v = [] for z in xrange(f): v.append(random.gauss(0, 1)) t.add_item(i, v) t.build(2 * f) t.save('test.tree') limits = [10, 100, 1000, 10000] k = 10 prec_sum = {} prec_n = 1000 time_sum = {} for i in xrange(prec_n): j = random.randrange(0, n) closest = set(t.get_nns_by_item(j, k, n)) for limit in limits: t0 = time.time() toplist = t.get_nns_by_item(j, k, limit) T = time.time() - t0 found = len(closest.intersection(toplist)) hitrate = 1.0 * found / k prec_sum[limit] = prec_sum.get(limit, 0.0) + hitrate time_sum[limit] = time_sum.get(limit, 0.0) + T for limit in limits: print('limit: %-9d precision: %6.2f%% avg time: %.6fs' % (limit, 100.0 * prec_sum[limit] / (i + 1), time_sum[limit] / (i + 1))) .. rst-class:: sphx-glr-script-out .. code-block:: none limit: 10 precision: 13.79% avg time: 0.000055s limit: 100 precision: 21.05% avg time: 0.000061s limit: 1000 precision: 56.48% avg time: 0.000226s limit: 10000 precision: 97.13% avg time: 0.001538s .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. 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 19.800 seconds) .. _sphx_glr_download_auto_examples_annoy_plot_precision_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/main?urlpath=lab/tree/notebooks/auto_examples/annoy/plot_precision_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_precision_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_precision_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_precision_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_precision_script.zip ` .. include:: plot_precision_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_